在recyclerview中拖动不起作用,但刷卡可以吗?

问题描述

我正在尝试使拖动功能在我的recyclerView上运行,但目前仅可滑动。

我只有基本的代码,据我了解应该可以执行该操作

itemtouchhelper helper = new itemtouchhelper(new itemtouchhelper.SimpleCallback(itemtouchhelper.UP | itemtouchhelper.DOWN,itemtouchhelper.LEFT | itemtouchhelper.RIGHT) {
            @Override
            public boolean onMove(@NonNull RecyclerView recyclerView,@NonNull RecyclerView.ViewHolder dragged,@NonNull RecyclerView.ViewHolder target) {


                return false;
            }

            @Override
            public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder,int direction) {

            }
        });
        helper.attachToRecyclerView(recyclerView);

代码与下面的.XML文件相关

<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusable="true"
    android:clickable="true">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/historyView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:focusable="true"
        android:clickable="true"
        /> </androidx.constraintlayout.widget.ConstraintLayout>

这是一件很简单的事情,我很困惑为什么它不能正常工作,因为刷卡工作正常。我似乎找不到其他偶然发现同一问题的人。

解决方法

尝试

@Override
public int getSwipeDirs(RecyclerView recyclerView,RecyclerView.ViewHolder viewHolder) {
    if (viewHolder instanceof CartAdapter.MyViewHolder) return 0;
    return super.getSwipeDirs(recyclerView,viewHolder);
}