RecyclerView的使用

//继承的adapter

public class RecyclerAdapter extends RecyclerView.Adapter <RecyclerAdapter.ViewHolder> implements View.OnClickListener {
    private Context context;
    private List<Data.DataBean.ComicsBean> recylist;
    private  RecyclerListeaer.onItemListeaer  onItemListeaer;


    public RecyclerAdapter(Context context, List<Data.DataBean.ComicsBean> recylist) {
        this.context = context;
        this.recylist = recylist;
    }

    public  void  setOnItemListeaer(RecyclerListeaer.onItemListeaer  listeaer){
    this.onItemListeaer = listeaer;
}
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.recycler,null);
        view.setOnClickListener(this);
        ViewHolder  vh = new ViewHolder(view);
        return vh;
    }

    //绑定也就是赋值
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.itemView.setTag(position);
       holder.textView.setText(recylist.get(position).getTitle());
        Glide.with(context).load(recylist.get(position).getCover_image_url()).into(holder.imageView);
    }

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

    @Override
    public void onClick(View v) {
        if (onItemListeaer!=null){
            onItemListeaer.onItemClick(v, (Integer) v.getTag());
        }

    }

    class ViewHolder extends RecyclerView.ViewHolder{
private ImageView imageView;
        private TextView  textView;
        public ViewHolder(View itemView) {
            super(itemView);
          imageView = (ImageView) itemView.findViewById(R.id.recycler_iv);
            textView = (TextView) itemView.findViewById(R.id.recycler_tv);
        }
    }

}
//使用接口监听

public class RecyclerListeaer {
   public  interface  onItemListeaer{
        void onItemClick(View v,int position);
    }


}
//主类的使用

 GridLayoutManager  gm = new GridLayoutManager(getActivity(),3);//这个点你使用什么类型就创建什么类型的管理器
                rlv.setLayoutManager(gm);
                RecyclerAdapter adapter = new RecyclerAdapter(getActivity(), recylist);
                adapter.setOnItemListeaer(new RecyclerListeaer.onItemListeaer() {
                    @Override
                    public void onItemClick(View v, int position) {
                          Intent  intent = new Intent(getActivity(), WebAcitvity.class);
                           intent.putExtra("url",recylist.get(position).getUrl());
                        startActivity(intent);
                    }
                });
                rlv.setAdapter(adapter);
                adapter.notifyDataSetChanged();


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值