Programmatically show Toolbar after hidden by scrolling (Android Design Library)

一个人想着一个人 提交于 2019-12-01 03:41:33

You can do it by accessing to the AppBarLayout that contains your Toolbar

Here is an example:

if (mToolbar.getParent() instanceof AppBarLayout){
  ((AppBarLayout)mToolbar.getParent()).setExpanded(true,true);
}

setExpanded(expand,animation) will do the work. You can also have a a reference to the AppBarLayout instead of call the getParent from the toolbar.

U could either do toolbar.transitionY(int y); on the on scroll method or use visibility gone and u have to add an header in the list view or recycler view with the size of the toolbar. So that the whole list still shows

You need to put header to the RecyclerView to the height of the AppBarLayout. I.e at position 0 of the RecyclerView you need to add the header and then the rest of the elements.

If you want to forcefully show the Toolbar, actually the AppBarLayout with offsetting top and bottom of the AppBarLayout dependent views (it is called Behavoir) . You need to keep reference of height of the AppBarLayout, as we know that height is the distance between top and bottom of view Rect.

Assuming that your AppBarLayout hold only a Toolbar:

int mAppBarLayoutHeight = mAppBarLayout.getBottom() - mAppBarLayout.getTop(); //initial, normal height

 private void showToolbar(){
        if(this.mAnimator == null) {
            this.mAnimator = new ValueAnimator();
            this.mAnimator.setInterpolator(new DecelerateInterpolator());
            this.mAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                 int animatedOffset = (int) animation.getAnimatedValue();
                 mToolbar.offsetTopBottom(animatedOffset/2);
                }
            });
        } else {
            this.mAnimator.cancel();
        }

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