Show toolbar when view pager is swiped. [CoordinatorLayout]

前端 未结 2 1196
长情又很酷
长情又很酷 2021-01-22 19:49

in my app I am using a viewpager with 3 fragments. In two of those I have recuclerviews. I took advatngage of the new Coordinator layout and made my toolbar hides/shows when scr

2条回答
  •  野的像风
    2021-01-22 20:06

    I was able to achieve this by referencing the AppBarLayout and calling the "setExtended" method like that:

    viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                //Make sure that the fab is visible when scrolling the pages...
                MainActivity.mFloatingActionsMenu.animate()
                        .setDuration(150)
                        .translationY(0);
                //show the toolbar
                expandToolbar();
    
            }
    
            @Override
            public void onPageSelected(int position) {
    
            }
    
            @Override
            public void onPageScrollStateChanged(int state) {
    
            }
        });
    

    my expandToolbar method:

    public void expandToolbar(){
        //setExpanded(boolean expanded, boolean animate)
        AppBarLayout appBarLayout = (AppBarLayout)findViewById(R.id.appBarLayouy);
        appBarLayout.setExpanded(true, true);
    }
    

提交回复
热议问题