Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Horizontal RecyclerView inside ViewPager is not scrolling

I have a horizontal RecyclerView inside a Complex hierarchy that looks like this -

<ViewPager id="+@id/first">
    <ViewPager id="+@id/second"> this viewpager is taking away the scroll event
        <SwipeToRefreshLayout>
            <RecyclerView> //this one is vertical
                <RecyclerView id="@id/rv1"> //this one is horizontal
                <RecyclerView id="@id/rv2"> //this one is working fine (different type)
            </RecyclerView>
        </SwipeToRefreshLayout>
    </ViewPager>
</ViewPager>

Now the problem is that the second ViewPager is hijacking the scroll from Horizontal RV. There are 2 type of Horizontal RV (RV1 and RV2) in that vertical RV. But only one of them (RV1) is facing this problem. The second one (RV2) is working properly. Also when I press and hold then the scrolling is working fine. And when RV1 is already scrolling and has not settle, then also scrolling works fine. I have referred to other answers talking about setting nestedScrolling false. Nothing seems to be working.

like image 457
Rishabh876 Avatar asked Jan 19 '19 17:01

Rishabh876


People also ask

Can recyclerview scroll horizontally?

Otherwise, they won’t scroll horizontally; the swipe is taken by the rootRecyclerView and so the you would see vertical movement.

What is the difference between viewpager2 and recyclerview in OpenCV?

I implemented one of them is a horizontal ViewPager2 and another one is implemented with horizontal RecyclerView (say, childRecyclerView ). The rootRecyclerView swipes vertically whereas the viewPager2 and childRecyclerView swipes horizontally.

Can a recyclerview have different kinds of rows?

I have a RecyclerView (say, rootRecyclerView) that can have different kinds of rows depending on some API response. I implemented one of them is a horizontal ViewPager2 and another one is implemented with horizontal RecyclerView (say, childRecyclerView ).

How do I know if I'm in the last item in recyclerview?

The Recyclerview also has another function to find out if you're in the last item in a very simple way using the canScrollHorizontally or canScrollVertically functions. So we basically tell the program to disallow scrolling on the parent (the Viewpager) when we haven’t reached the last item.


2 Answers

You can achieve this by overriding onInterceptTouchEvent method:

 mRecyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
            @Override
            public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent event) {
                int action = event.getAction();
               
                switch (action) {
                    case MotionEvent.ACTION_DOWN:
                        rv.getParent().requestDisallowInterceptTouchEvent(true);

                        break;
                
                }
                return false;
            }

            @Override
            public void onTouchEvent(@NonNull RecyclerView view, @NonNull MotionEvent event) {

            }

            @Override
            public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

            }
        });
like image 97
Astrit Veliu Avatar answered Sep 19 '22 12:09

Astrit Veliu


This answer is Kotlin version of @Astril Veliu's answer

yourRecyclerView.addOnItemTouchListener(object : RecyclerView.OnItemTouchListener {
            
            override fun onTouchEvent(view: RecyclerView, event: MotionEvent) {}
            
            override fun onInterceptTouchEvent(view: RecyclerView, event: MotionEvent): Boolean {
                when (event.action) {
                    MotionEvent.ACTION_DOWN -> {
                        yourRecyclerView.parent?.requestDisallowInterceptTouchEvent(true)
                    }
                }
                return false
            }
            
            override fun onRequestDisallowInterceptTouchEvent(disallowIntercept: Boolean) {}
        })
like image 27
OhhhThatVarun Avatar answered Sep 20 '22 12:09

OhhhThatVarun