提问者:小点点

ItemTouchHelper在NestedScrollView中使用Recy的查看:拖动滚动不起作用


我已经实现了ItemTouchHelper,如本文所述:https://medium.com/@ipaulpro/drag-and-swipe-with-recyclerview-b9456d2b1aaf#.k7xm7amxi

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

但是,如果 RecyclerView 是 CoordinatorLayout 中 NestedScrollView 的子级,则拖动滚动将不再起作用。疏通一个项目并将其移动到屏幕的顶部或底部,如果它不是 NestedScrollView 的子项,则回收器视图不会像它那样滚动。

有什么想法吗?


共3个答案

匿名用户

您必须禁用回收器视图嵌套滚动

recyclerView.setIsNestedScrollingEnabled(false);

匿名用户

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

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

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

为了使滚动行为正常,我还让nestedScrollingRecy的视图禁用:RecyclerView.setIsNestedScrollingEn的(假);

但是使用ItemTouchHelper,当我在其中拖动项目时,我仍然无法使Recyclerview按预期自动滚动。它无法滚动的原因在 ItemTouchHelper 的方法 scrollIfEssential() 中:

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;
}
  • 原因1:当RecyclerViewnestedScrolling设置为false时,实际上有效的滚动对象是NestedScrollView,它是RecyclerView。所以RecyclerView。scrollBy(x,y)这里根本不起作用
  • 原因2:mRecyclerView。getHeight()比嵌套ScrollView.getHeight(。因此,当我将RecyclerView中的项目拖到底时,scrollIfNecessary()的结果也是false
  • 原因3:mSelectedStartY在我们的案例中看起来不像预期值。因为在我们的例子中,我们需要计算NestedScrollViewscrollY

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

除了覆盖< code > scrolllifnessary()之外,我们还需要覆盖< code>select(),以便在开始拖动时获取< code>mSelectedStartY的值和< code>NestedScrollView的< code>scrollY。

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()
    }
}

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

现在我们可以重写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 向您展示我的作品:

匿名用户

这是适合我的解决方案。

创建2个自定义类

公共类 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

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.isNestedScrollingEn的=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)