recycleview 点击条目背景 字体改变

1.recycleview没有点击条目的监听,首先接口回调

//定义一个变量存储数据

private OnItemClickListner  onItemClickListner;

public interface OnItemClickListner {

        void onItemClick(int position);

    }

public void setOnitemClickListner(OnItemClickListner onItemClickListner) {

this.onItemClickListner = onItemClickListner;

}

((OneViewHolder) holder).llHole.setOnClickListener(new View.OnClickListener() {

@Override

    public void onClick(View v) {

onItemClickListner.onItemClick(position);

notifyDataSetChanged();

}

});

2.在设置好条目的监听之后设置位置的方法

adapter.setOnitemClickListner(new RecycleListAdapter.OnItemClickListner() {

@Override

    public void onItemClick(int position) {

adapter.setThisPosition(position);

adapter.notifyDataSetChanged();

}

});

//先声明一个int成员变量

private int thisPosition;

//再定义一个int类型的返回值方法

public int getthisPosition() {

return thisPosition;

}

//其次定义一个方法用来绑定当前参数值的方法

//此方法是在调用此适配器的地方调用的,此适配器内不会被调用到

public void setThisPosition(int thisPosition) {

this.thisPosition = thisPosition;

}

在onbindViewHolder中根据位置判断更改的效果

if (position == getthisPosition()) {

((OneViewHolder) holder).llHole.setBackgroundResource(R.drawable.background_coner_color);

((OneViewHolder) holder).ivImage.setBackgroundResource(R.color.colorBlack);

((OneViewHolder) holder).tvText.setTextColor(context.getResources().getColor(R.color.colorGray));

}else {

((OneViewHolder) holder).llHole.setBackgroundResource(R.drawable.background_coner_color2);

((OneViewHolder) holder).ivImage.setBackgroundResource(R.color.colorAccent);

((OneViewHolder) holder).tvText.setTextColor(context.getResources().getColor(R.color.colorwhite));

}

你可能感兴趣的:(recycleview 点击条目背景 字体改变)