10

I followed this tutorial to implement the behaviors for both hiding the toolbar and the FAB when scrolled: https://mzgreen.github.io/2015/06/23/How-to-hideshow-Toolbar-when-list-is-scrolling(part3)/

I have pasted a demo of what the behavior looks like below.

scrolling with fab

Now instead of those individual items within the recyclerview in the tabs holding just a textView, I have coded it so that they hold a picture (ImageView) and below it, a recyclerview showing a list of items.

Hence, there is an outer recyclerview that contains a list of inner recyclerviews.

The inner recyclerview does not scroll - I disabled it by following the answer in this thread by overriding the method canScrollVertically(): Disable Scrolling in child Recyclerview android. I also tried enabling the scrolling for the inner recyclerview, but I experienced the same problem.

The outer recyclerview does scroll and has the behavior that shows/hides the toolbar and the FAB.

When I scroll by holding onto the picture (ImageView), the app behavior works perfectly fine, showing and hiding the toolbar and FAB. However, when I have my finger on the inner recyclerview to scroll, the outer recyclerview scrolls and the list moves up and down, but the behavior of show/hiding the toolbar and FAB is never activated.

I have a feeling that this is because the inner recyclerview had intercepted the scroll and the outer recyclerview did not get the scroll event to activate the behavior.

Does anyone know how to make sure the outer recyclerview also gets the scroll event so that the behavior works?

Community
  • 1
  • 1
Simon
  • 19,658
  • 27
  • 149
  • 217

3 Answers3

8

Hank Moody comment actually lead me to the correct answer - thanks Hank!

This is how I solved my problem:

  1. Create a 'Scroll Through' recyclerview where the parent will receive all the scroll events of the child by doing this:

    public class ScrollThroughRecyclerView extends RecyclerView {
        public ScrollThroughRecyclerView(Context context) {
            super(context);
        }
    
        public ScrollThroughRecyclerView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public ScrollThroughRecyclerView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        @Override
        public boolean dispatchTouchEvent(MotionEvent ev){
            //true - block scrolling of child and allow scrolling for parent recycler
            return true;
        }
    }
    
  2. Use this custom recyclerview in your xml or in your java code and the scroll events will be passed correctly to your parent recyclerview which will activate the app scrollbehavior.

Pablo
  • 652
  • 6
  • 21
Simon
  • 19,658
  • 27
  • 149
  • 217
3

if you want put one vertical recycler in another vertical you must set fixed height for child recycler view, or try to overwrite this method

@Override
public boolean dispatchTouchEvent(MotionEvent ev){
    //false - block scrolling of parent recycler and allow scrolling for child
    return false;
}
Hank Moody
  • 354
  • 2
  • 15
  • I don't think this would work as currently I'm trying to pass the scroll event to the parent and I'm successful as the outer recyclerview is scrolling. However, the inner recyclerview is not activating the scroll behavior to hide or show the toolbar / FAB. – Simon Aug 29 '15 at 22:17
  • Thanks - this actually lead me to the correct answer which I have posted below. – Simon Aug 30 '15 at 08:20
0

Have you tried using the following in the xml of the inner recyclerview?

app:layout_behavior="@string/appbar_scrolling_view_behavior"

EDIT: assuming you are using CoordinatorLayout.

dejavu89
  • 754
  • 1
  • 7
  • 17
  • I'm not sure this would work as, if you see the demo, I have an activity with tabs where each tab is displaying a fragment. The CoordinatorLayout is on the activity layout xml and not in the fragment layout xml, hence that is why the viewpager in the activity has the app:layout_behavior associated with it. In the fragment layout xml, which contains the outer recyclerview and in the inner recyclerview layout xml, there is no coordinatorlayout, so I don't think putting a behavior in either would have any effect. – Simon Aug 29 '15 at 22:13