gpt4 book ai didi

android - CoordinatorLayout 与 RecyclerView : onScroll

转载 作者:太空狗 更新时间:2023-10-29 15:26:18 28 4
gpt4 key购买 nike

我有一个 RecyclerView,我在其中添加了可以对列表项执行的不同操作。特别是 DragDropSwipeDismissDragDrop 由长按发起,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 for collapsable toolbars .现在向上滚动时,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);
}
}

所以我的问题

如何监听我的回收器 View 上被 CoordinatorLayout 拦截的那些滚动变化?行为是正确的方法吗?

最佳答案

您是否尝试添加一个 touchEvent 监听器,然后停用父 View 组持有者的拦截器事件?我认为这会将您要处理的任何触摸事件的控制权交还给 child 。

    scrollView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// Disallow the touch request for parent scroll on touch of child view

v.getParent().requestDisallowInterceptTouchEvent(true);

} else if (event.getAction() == MotionEvent.ACTION_UP) {

v.getParent().requestDisallowInterceptTouchEvent(false);
}
return false;
}
});

关于android - CoordinatorLayout 与 RecyclerView : onScroll,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31352069/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com