简体   繁体   English

在 recyclerview 中拖动不起作用,但滑动有效?

[英]Dragging in recyclerview is not working but swiping is?

I'm trying to get the dragging function to work on my recyclerView but only the swiping works at the moment.我正在尝试让拖动功能在我的recyclerView上工作,但目前只有滑动工作。

I only have the basic code which as far as I understand should enable the action我只有基本代码,据我所知应该启用该操作

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);

And this code relates to the .XML file below此代码与下面的 .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>

It such a simple thing that I'm very confused as to why it wouldn't be working since the swiping works flawlessly.这是一件如此简单的事情,我很困惑为什么它不能工作,因为刷卡工作完美无缺。 I can't seem to find anyone else who stumbled upon the same issue.我似乎找不到其他人偶然发现了同样的问题。

try this尝试这个

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM