安卓recyclerview点击事件
时间: 2023-07-05 11:09:36 浏览: 102
实现 RecyclerView 的点击事件有多种方式,其中较为常用的两种方式如下:
1. 在 Adapter 中设置监听器
在 onBindViewHolder() 方法中为 RecyclerView 中的每个 item 设置点击监听器,代码如下:
```
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private List<String> mData;
private OnItemClickListener mClickListener;
public void setClickListener(OnItemClickListener clickListener) {
mClickListener = clickListener;
}
// ...
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
String item = mData.get(position);
holder.mTextView.setText(item);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mClickListener != null) {
mClickListener.onItemClick(position);
}
}
});
}
public interface OnItemClickListener {
void onItemClick(int position);
}
// ...
}
```
在 Activity 或 Fragment 中设置监听器并处理点击事件,代码如下:
```
public class MyActivity extends AppCompatActivity {
// ...
private void initRecyclerView() {
RecyclerView recyclerView = findViewById(R.id.recycler_view);
MyAdapter adapter = new MyAdapter(mData);
adapter.setClickListener(new MyAdapter.OnItemClickListener() {
@Override
public void onItemClick(int position) {
// 处理点击事件
}
});
recyclerView.setAdapter(adapter);
}
// ...
}
```
2. 实现 RecyclerView.OnItemTouchListener 接口
自定义一个 RecyclerView.OnItemTouchListener 实现类,然后实现其中的方法来处理点击事件,代码如下:
```
public class MyOnItemTouchListener implements RecyclerView.OnItemTouchListener {
private GestureDetector mGestureDetector;
private OnItemClickListener mClickListener;
public MyOnItemTouchListener(Context context, final RecyclerView recyclerView, OnItemClickListener clickListener) {
mClickListener = clickListener;
mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) {
View childView = recyclerView.findChildViewUnder(e.getX(), e.getY());
if (childView != null && mClickListener != null) {
int position = recyclerView.getChildAdapterPosition(childView);
mClickListener.onItemClick(position);
return true;
}
return false;
}
});
}
@Override
public boolean onInterceptTouchEvent(@NonNull RecyclerView rv, @NonNull MotionEvent e) {
return mGestureDetector.onTouchEvent(e);
}
@Override
public void onTouchEvent(@NonNull RecyclerView rv, @NonNull MotionEvent e) {
// do nothing
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
// do nothing
}
public interface OnItemClickListener {
void onItemClick(int position);
}
}
```
在 Activity 或 Fragment 中设置 OnItemTouchListener 并处理点击事件,代码如下:
```
public class MyActivity extends AppCompatActivity {
// ...
private void initRecyclerView() {
RecyclerView recyclerView = findViewById(R.id.recycler_view);
MyAdapter adapter = new MyAdapter(mData);
recyclerView.setAdapter(adapter);
recyclerView.addOnItemTouchListener(new MyOnItemTouchListener(this, recyclerView, new MyOnItemTouchListener.OnItemClickListener() {
@Override
public void onItemClick(int position) {
// 处理点击事件
}
}));
}
// ...
}
```
以上是两种常用的 RecyclerView 点击事件实现方式,你可以选择其中一种或者结合使用。
阅读全文