March3April4
March3April4

Reputation: 2281

Android recyclerView inside NestedScrollView Horizontal Scrolling hard to do

I have a nestedScrollView with many recyclerViews which will be using linearLayoutManager(HORIZONTAL).

It flings OK with nestedScrollingEnabled(false), but the horizontal scroll is extremely hard to do.

If I want to scroll a recyclerView right, I have to totally stop scrolling down the nestedScrollView, then scroll right.

Setting the nestedScrollingEnabled to true did nothing but disabling the nestedScrollView's fling.

What can I do to make the horizontal recyclerViews to scroll feel easier?

Upvotes: 0

Views: 2071

Answers (3)

I also faced same issue few months back and only solution that worked for me was by creating your own customized RecyclerView and NestedScrollView as :

MeroHorizontalRecyclerView class

public class MeroHorizontalRecyclerView extends RecyclerView {

private GestureDetector mGestureDetector;

public MeroHorizontalRecyclerView(Context context) {
    super(context);
    init();
}

public MeroHorizontalRecyclerView(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    init();
}

public MeroHorizontalRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
}

private void init() {
    LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
    layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
    setLayoutManager(layoutManager);

    mGestureDetector = new GestureDetector(getContext(), new VerticalScrollDetector());
}

@Override
public boolean onInterceptTouchEvent(MotionEvent e) {
    if (mGestureDetector.onTouchEvent(e)) {
        return false;
    }
    return super.onInterceptTouchEvent(e);
}

public class VerticalScrollDetector extends
        GestureDetector.SimpleOnGestureListener {

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        return Math.abs(distanceY) > Math.abs(distanceX);
    }

}

}

MeroNestedScrollView class

public class MeroNestedScrollView extends NestedScrollView {

public MeroNestedScrollView(Context context) {
    super(context);
}

public MeroNestedScrollView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public MeroNestedScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}


private float xDistance, yDistance, lastX, lastY;

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    final float x = ev.getX();
    final float y = ev.getY();
    switch (ev.getAction()) {
        case MotionEvent.ACTION_DOWN:
            xDistance = yDistance = 0f;
            lastX = ev.getX();
            lastY = ev.getY();

            // This is very important line that fixes
            computeScroll();


            break;
        case MotionEvent.ACTION_MOVE:
            final float curX = ev.getX();
            final float curY = ev.getY();
            xDistance += Math.abs(curX - lastX);
            yDistance += Math.abs(curY - lastY);
            lastX = curX;
            lastY = curY;

            if (xDistance > yDistance) {
                return false;
            }
    }


    return super.onInterceptTouchEvent(ev);
}

}

Now import them in xml as:

<com.hancha.utils.MeroNestedScrollView
        android:id="@+id/nestedScroll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

  <com.hancha.utils.MeroHorizontalRecyclerView
                android:id="@+id/recycler_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/app_bg_color"/>

</com.hancha.utils.MeroNestedScrollView>

Hope this helps!

Upvotes: 0

Khemraj Sharma
Khemraj Sharma

Reputation: 58934

Really appreciate your question, it is very typical to manage if you don't know the way. Here is a solution.

Magic method is requestDisallowInterceptTouchEvent, this method can allow or deny touch events.

public static void smartScroll(final ScrollView scrollView, final RecyclerView recyclerView) {
    recyclerView.setOnTouchListener(new View.OnTouchListener() {
        private boolean isListOnTop = false, isListOnBottom = false;
        private float delta = 0, oldY = 0;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    scrollView.requestDisallowInterceptTouchEvent(true);
                    recyclerView.requestDisallowInterceptTouchEvent(false);
                    oldY = event.getY();
                    break;
                case MotionEvent.ACTION_UP:
                    delta = 0;
                    break;
                case MotionEvent.ACTION_MOVE:
                    delta = event.getY() - oldY;
                    oldY = event.getY();

                    isListOnTop = false;
                    isListOnBottom = false;

                    View first = recyclerView.getChildAt(0);
                    View last = recyclerView.getChildAt(recyclerView.getChildCount() - 1);

                    LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                    if (first != null && layoutManager.findFirstVisibleItemPosition() == 0 && first.getTop() == 0 && delta > 0.0f) {
                        isListOnTop = true;
                    }
                    if (last != null && layoutManager.findLastVisibleItemPosition() == recyclerView.getChildCount() - 1 && last.getBottom() <= recyclerView.getHeight() && delta < 0.0f) {
                        isListOnBottom = true;
                    }

                    if ((isListOnTop && delta > 0.0f) || (isListOnBottom && delta < 0.0f)) {
                        scrollView.requestDisallowInterceptTouchEvent(false);
                        recyclerView.requestDisallowInterceptTouchEvent(true);
                    }
                    break;
                default:
                    break;
            }
            return false;
        }
    });
}

This solution is for ScrollView and RecyclerView but you can use this for any kind of Views like ViewPager, ScrollView, ListView by using requestDisallowInterceptTouchEvent.

Upvotes: 3

Dhaval Solanki
Dhaval Solanki

Reputation: 4705

Can you please try following example or way may be this will help you.

myRecyclerViewHorizental.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false) {
            @Override
            public boolean canScrollHorizontally() {
                //return false if you don't want to scroll horizontal
                return true;
            }

            @Override
            public boolean canScrollVertically() {
                //return false if you don't want to scroll vertical
                return false;
            }
        });

Upvotes: -1

Related Questions