1

How can I create a half-expanded toolbar? Something like that WhatsApp profile page.

I've tried scrollBy and scrollTo, but they don't seem to work.

Even dispatchNestedScroll, onNesterScroll and onNestedPreScroll don't seem to work.

jkdev
  • 9,037
  • 14
  • 52
  • 75
Ahmad Sattout
  • 1,079
  • 7
  • 22

2 Answers2

1

I managed to solve this problem by adapting this answer: https://stackoverflow.com/a/34920495/5369519 and using the following code:

nestedScrollView.post(() -> {
        int appBarHeight = appBar.getHeight()/2;
        nestedScrollView.startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL);
        nestedScrollView.dispatchNestedPreScroll(0, appBarHeight, null, null);
        nestedScrollView.dispatchNestedScroll(0, 0, 0, 0, new int[]{0, -appBarHeight});
    });

This simulates scrolling halfway down. Just using NestedScrollView.scrollTo() is not enough to trigger the scroll events for the parent.

flauschtrud
  • 249
  • 2
  • 13
  • 1
    I already found an answer long ago. But this answer sums up what I did. I made the nestedScrollView scroll 50$ the height of the appbar, making the appbar half collapsed – Ahmad Sattout May 21 '20 at 13:08
  • Did you also encounter problems with a flickering UI? I added a question for this problem here: https://stackoverflow.com/q/61938816/5369519 – flauschtrud May 21 '20 at 20:55
  • No not at all, it was a fine implementation. – Ahmad Sattout May 22 '20 at 09:43
0

To create the collapsing toolbar, CollapsingToolbarLayout integrates with AppBarLayout, CoordinatorLayout, Toolbar, and a scrollable content view, such as RecyclerView

Ajeet Yadav
  • 655
  • 1
  • 9
  • 27