美文网首页
Recycler View 适配器

Recycler View 适配器

作者: 桥北_2676 | 来源:发表于2018-03-13 11:42 被阅读0次

    public class MyAdapter extends RecyclerView.Adapterimplements View.OnClickListener { private ArrayListmList;  public MyAdapter(ArrayList mList) {

            this.mList = mList;

        }

        @Override

        public Holder onCreateViewHolder(ViewGroup parent, int viewType) {

            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recy_item, null);

            Holder holder = new Holder(view);

            //给这个VIew注册点击事件了

            view.setOnClickListener(this);

            return holder;

        }

        @Override

        public void onBindViewHolder(Holder holder, int position) {

            holder.mText.setText(mList.get(position));

            //itemVIew 跟 View一个东西  传入标记

            holder.itemView.setTag(position);

        }

        @Override

        public int getItemCount() {

            return mList.size();

        }

        public class Holder extends RecyclerView.ViewHolder {

            private TextView mText;

            public Holder(View itemView) {

                super(itemView);

                mText = itemView.findViewById(R.id.Recy_Name);

            }

        }

        public interface OnItemClick {

            void setOnItemClick(View v, int position);

        }

        private OnItemClick mOnItem;

        @Override

        public void onClick(View v) {

            if (mOnItem != null) {

                mOnItem.setOnItemClick(v, (int) v.getTag());

            }

        }

        public void setOnItem(OnItemClick onItem) {

            this.mOnItem = onItem;

        }

    }

    相关文章

      网友评论

          本文标题:Recycler View 适配器

          本文链接:https://www.haomeiwen.com/subject/bmxzfftx.html