【问题标题】:ListView inside SwipeRefreshLayout inside Drawer(Layout): does not lock scroll direction抽屉(布局)内的SwipeRefreshLayout内的ListView:不锁定滚动方向
【发布时间】:2016-04-06 12:47:06
【问题描述】:

ListView 将其滚动方向锁定在滚动开始的方向。这很适合这种配置:

DrawerLayout
    ListView

当我向上/向下滑动时,列表会滚动。当我向左滚动时,抽屉关闭:完美。当我开始向下滑动然后改变方向时,初始方向(水平/垂直)被锁定。

但是,如果我像这样将列表包装在 SwipeRefreshLayout 中:

DrawerLayout
    SwipeRefreshLayout
        ListView

.. 然后滚动/滑动方向的锁定不再起作用。当我向上/向下滑动然后向左/向右滑动一点时,列表滚动并且抽屉也移动。后者不是我想要的。

关于如何使用 SwipeRefreshLayout 恢复以前的行为有什么建议吗?

【问题讨论】:

    标签: android listview


    【解决方案1】:

    我遇到了同样的问题,但使用的是 RecyclerView 而不是 ListView。最简单的解决方法是扩展 SwipeRefreshLayout,当检测到向下滚动动作时,告诉父(抽屉)布局不要拦截事件(例如滑动抽屉)。

    import android.content.Context;
    import android.support.v4.widget.SwipeRefreshLayout;
    import android.util.AttributeSet;
    import android.view.MotionEvent;
    import android.view.ViewConfiguration;
    
    public class DrawerSwipeRefreshLayout extends SwipeRefreshLayout {
    
        private int touchSlop;
        private float initialDownY;
    
        public DrawerSwipeRefreshLayout(Context context, AttributeSet attrs) {
            super(context, attrs);
            touchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
        }
    
        @Override
        public boolean onInterceptTouchEvent(MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    initialDownY = event.getY();
                    break;
    
                case MotionEvent.ACTION_MOVE:
                    final float eventY = event.getY();
                    float yDiff = Math.abs(eventY - initialDownY);
    
                    if (yDiff > touchSlop) {
                        getParent().requestDisallowInterceptTouchEvent(true);
                    }
            }
    
            return super.onInterceptTouchEvent(event);
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      您可以尝试在手势检测器的帮助下确定滑动是水平的还是垂直的。如果它的垂直将触摸事件给列表视图,否则给 DrawerLayout。

      【讨论】:

      • 我以前从未使用过它,但会试一试。谢谢。
      【解决方案3】:

      我使用的是支持库版本“23.2.1”,没有问题。

      【讨论】:

        猜你喜欢
        • 2020-03-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-02
        相关资源
        最近更新 更多