我有一个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项的单击事件和单个按钮。 当单击按钮时,它与click事件侦听器一起正常工作。 而且还会侦听RecycerView 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);
}
});
有什么办法可以避免吗?
我猜你应该尽量避免实现recycleView.onItemTouchListener,insted使用带有适配器的recycle视图,并在recycleView项单击时使用TouchListener,代码如下-
公共类RecycleAdapter扩展了RecycleAdapter.Adapter
private Context context;
// your data variables
public RecycleAdapter(Context context) {
this.context = context;
}
// implement your click and touch listeners here
@NonNull
@Override
public CustomViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new CustomViewHolder(LayoutInflater.from(context).inflate(R.layout.item_layout, parent, false));
}
@Override
public void onBindViewHolder(@NonNull CustomViewHolder holder, int position) {
//data binding code
}
@Override
public int getItemCount() {
// return size;
}
public class CustomViewHolder extends RecyclerView.ViewHolder {
ImageView btnMarkDone, btnMarkDone;
public CustomViewHolder(@NonNull View itemView) {
super(itemView);
btnMarkDone = itemView.findViewById(R.id.btnMarkDone);
btnMarkDone = itemView.findViewById(R.id.btnMarkDone);
btnMarkDone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// click listener here
}
});
btnDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// click listener here
}
});
itemView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
//implement touch listener action here
return false;
}
});
}
}
}
如果希望对循环视图滚动操作,请使用recycleerview.onscrolllistener
希望这能帮上忙。
快乐的编码!