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 RecyclerView
s
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 RecyclerView
s 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 RecyclerView
s 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 RecyclerView
s 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.
In case of
ACTION_DOWN
I need to record thelastX
variable as well as in case ofACTION_MOVE
, so that first move ofdx
is a proper value. This explains why thechildCanScroll
method doesn't work on things likeScrollView
(asRecyclerView
'sgetScrollX()
always return 0).In method
childCanScroll
, the innerif
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 returntrue
, that make things not working properly.
After I fix these two mistakes all things are right.
Related Topics
Eclipse Indexer Errors When Using Stl with Android Ndk
How to Create a Looper Thread, Then Send It a Message Immediately
How to Embed a Youtube Clip in a Webview on Android
Android Add Image to Webview from a Drawable
Disable Android Browser's Input Overlays
Extract Notification Text from Parcelable, Contentview or Contentintent
Shared Preferences Reset Data When App Is Force Closed or Device Is Restarted
Using Multiple Text Colors in Android's Textview [ HTML.Fromhtml() ]
Textview with Background Color and Line Spacing
Android: Change Tab Text Color Programmatically
Do Android Support Multiple Languages
Set Rowspan or Colspan of a Child of a Gridlayout Programmatically
How to Keep The Oauth Consumer Secret Safe, and How to React When It's Compromised
Using Android Google Maps V2 with Custom View or Imageview
Flutter: How to Fix "A Renderflex Overflowed by Pixels " Error
Onitemclicklistener and Onclicklistener Not Working for Listview
How to Create a Service Which Continuously Monitors App Usage Information