RecycleView 列表的item点击事件

时间:2022-03-07 14:46:34

在工作中,用到横向滑动,选择年份且选择的item变色的功能,就想到了RecycleView可以完成横向滑动,结果在使用时发现,布局虽然没有问题,但是RecycleView没有item点击事件,更不用说类似于RadioButton的单选,后经查询,网上建议,可自己写接口回调,完成item的点击事件,后根据这一思路,完成item点击事件以及item的单选,以下为代码:

Adapter的代码:

public class RecycleSelectDateAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
private List<String> listYears;//年份列表(最近三年)
private Context context;
private OnRecycleItemClickListener mListener;//设置item点击监听事件

private List<Boolean> list_click=new ArrayList<>();//判断该item 是否被点击

public RecycleSelectDateAdapter(Context context) {
listYears=new ArrayList<>();
int year_now= Integer.parseInt(Utils.getCustonFormatTime("yyyy"));

listYears.add(year_now-3+"");
listYears.add(year_now-2+"");
listYears.add(year_now-1+"");
listYears.add(year_now+"");
listYears.add(year_now+1+"");
listYears.add(year_now+2+"");
listYears.add(year_now+3+"");

resetIsClick();

this.context = context;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.recy_year_item,parent,false);
MyViewHolder viewHolder = new MyViewHolder(view);

return viewHolder;
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
final MyViewHolder myHolder=(MyViewHolder) holder;
myHolder.tv_time_year.setText(listYears.get(position));

if (!list_click.get(position)){
myHolder.tv_time_year.setTextColor(Color.BLACK);
}else {
myHolder.tv_time_year.setTextColor(Color.WHITE);
}
myHolder.tv_time_year.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mListener!=null){
resetIsClick();
list_click.remove(position);
list_click.add(position,true);
mListener.onItemCiclk(position,myHolder.tv_time_year);
notifyDataSetChanged();
}
}
});


}

@Override
public int getItemCount() {
return listYears.size();
}

public class MyViewHolder extends RecyclerView.ViewHolder{
TextView tv_time_year;
// List<Boolean> booleans=new ArrayList<>();
public MyViewHolder(View itemView) {
super(itemView);
tv_time_year=(TextView) itemView.findViewById(R.id.tv_time_year);
}

}

//设置监听
public void setOnItemClickListener(OnRecycleItemClickListener listener){
mListener=listener;
}

//重置判断是否被监听
public void resetIsClick(){
list_click.clear();
for (int i=0;i<listYears.size();i++){
list_click.add(false);
}
}

public interface OnRecycleItemClickListener{
void onItemCiclk(int pos,TextView textView);
}
}

该部分代码,主要功能的实现逻辑在 onBindViewHolder()中,当TextView被点击时,即传入接口回调,其中list_click集合为记录哪个位置的item被点击,然后调用 notifyDataSetChanged();重新刷新adapter的事件,在调用该方法时,adapter的构造方法会执行一次,MyViewHolder会执行一次,onBindViewHolder()会执行全部次数;

下列为Activity中的代码:

 //设置布局管理器
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
recyclerview_year.setLayoutManager(linearLayoutManager);

dateAdapter=new RecycleSelectDateAdapter(this);
recyclerview_year.setAdapter(dateAdapter);

//监听点击的item
dateAdapter.setOnItemClickListener(new RecycleSelectDateAdapter.OnRecycleItemClickListener() {
@Override
public void onItemCiclk(int pos, TextView textView) {
str_year=textView.getText().toString()+"年";
if (TextUtils.isEmpty(str_month)){//未选择月
tv_title_name.setText(str_year);
}else {
tv_title_name.setText(str_year+str_month);
}
}
});
此时完成了adapter的item点击监听事件