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

前端 未结 3 705
遥遥无期
遥遥无期 2021-01-04 08:02

I have the following layout: a drawer, with the main content view having a AppBarLayout, RecyclerView and a TextView. When I scroll the recycler, the toolbar is correctly hi

3条回答
  •  情歌与酒
    2021-01-04 08:33

    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();
        }
    

提交回复
热议问题