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

前端 未结 3 681
遥遥无期
遥遥无期 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:15

    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.

    0 讨论(0)
  • 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();
        }
    
    0 讨论(0)
  • 2021-01-04 08:35

    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

    0 讨论(0)
提交回复
热议问题