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/