addHeaderView在回收站网格视图中?

时间:2019-06-21 13:46:39

标签: java android android-recyclerview android-gridview

请建议我如何在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);
    }
}

0 个答案:

没有答案