今天在做项目的时候用到了CoordinatorLayout + AppBarLayout + CollapsingToolbarLayout + Toolbar + TabLayout + NestedScrollView,网上有很多,这种控价搭配使用的教程,这里就不多描述了。
然后遇到了个大难题,网上找了很多关于AppBarLayout的帖子,不过大本分都是讲怎么用的,都解决不了我的问题,黄天不负有心人,终于找到了一个有用的技术文档。
先说一下我的使用情况吧:
我的头部最上方是AppBarLayout包裹的AViewpager,里面加载AFragment,中间是TabLayout,下部是ScrollView包裹着BViewpager,里面加载的BFragment,BFragment中最外层是一个ScrollView,里面嵌套了三个RecyclerView。
大致效果是这个样子的,(请原谅我的美术功底)凑合着看一下吧:
现在想在AViewpager滑动到第二页的时候,就禁止AppBarLayout的折叠效果。
具体解决办法,监听AViewPager的滑动事件,在监听中动态去设置AppBarLayout的LayoutParam
AViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
int i0 = AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL;
int i1 = AppBarLayout.LayoutParams.SCROLL_FLAG_EXIT_UNTIL_COLLAPSED;
View appBarChildAt = mAppBarLayout.getChildAt(0);
AppBarLayout.LayoutParams appBarParams = (AppBarLayout.LayoutParams) appBarChildAt.getLayoutParams();
if (position == 0) {
appBarParams.setScrollFlags( i0 | i1);// 重置折叠效果
} else {
appBarParams.setScrollFlags(0);//这个加了之后不可滑动
}
appBarChildAt.setLayoutParams(appBarParams);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
在网上大神的帖子中找到了关于setScrollFlags的参数说明: