Android Layout: Horizontal Recyclerview Inside a Vertical Recyclerview Inside a Viewpager with Scroll Behaviors

Android Layout: Horizontal Recyclerview inside a Vertical Recyclerview inside a Viewpager with Scroll Behaviors

Tested solution:

All you need is to call mInnerRecycler.setNestedScrollingEnabled(false); on your inner RecyclerViews



Explanation:

RecyclerView has support for nested scrolling introduced in API 21 through implementing the NestedScrollingChild interface. This is a valuable feature when you have a scrolling view inside another one that scrolls in the same direction and you want to scroll the inner View only when focused.

In any case, RecyclerView by default calls RecyclerView.setNestedScrollingEnabled(true); on itself when initializing. Now, back to the problem, since both of your RecyclerViews are within the same ViewPager that has the AppBarBehavior, the CoordinateLayout has to decide which scroll to respond to when you scroll from your inner RecyclerView; when your inner RecyclerView's nested scrolling is enabled, it gets the scrolling focus and the CoordinateLayout will choose to respond to its scrolling over the outer RecyclerView's scrolling. The thing is that, since your inner RecyclerViews don't scroll vertically, there is no vertical scroll change (from the CoordinateLayout's point of view), and if there is no change, the AppBarLayout doesn't change either.

In your case, because your inner RecyclerViews are scrolling in a different direction, you can disable it, thus causing the CoordinateLayout to disregard its scrolling and respond to the outer RecyclerView's scrolling.


Notice:

The xml attribute android:nestedScrollingEnabled="boolean" is not intended for use with the RecyclerView, and an attempt to use android:nestedScrollingEnabled="false" will result in a java.lang.NullPointerException so, at least for now, you will have to do it in code.

Vertical and Horizontal Recyclerview in the same layout

That's called a nested recycler view. you basically have a vertical recycler view in which you inflate your child recycler views and inside the child recycler views you inflate your final CardViews.

Here is a tutorial

Horizontal ScrollView inside Vertical RecyclerView inside Horizontal RecyclerView scrolling behavior

There are two mistakes I make.

  1. In case of ACTION_DOWN I need to record the lastX variable as well as in case of ACTION_MOVE, so that first move of dx is a proper value. This explains why the childCanScroll method doesn't work on things like ScrollView(as RecyclerView's getScrollX() always return 0).

  2. In method childCanScroll, the inner if statement should not only consider the x coordinate, but also the y coordinate. If y is not considered, when I scroll outside inner horizontal-scrollable-views, the method also return true, that make things not working properly.

After I fix these two mistakes all things are right.



Related Topics



Leave a reply



Submit