bertvh
bertvh

Reputation: 831

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

A ListView locks its scrolling direction in the direction scrolling started. This works great with this configuration:

DrawerLayout
    ListView

When I swipe up/down, the list scrolls. When I scroll left, the drawer closes: perfect. When I start swiping down and then change direction, the initial direction (horizontal/vertical) islocked.

However, if I wrap the list in a SwipeRefreshLayout like this:

DrawerLayout
    SwipeRefreshLayout
        ListView

.. then the locking of the scroll/swipe direction does not work anymore. When I swipe up/down and then a bit to the left/right, the list scrolls and the drawer also moves. The latter is not what I want.

Any suggestions on how to get the former behavior back with the SwipeRefreshLayout?

Upvotes: 4

Views: 578

Answers (3)

cranehuang
cranehuang

Reputation: 505

I use support library version "23.2.1", and I don't have the problem.

Upvotes: 0

aluxian
aluxian

Reputation: 1076

I had the same problem but with a RecyclerView instead of a ListView. The easiest fix is to extend SwipeRefreshLayout and when a scroll down motion is detected, tell the parent (drawer) layout not to intercept events (e.g. sliding the drawer).

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

}

Upvotes: 1

Anitha Manikandan
Anitha Manikandan

Reputation: 1170

What you can try is, with the help of Gesture Detector, find whether the swipe is horizontal or vertical. If its vertical give the touch event to list view, otherwise to DrawerLayout.

Upvotes: 0

Related Questions