addHeaderView в виде сетки переработчика? - PullRequest
0 голосов
/ 21 июня 2019

подскажите, пожалуйста, как мы можем добавить заголовок в RecyclerView`.

Я просмотрел все учебные пособия и предложил переполнение стека, но все они устарели.

У меня есть вид сеткии я хочу добавить заголовок одной картинки, пожалуйста, помогите мне

Я сделал макет с именем top_view.xml для заголовка, а также адаптер с именем WordAdapeter

Не можем ли мыдобавить несколько кодов для заголовка в WordAdapter?

public class WordAdapter extends RecyclerView.Adapter<WordAdapter.ViewHolder> {


    private List<Word> wordList;

    private OnNoteListener mOnNoteListener;

    public boolean isHeader(int position) {
        return position == 0;
    }

    public WordAdapter(List<Word> wordList, OnNoteListener onNoteListener) {
        this.wordList = wordList;
        this.mOnNoteListener = onNoteListener;
    }

    public WordAdapter(OnNoteListener onNoteListener) {
        this(new ArrayList<Word>(), onNoteListener);
    }

    public void addItems(List<Word> items) {
        wordList.addAll(items);
        notifyDataSetChanged();
    }

    public void clear() {
        wordList.clear();
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.home_item_view, viewGroup, false);
        return new ViewHolder(view, mOnNoteListener);

    }



    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewholder, int position) {
        int resource = wordList.get(position).getImageResource();
        String title = wordList.get(position).getTitle();
        viewholder.setData(resource, title);
    }

    @Override
    public int getItemCount() {
        return wordList.size();
    }



    static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private ImageView imageView;
        private TextView title;

        private OnNoteListener onNoteListener;

        public ViewHolder(@NonNull View itemView, OnNoteListener onNoteListener) {
            super(itemView);

            imageView = itemView.findViewById(R.id.imageView);
            title = itemView.findViewById(R.id.word);
            this.onNoteListener = onNoteListener;

            itemView.setOnClickListener(this);
        }

        private void setData(int resource, String titleText) {
            imageView.setImageResource(resource);
            title.setText(titleText);
        }

        @Override
        public void onClick(View view) {
            onNoteListener.onNoteClick(getAdapterPosition());
        }
    }

    public interface OnNoteListener {
        void onNoteClick(int position);
    }
}
...