【发布时间】:2025-12-05 13:50:01
【问题描述】:
我有一个带有按钮的 recyclerview 项目。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_margin="1dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="1dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1"
android:id ="@+id/layLeft">
<TextView
android:id="@+id/txtName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginTop="2dp"
android:textColor="@android:color/black" />
<TextView
android:id="@+id/txtDesc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="2dp"
android:layout_marginLeft="2dp"
android:layout_marginTop="2dp"
android:layout_marginRight="2dp"
android:ellipsize="end"
android:maxLines="1" />
<TextView
android:id="@+id/txtCategory"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:textColor="@android:color/holo_red_dark" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ImageButton
android:id="@+id/btn_mark_done"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:padding="4dp"
android:src="@android:drawable/ic_menu_set_as"
android:layout_centerHorizontal="true"
android:adjustViewBounds="true"
android:scaleType="fitXY"/>
<ImageButton
android:id="@+id/btn_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:padding="4dp"
android:src="@android:drawable/ic_menu_delete"
android:layout_centerHorizontal="true"
android:adjustViewBounds="true"
android:scaleType="fitXY"/>
</LinearLayout>
</LinearLayout>
</FrameLayout>
为处理 RecyclerView 项目和单个按钮的单击事件创建了一个自定义侦听器。单击按钮时,单击事件侦听器可以正常工作。而且 RecyclerView itemclick 监听器也被监听。这会导致双重执行。
下面给出了监听器
class RecyclerTouchListener implements RecyclerView.OnItemTouchListener{
private ClickListener clicklistener;
private GestureDetector gestureDetector;
public RecyclerTouchListener(Context context, final RecyclerView recycleView, final ClickListener clicklistener){
this.clicklistener=clicklistener;
gestureDetector=new GestureDetector(context,new GestureDetector.SimpleOnGestureListener(){
@Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
@Override
public void onLongPress(MotionEvent e) {
View child=recycleView.findChildViewUnder(e.getX(),e.getY());
if(child!=null && clicklistener!=null){
clicklistener.onLongPress(recycleView.getChildAdapterPosition(child));
}
}
});
}
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
View child=rv.findChildViewUnder(e.getX(),e.getY());
if(child!=null && clicklistener!=null && gestureDetector.onTouchEvent(e)){
clicklistener.launchIntent(rv.getChildAdapterPosition(child));
}
return false;
}
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) { }
}
对于按钮
btnMarkDone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clickListener.onMarkDone( 0);
}
});
btnDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clickListener.onDeleteItem( 0);
}
});
有什么办法可以避免吗?
【问题讨论】:
标签: android button android-recyclerview onclicklistener