我有一个RecyclerView,其中添加了可以对列表项执行的不同操作,特别是DragDrop
和SwipeDismiss
。 DragDrop
由长按发起,而SwipeDismiss
则由滑动发起。我必须监听滚动事件以阻止这些操作的发生。在滚动时突然转换为拖动很让人烦恼。之前我会添加一个OnScrollListener
来处理这个问题。
以下是我之前所做的:
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if(newState == RecyclerView.SCROLL_STATE_IDLE){
drag.setCurrentAction(IDLE);
} else {
drag.setCurrentAction(SCROLL);
}
}
不再工作
最近我添加了可折叠工具栏的CoordinatorLayout。现在,当向上滚动时,drag
直到工具栏被折叠后才会被设置为SCROLL
。我试图创建自己的行为,但那将替换我正在使用的当前行为;@string/appbar_scrolling_view_behavior
。这删除了我想要的工具栏行为,并且无法通知我的drag
状态。
我试过的方法:
@Override
public boolean onStartNestedScroll(CoordinatorLayout cl, CollapsingToolbarLayout child, View directTargetChild,
View target, int nestedScrollAxes) {
if (nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL) {
if (drag.getCurrentAction().ordinal() < SCROLL.ordinal()) {
/* No actions have started and we are scrolling. set the new action */
drag.setCurrentAction(SCROLL);
}
return true;
}
return super.onStartNestedScroll(cl, child, directTargetChild, target, nestedScrollAxes);
}
@Override
public void onStopNestedScroll(CoordinatorLayout coordinatorLayout, CollapsingToolbarLayout child, View target) {
super.onStopNestedScroll(coordinatorLayout, child, target);
if (drag.getCurrentAction() == SCROLL) {
drag.setCurrentAction(INVALID);
}
}
所以我的问题是
如何监听被CoordinatorLayout
拦截的recycler view滚动变化?使用Behavior是否是正确的方式?
ItemTouchHandler
删除滑动/拖动代码将会更加容易,而它与CoordinatorLayout
和嵌套滚动非常兼容。 - ianhanniballake