RecycleView使用流程

2020-09-11  本文已影响0人  EraJieZhang

Activity

成员变量

DomeRecycleAdapter domeRecycleAdapter;
List<DomeRecycleBean> mList = new ArrayList<>();

初始化布局

domeRecycleAdapter = new DomeRecycleAdapter(this,mList);
自定义的点击事件实现

        domeRecycleAdapter.setOnItemClickListener(new DomeRecycleAdapter.OnItemClickListener() {
            ...
        });
        
        mRvPhizList.setLayoutManager(new LinearLayoutManager(this));  
        mRvPhizList.setAdapter(domeRecycleAdapter);

Adapter

classname类名

    class classname extends RecyclerView.Adapter<classname.MyViewHolder>
    private List<DomeRecycleBean> mList;
    private Context mContext;
    private LayoutInflater mInflater;
    /**
    *构造方法
    */
     public classname(Context context, List<DomeRecycleBean> list) {
        mContext = context;
        mList = list;
        mInflater = LayoutInflater.from(context);
    }
    /**
    *自动生成
    */
    @NonNull
    @Override
    public 内部类 onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new 内部类(mInflater.inflate(R.layout.item_download_phiz, viewGroup, false));
    }
    
    public static class 内部类 extends RecyclerView.ViewHolder {
        ImageView mItemPhizIcon;
        public MyViewHolder(View itemView) {
            super(itemView);
            mItemPhizIcon = itemView.findViewById(R.id.item_phiz_icon);
        }
    }
    
    /**
     * 自定义点击事件接口
     */
    public interface OnItemClickListener {
        /**
         * 点击事件
         *
         * @param view     控件
         * @param position 下标
         */
        void onClick(View view, int position);
        void onDownLoadPHIZ(int position,String path);
    }

    /**
     * 点击事件
     *
     * @param onItemClickListener 传过来的点击事件
     */
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    private OnItemClickListener mOnItemClickListener;
上一篇下一篇

猜你喜欢

热点阅读