优于过去版本的适配器,更加灵活,方便复用
public class ListAllAdapter extends RecyclerView.Adapter<ListAllAdapter.ListAllViewHodler> { private List<Course> list; private Context context; public ListAllAdapter(Context context,List<Course> list){ this.context = context; this.list = list; } @Override public ListAllViewHodler onCreateViewHolder(ViewGroup parent, int viewType) { ListAllViewHodler hodler = new ListAllViewHodler(LayoutInflater.from(context).inflate(R.layout.list_all_item,null)); return hodler; } @Override public void onBindViewHolder(ListAllViewHodler holder, final int position) { holder.imv.setImageURI(Uri.parse(list.get(position).imageUrl)); holder.tv.setText(list.get(position).name); holder.imv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(context, LessonDetailActivity.class); Bundle bundle = new Bundle(); bundle.putSerializable("course",list.get(position)); intent.putExtras(bundle); context.startActivity(intent); } }); } @Override public int getItemCount() { return list.size(); } class ListAllViewHodler extends RecyclerView.ViewHolder{ SimpleDraweeView imv; TextView tv; public ListAllViewHodler(View itemView) { super(itemView); imv = (SimpleDraweeView)itemView.findViewById(R.id.course_imv); tv = (TextView)itemView.findViewById(R.id.course_name); } } }