//继承的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();