How to set initial height of CollapsingToolbar without flickering?

痴心易碎 提交于 2021-02-11 12:30:03

问题


I am using a CollapsingToolbar with a NestedScrollView in my layout and I want to display it with half its height when the activity is initially shown, i.e. I want to simulate a scrolling event.

I achieved this by dispatching a nested scroll in my activity's onCreate() method.

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...

    NestedScrollView nestedScrollView = findViewById(R.id.scroll_view);
    nestedScrollView.post(() -> {
        int appBarHeight = findViewById(R.id.app_bar).getHeight()/2;
        nestedScrollView.startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL);
        nestedScrollView.dispatchNestedPreScroll(0, appBarHeight, null, null);
        nestedScrollView.dispatchNestedScroll(0, 0, 0, 0, new int[]{0, -appBarHeight});
    });

    ...
}

The problem is that this solution produces a visible flickering because the programmatic scroll is added to the message queue and executed after the initial drawing.

Doing the scroll directly in onCreate() does not work and neither does nestedScrollView.scrollTo(x,y) since this does not dispatch the scroll event to the toolbar (that means that the toolbar stays at its declared height).

Is there a way to simulate the scroll earlier in the activity lifecycle so that I do not get the flickering? Or a completely other way how I can set the CollapsingToolbar's initial height?

来源:https://stackoverflow.com/questions/61938816/how-to-set-initial-height-of-collapsingtoolbar-without-flickering

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!