18

我已经像这篇文章中描述的那样实现了 ItemTouchHelper: https ://medium.com/@ipaulpro/drag-and-swipe-with-recyclerview-b9456d2b1aaf#.k7xm7amxi

如果 RecyclerView 是 CoordinatorLayout 的子级,则一切正常。

但如果 R​​ecyclerView 是 CoordinatorLayout 中 NestedScrollView 的子项,则拖动滚动不再起作用。拖动一个项目并将其移动到屏幕的顶部或底部,RecyclerView 不会像它不是 NestedScrollView 的子项那样滚动。

有任何想法吗?

4

4 回答 4

2

您必须禁用nestedScrollingrecyclerView

recyclerView.setIsNestedScrollingEnabled(false);
于 2018-09-11T01:26:35.627 回答
1

我遇到了同样的问题,我花了将近一整天的时间来解决它。

前提:

首先,我的 xml 布局如下所示:

<CoordinatorLayout>
    <com.google.android.material.appbar.AppBarLayout
        ...
    </com.google.android.material.appbar.AppBarLayout>
    <NestedScrollView>
        <RecyclerView/>
    </NestedScrollView>
</CoordinatorLayout>

为了使滚动行为正常,我还让nestedScrolling残疾人RecyclerView士通过:RecyclerView.setIsNestedScrollingEnabled(false);

原因:

但是当我在其中拖动项目时,ItemTouchHelper我仍然无法按预期进行自动滚动。IT CANNOT SCROLLRecyclerview原因在于:scrollIfNecessary()ItemTouchHelper

boolean scrollIfNecessary() {
    RecyclerView.LayoutManager lm = mRecyclerView.getLayoutManager();
    if (mTmpRect == null) {
        mTmpRect = new Rect();
    }
    int scrollY = 0;
    lm.calculateItemDecorationsForChild(mSelected.itemView, mTmpRect);
    if (lm.canScrollVertically()) {
        int curY = (int) (mSelectedStartY + mDy);
        final int topDiff = curY - mTmpRect.top - mRecyclerView.getPaddingTop();
        if (mDy < 0 && topDiff < 0) {
            scrollY = topDiff;
        } else if (mDy > 0) {
            final int bottomDiff = curY + mSelected.itemView.getHeight() + mTmpRect.bottom
                    - (mRecyclerView.getHeight() - mRecyclerView.getPaddingBottom());
            if (bottomDiff > 0) {
                scrollY = bottomDiff;
            }
        }
    }
    if (scrollY != 0) {
        scrollY = mCallback.interpolateOutOfBoundsScroll(mRecyclerView,
                mSelected.itemView.getHeight(), scrollY,
                mRecyclerView.getHeight(), scrollDuration);
    }
    if (scrollY != 0) {
        mRecyclerView.scrollBy(scrollX, scrollY);
        return true;
    }
    return false;
}
  • 原因一:nestedScrollingforRecyclerView设置为 false 时,实际上有效的滚动对象是NestedScrollView,它是 的父对象RecyclerView。所以RecyclerView.scrollBy(x, y)这里根本不起作用!
  • 原因2: mRecyclerView.getHeight()比 大得多NestedScrollView.getHeight()。因此,当我将项目拖到RecyclerView底部时,结果scrollIfNecessary()也是错误的。
  • 原因 3: mSelectedStartY在我们的案例中,它看起来不像预期值。因为我们需要在我们的例子中计算scrollYof NestedScrollView

因此,我们需要重写这个方法来满足我们的期望。解决方案来了:

解决方案:

步骤1:

为了覆盖这个scrollIfNecessary()(这个方法不是),你需要在一个与'spublic同名的包下新建一个类。ItemTouchHelper像这样: 示例代码

第2步:

除了覆盖之外scrollIfNecessary(),我们还需要覆盖select(),以便在开始拖动时获取mSelectedStartYscrollY的值NestedScrollView

public override fun select(selected: RecyclerView.ViewHolder?, actionState: Int) {
    super.select(selected, actionState)
    if (selected != null) {
        mSelectedStartY = selected.itemView.top
        mSelectedStartScrollY = (mRecyclerView.parent as NestedScrollView).scrollY.toFloat()
    }
}

注意: mSelectedStartY和对于向上或向下mSelectedStartScrollY滚动都非常重要。NestedScrollView

第 3 步:

现在我们可以覆盖scrollIfNecessary()了,你需要注意下面的注释:

public override fun scrollIfNecessary(): Boolean {
    ...
    val lm = mRecyclerView.layoutManager
    if (mTmpRect == null) {
        mTmpRect = Rect()
    }
    var scrollY = 0
    val currentScrollY = (mRecyclerView.parent as NestedScrollView).scrollY
    
    // We need to use the height of NestedScrollView, not RecyclerView's!
    val actualShowingHeight = (mRecyclerView.parent as NestedScrollView).height

    lm!!.calculateItemDecorationsForChild(mSelected.itemView, mTmpRect!!)
    if (lm.canScrollVertically()) {
        // The true current Y of the item in NestedScrollView, not in RecyclerView!
        val curY = (mSelectedStartY + mDy - currentScrollY).toInt()

        // The true mDy should plus the initial scrollY and minus current scrollY of NestedScrollView
        val checkDy = (mDy + mSelectedStartScrollY - currentScrollY).toInt()
        
        val topDiff = curY - mTmpRect!!.top - mRecyclerView.paddingTop
        if (checkDy < 0 && topDiff < 0) {// User is draging the item out of the top edge.
            scrollY = topDiff
        } else if (checkDy > 0) { // User is draging the item out of the bottom edge.
            val bottomDiff = (curY + mSelected.itemView.height + mTmpRect!!.bottom
                    - (actualShowingHeight - mRecyclerView.paddingBottom))
            if (bottomDiff > 0) {
                scrollY = bottomDiff
            }
        }
    }
    if (scrollY != 0) {
        scrollY = mCallback.interpolateOutOfBoundsScroll(
            mRecyclerView,
            mSelected.itemView.height, scrollY, actualShowingHeight, scrollDuration
        )
    }
    if (scrollY != 0) {
        ...
        // The scrolling behavior should be assigned to NestedScrollView!
        (mRecyclerView.parent as NestedScrollView).scrollBy(0, scrollY)
        return true
    }
    ...
    return false
}

结果:

我可以通过下面的 Gif 向您展示我的工作:

结果

于 2022-01-13T16:34:22.683 回答
0

这是对我有用的解决方案。

创建 2 个自定义类

1> LockableScrollView

公共类 LockableScrollView 扩展 NestedScrollView {

// true if we can scroll (not locked)
// false if we cannot scroll (locked)
private boolean mScrollable = true;

public LockableScrollView(@NonNull Context context) {
    super(context);
}

public LockableScrollView(@NonNull Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
}

public LockableScrollView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}


public void setScrollingEnabled(boolean enabled) {
    mScrollable = enabled;
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    // Don't do anything with intercepted touch events if
    // we are not scrollable
    if (ev.getAction() == MotionEvent.ACTION_MOVE) {// if we can scroll pass the event to the superclass
        return mScrollable && super.onInterceptTouchEvent(ev);
    }
    return super.onInterceptTouchEvent(ev);

}

}

2>LockableRecyclerView 扩展 RecyclerView

public class LockableRecyclerView extends RecyclerView {

private LockableScrollView scrollview;

public LockableRecyclerView(@NonNull Context context) {
    super(context);
}

public LockableRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
}

public LockableRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

public void setScrollview(LockableScrollView lockedscrollview) {
    this.scrollview = lockedscrollview;
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    if (ev.getAction() == MotionEvent.ACTION_MOVE) {
        scrollview.setScrollingEnabled(false);
        return super.onInterceptTouchEvent(ev);
    }
    scrollview.setScrollingEnabled(true);
    return super.onInterceptTouchEvent(ev);

}

@Override
public boolean onTouchEvent(MotionEvent e) {
    if (e.getAction() == MotionEvent.ACTION_MOVE) {
        scrollview.setScrollingEnabled(false);
        return super.onTouchEvent(e);
    }
    scrollview.setScrollingEnabled(true);
    return super.onTouchEvent(e);

}

}

在 xml 中使用此视图而不是 NestedScrollView 和 RecyclerView

在 kotlin 文件中设置 recyclerView.setScrollview(binding.scrollView) recyclerView.isNestedScrollingEnabled = false

ItemTouchHelper(object : ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.UP) { override fun onMove( @NonNull recyclerView: RecyclerView, @NonNull viewHolder: RecyclerView.ViewHolder, @NonNull target: RecyclerView.ViewHolder ): Boolean { return false }

        override fun onSwiped(@NonNull viewHolder: RecyclerView.ViewHolder, direction: Int) {
            // when user swipe thr recyclerview item to right remove item from favorite list
            if (direction == ItemTouchHelper.UP) {

                val itemToRemove = favList[viewHolder.absoluteAdapterPosition]

            }
        }
    }).attachToRecyclerView(binding.recyclerView)
于 2022-02-02T07:04:49.467 回答
-1

android:descendantFocusability="blocksDescendants"

添加NestedScrollView并添加

安卓:focusableInTouchMode="true"

在子布局中,如下所示

   <androidx.core.widget.NestedScrollView 
        android:descendantFocusability="blocksDescendants"> 

    <androidx.constraintlayout.widget.ConstraintLayout
        android:focusableInTouchMode="true">
        </androidx.constraintlayout.widget.ConstraintLayout> 

</androidx.core.widget.NestedScrollView>

检查这个 github 仓库 https://github.com/khambhaytajaydip/Drag-Drop-recyclerview

于 2019-07-10T06:13:48.887 回答