问题
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