AppBarLayout with recyclerView in nested fragment

前端 未结 3 1372
长情又很酷
长情又很酷 2020-12-31 12:58

As images are always better than words, I present to you my current layout.

The toolbar/tabs is in an activity.xml with a viewPager, and the recycle

3条回答
  •  不知归路
    2020-12-31 13:09

    You can do this by implementing ViewPager.OnPageChangeListener and enabling/disabling AppBarLayout.LayoutParams scrolling flags.

    Here is a sample code:

            viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    
            }
    
            @Override
            public void onPageSelected(int position) {
                if (position == 0) {
                    //turn on scrolling
                    AppBarLayout.LayoutParams toolbarLayoutParams = (AppBarLayout.LayoutParams) mToolbar.getLayoutParams();
                    toolbarLayoutParams.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS);
                    mToolbar.setLayoutParams(toolbarLayoutParams);
    
                    CoordinatorLayout.LayoutParams appBarLayoutParams = (CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams();
                    appBarLayoutParams.setBehavior(new AppBarLayout.Behavior());
                    appBarLayout.setLayoutParams(appBarLayoutParams);
                } else {
                    //turn off scrolling
                    AppBarLayout.LayoutParams toolbarLayoutParams = (AppBarLayout.LayoutParams) mToolbar.getLayoutParams();
                    toolbarLayoutParams.setScrollFlags(0);
                    mToolbar.setLayoutParams(toolbarLayoutParams);
    
                    CoordinatorLayout.LayoutParams appBarLayoutParams = (CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams();
                    appBarLayoutParams.setBehavior(null);
                    appBarLayout.setLayoutParams(appBarLayoutParams);
                }
            }
    
            @Override
            public void onPageScrollStateChanged(int state) {
    
            }
        });
    

    But it does not seem like a good UX pattern. It will be confusing for users.

提交回复
热议问题