有任何想法吗?
您需要禁用recyclerView
的nestedScrolling
:
recyclerView.setIsNestedScrollingEnabled(false);
我遇到了同样的问题,花了近一整天的时间来解决它。
首先,我的XML布局如下:
<CoordinatorLayout>
<com.google.android.material.appbar.AppBarLayout
...
</com.google.android.material.appbar.AppBarLayout>
<NestedScrollView>
<RecyclerView/>
</NestedScrollView>
</CoordinatorLayout>
为了使滚动行为正常,我还让RecyclerView
的nestedScrolling
失效了:RecyclerView.setIsNestedScrollingEnabled(false);
但是使用ItemTouchHelper
时,当我在Recyclerview
中拖动项目时,仍然无法使其自动滚动。造成无法滚动的原因在于ItemTouchHelper
的scrollIfNecessary()
方法中:
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;
}
RecyclerView
的nestedScrolling
设置为false时,实际上有效的滚动对象是NestedScrollView
,它是RecyclerView
的父级。因此,RecyclerView.scrollBy(x, y)
在这里根本不起作用!mRecyclerView.getHeight()
比NestedScrollView.getHeight()
大得多。所以,当我将RecyclerView
中的项目拖到底部时,scrollIfNecessary()
的结果也为false。mSelectedStartY
并不像我们的情况中预期的那样。因为我们需要计算NestedScrollView
的scrollY
。因此,我们需要重写这个方法来满足我们的期望。以下是解决方案:
scrollIfNecessary()
(此方法不是public
),您需要在与ItemTouchHelper
相同的包下新建一个类。像这样:
scrollIfNecessary()
之外,我们还需要覆盖select()
以便在开始拖动时获取mSelectedStartY
和NestedScrollView
的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()
}
}
注意:mSelectedStartY
和mSelectedStartScrollY
对于上下滚动NestedScrollView
都非常重要。
现在我们可以重写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展示我的工作:
// 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)
<androidx.core.widget.NestedScrollView
android:descendantFocusability="blocksDescendants">
<androidx.constraintlayout.widget.ConstraintLayout
android:focusableInTouchMode="true">
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>