Skip to content Skip to sidebar Skip to footer

How To Use Collapsing Toolbar With A Listview Instead Of A Recycler View

Does anyone know how to implement the Collapsing Toolbar using A listview instead of a recycler view?

Solution 1:

To make it woks you should to:

  1. Implement NestedScrollingChild in your custom ListView implementation.

  2. Add field private final NestedScrollingChildHelper mScrollingChildHelper; and init it in constructors

  3. Delegate to it methods from NestedScrollingChild

  4. Invoke setNestedScrollingEnabled(true); after mScrollingChildHelper initialization

Here is my list view implementation for example:

publicclassNestedScrollingListViewextendsListViewimplementsNestedScrollingChild {

    privatefinal NestedScrollingChildHelper mScrollingChildHelper;

    publicNestedScrollingListView(Context context) {
       super(context);
       mScrollingChildHelper = newNestedScrollingChildHelper(this);
       setNestedScrollingEnabled(true);
    }

    publicNestedScrollingListView(Context context, AttributeSet attrs) {
       super(context, attrs);
       mScrollingChildHelper = newNestedScrollingChildHelper(this);
       setNestedScrollingEnabled(true);
    }

    @OverridepublicvoidsetNestedScrollingEnabled(boolean enabled) {
       mScrollingChildHelper.setNestedScrollingEnabled(enabled);
    }

    @OverridepublicbooleanisNestedScrollingEnabled() {
       return mScrollingChildHelper.isNestedScrollingEnabled();
    }

    @OverridepublicbooleanstartNestedScroll(int axes) {
       return mScrollingChildHelper.startNestedScroll(axes);
    }

    @OverridepublicvoidstopNestedScroll() {
        mScrollingChildHelper.stopNestedScroll();
    }

    @OverridepublicbooleanhasNestedScrollingParent() {
        return mScrollingChildHelper.hasNestedScrollingParent();
    }

    @OverridepublicbooleandispatchNestedScroll(int dxConsumed, int dyConsumed, int dxUnconsumed,
                                    int dyUnconsumed, int[] offsetInWindow) {
        return mScrollingChildHelper.dispatchNestedScroll(dxConsumed, dyConsumed,
            dxUnconsumed, dyUnconsumed, offsetInWindow);
    }

    @OverridepublicbooleandispatchNestedPreScroll(int dx, int dy, int[] consumed, int[] offsetInWindow) {
        return mScrollingChildHelper.dispatchNestedPreScroll(dx, dy, consumed, offsetInWindow);
    }

    @OverridepublicbooleandispatchNestedFling(float velocityX, float velocityY, boolean consumed) {
        return mScrollingChildHelper.dispatchNestedFling(velocityX, velocityY, consumed);
    }

    @OverridepublicbooleandispatchNestedPreFling(float velocityX, float velocityY) {
        return mScrollingChildHelper.dispatchNestedPreFling(velocityX, velocityY);
    }
}

Solution 2:

Only add this code to your project. It only work in Lollipop devices and later.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    listView.setNestedScrollingEnabled(true);
    listView.startNestedScroll(View.OVER_SCROLL_ALWAYS);
}

Solution 3:

If you want to make nested scrolling work on pre-Lollipop devices, which you probably do, you have to use corresponding utility classes from the Support library. First you have to replace you ScrollView with NestedScrollView. The latter implements both NestedScrollingParent and NestedScrollingChild so it can be used as a parent or a child scroll container.

But ListView doesn't support nested scrolling, therefore you need to subclass it and implement NestedScrollingChild. Fortunately, the Support library provides NestedScrollingChildHelper class, so you just have to create an instance of this class and call its methods from the corresponding methods of your view class.

Post a Comment for "How To Use Collapsing Toolbar With A Listview Instead Of A Recycler View"