android - ListView inside SwipeRefreshLayout inside Drawer(Layout) : does not lock scroll direction

标签 android listview

ListView 将其滚动方向锁定在滚动开始的方向。这适用于此配置:

DrawerLayout
    ListView

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

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

DrawerLayout
    SwipeRefreshLayout
        ListView

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

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

最佳答案

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

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

}

关于android - ListView inside SwipeRefreshLayout inside Drawer(Layout) : does not lock scroll direction,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31852687/

相关文章:

android - 懒加载ListView(不是那种懒)

android - getEditableText 和 getText 有什么区别?

android - '安卓 :paddingRight’ working with ‘android:drawableRight’

c# - 我如何在我的 ASP.NET 应用程序中为我的 ListView 使用 ItemCommand 事件

android - ListView 项目在滚动时随机排列

ios - SwiftUI - 使用上下文菜单删除列表中的行 - UI 故障

android - 如何重用相同的 Kotlin Flow 来执行相同的代码

javascript - 我一直在修复此共享短信功能的错误,但是当我通过谷歌信使应用程序共享时,它不会预先填充文本正文

android - 在多个平台上运行一个 Appium 测试

android - Listview 上的 ListSelector 不工作