一、adapter代码如下:
public class RecyclerHorizontalAdapter extends RecyclerView.Adapter<RecyclerHorizontalAdapter.MyViewHolder > implements View.OnClickListener{ private Context mContext; private List<CarModelType> items; private int checkPosition; public OnItemClickListener onItemClickListener; public interface OnItemClickListener{ void onItemClick(int position, View itemView); } public void setOnItemClickListener(OnItemClickListener onItemClickListener){ this.onItemClickListener = onItemClickListener; } public RecyclerHorizontalAdapter(Context context, List<CarModelType> items){ this.mContext = context; this.items = items; } public int getCheckPosition() { return checkPosition; } public void setCheckPosition(int checkPosition) { this.checkPosition = checkPosition; } @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { //负责创建视图 View view = LayoutInflater.from(mContext).inflate(R.layout.item_make_year, null); //将全局的监听传递给holder MyViewHolder holder = new MyViewHolder(view); return holder; } @Override public void onClick(View view) { int position = (int) view.getTag(); if(null != onItemClickListener){ onItemClickListener.onItemClick(position, view); } } @Override public void onBindViewHolder(MyViewHolder holder, int position) { CarModelType item = items.get(position); holder.SetContent(item); holder.itemView.setTag(position); if(position == getCheckPosition()){ holder.yearName.setTextColor(mContext.getResources().getColor(R.color.deep_sky_blue)); holder.cutLine.setBackgroundColor(mContext.getResources().getColor(R.color.deep_sky_blue)); }else{ holder.yearName.setTextColor(mContext.getResources().getColor(R.color.montana)); holder.cutLine.setBackgroundColor(mContext.getResources().getColor(R.color.white)); } } @Override public int getItemCount() { return null != items ? items.size() : 0; } public class MyViewHolder extends RecyclerView.ViewHolder{ public TextView yearName; public View cutLine; public MyViewHolder(View itemView) { super(itemView); yearName = (TextView) itemView.findViewById(R.id.car_year_name); cutLine = (View) itemView.findViewById(R.id.cut_line); //将全局的监听赋值给接口 itemView.setOnClickListener(RecyclerHorizontalAdapter.this); } public void SetContent(CarModelType item ){ yearName.setText(item.getName()); } } }
二、activity中的使用方式如下:
1.
2.
implements RecyclerHorizontalAdapter.
OnItemClickListener
recyclerHorizontalAdapter.setOnItemClickListener(this);
3.
private void initView() { recyclerHorizontal = (RecyclerView) findViewById(R.id.recycler_horizontal);//款型类别列表 LinearLayoutManager mLayoutManage = new LinearLayoutManager(this);//线性布局管理器 mLayoutManage.setOrientation(LinearLayoutManager.HORIZONTAL);// 设置 recyclerView 布局方式为横向布局 recyclerHorizontal.setLayoutManager(mLayoutManage); recyclerHorizontalAdapter = new RecyclerHorizontalAdapter(this, items); recyclerHorizontal.setAdapter(recyclerHorizontalAdapter); }
4.
@Override public void onItemClick(int position, View itemView) { //拿适配器调用适配器内部自定义好的setCheckPosition方法(参数写点击事件的参数的position) recyclerHorizontalAdapter.setCheckPosition(position); //不要忘记刷新适配器 recyclerHorizontalAdapter.notifyDataSetChanged(); }