Nested Scrolling support
I tried to use this library with a RecyclerView inside of a CoordinatorLayout that also contains a AppBarLayout with a Toolbar that uses app:layout_scrollFlags="scroll|enterAlways|snap":
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
android:id="@+id/coordinator_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlways|snap"/>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/store_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
<com.futuremind.recyclerviewfastscroll.FastScroller
android:id="@+id/fastscroll"
android:orientation="vertical"
android:layout_gravity="right"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
android:layout_marginBottom="?attr/actionBarSize"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</android.support.design.widget.CoordinatorLayout>
This has the following problems:
- the
FastScrollerisn't collapsing/expanding theAppBarLayout - this prevents the
FastScrollerto scroll to the last entry in the list if theAppBarLayoutis expanded - parts of the
FastScrollerare not visible (I quick fixed this problem with usingmarginTop/marginBottomfor theFastScroller)
It seems that RecyclerView doesn't handle Nested Scrolling if you scroll with scrollToPosition. Sadly I couldn't find any proper documentation how Nested Scrolling is supposed to work. So I can't say if this is a bug in RecyclerView or this is supposed to be handled by the scroll initiator. But I think it is possible to handle it with:
-
ViewCompat.startNestedScroll -
ViewCompat.dispatchNestedPreScroll -
RecyclerView.scrollBy -
ViewCompat.dispatchNestedScroll -
ViewCompat.stopNestedScroll
This snippet demonstrates a way that let's the RecyclerView scroll by 300px while also collapsing the AppBarLayout. However I don't know this is the proper way to do it:
if (ViewCompat.isNestedScrollingEnabled(recyclerView) && ViewCompat.startNestedScroll(recyclerView, ViewCompat.SCROLL_AXIS_VERTICAL)) {
int[] consumed = new int[2];
ViewCompat.dispatchNestedPreScroll(recyclerView, 0, 300, consumed, null);
int unsconumed = 300 - consumed[1];
recyclerView.scrollBy(0, unsconumed);
ViewCompat.dispatchNestedScroll(recyclerView, consumed[0], consumed[1], 0, unsconumed, null);
ViewCompat.stopNestedScroll(recyclerView);
}
I'm not sure if it is possible to fix this problem with the current approach in this lib to scroll with scrollToPosition.
This is an interesting problem. I think that what you wrote about nested scrolling makes a perfect starting point. One should probably look into how RecyclerView handles various scroll methods internally and move to another method if scrollToPosition doesn't work as expected. Filing a RecyclerView bug in Google would tell us if this is intended or not. Anyway this has to be fixed. The problem is that I currently have no time for maintaining the lib, so if you wanted to contribute, investigate this issue and post a PR, that would be great.
Also, if you decide to file a bug to Google, reference it here, please.
https://code.google.com/p/android/issues/detail?id=221455