RecyclerView 横向列表 及 adapter 打造 item监听 改变元素属性

 
 
一、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.
 
 
implements RecyclerHorizontalAdapter. OnItemClickListener

2.
 

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();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值