首页 > 其他分享 >RecyclerView在条目点击中传点击位置给Adapter

RecyclerView在条目点击中传点击位置给Adapter

时间:2023-02-01 12:37:35浏览次数:45  
标签:... 中传 int Adapter 点击 position RecyclerView


  1. RecyclerView.Adapter中定义变量记录被点击位置
private int mSelectedPosition;

public void setSelectedPosition(int position) {
mSelectedPosition = position;
}
  1. 在RecyclerView的点击事件中设置点击位置
ItemClickSupport.addTo(rv).setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {
@Override public void onItemClicked(RecyclerView recyclerView, int position, View v) {
mAdapter.setSelectedPosition(position); // 在Adapter中记录当前位置
mAdapter.notifyDataSetChanged();
mSelectedPosition = position; // 在当前上下文记录点击的位置
}
});
  1. RecyclerView.Adapter中对记录的点击位置进行处理
@Override public void onBindViewHolder(final ItemHolder holder, final int position) {
...
...
if (mSelectedPosition != position) { // 设置当前被点击位置的条目控件效果等
holder...
} else {
holder...
}
}


标签:...,中传,int,Adapter,点击,position,RecyclerView
From: https://blog.51cto.com/u_15950249/6031331

相关文章