我有一个工具栏和一个TabLayout以及一个包含在CollapsingToolbarLayout中的图像.这个gif几乎总结了一下:
https://raw.githubusercontent.com/vitovalov/TabbedCoordinatorLayout/master/art/demo.gif
这是XML:
我想要实现的是,例如在Tab 2上,我不想让用户能够看到图像.因此,即使他试图将AppBarLayout拉下来,他也不应该在任何情况下看到图像.
调用appBarLayout.setExpanded(false);
显示选项卡2会隐藏图像并将所有内容折叠回来.虽然当你按下AppBarLayout并向下滑动时,你可以恢复图像.情况并非如此.
我该如何防止这种行为?
我正在使用支持库的v23.1.1.对于图书馆的第22版,已经存在这个问题.但是,解决方法不再适用于版本23.
我找到了一招!
当我希望我的appbar保持折叠时:
public void lockAppBarClosed() { mAppBarLayout.setExpanded(false, false); mAppBarLayout.setActivated(false); CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams)mAppBarLayout.getLayoutParams(); lp.height = (int) getResources().getDimension(R.dimen.toolbar_height); }
当我希望我的appbar再次展开和滚动时
public void unlockAppBarOpen() { mAppBarLayout.setExpanded(true, false); mAppBarLayout.setActivated(true); CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams)mAppBarLayout.getLayoutParams(); lp.height = (int) getResources().getDimension(R.dimen.toolbar_expand_height); }
希望它能帮到你!
Anne-Claire提供的技巧有效,但你最终失去了动画.我所做的是创建一个扩展AppBarLayout.Behavior
类的自定义行为,允许我在每次选择时启用/禁用滚动.
public class CustomAppBarLayoutBehavior extends AppBarLayout.Behavior { private boolean shouldScroll = false; public CustomAppBarLayoutBehavior(Context context, AttributeSet attrs) { super(context, attrs); } @Override public boolean onStartNestedScroll(CoordinatorLayout parent, AppBarLayout child, View directTargetChild, View target, int nestedScrollAxes, int type) { return shouldScroll; } @Override public boolean onTouchEvent(CoordinatorLayout parent, AppBarLayout child, MotionEvent ev) { if(shouldScroll){ return super.onTouchEvent(parent, child, ev); }else{ return false; } } public void setScrollBehavior(boolean shouldScroll){ this.shouldScroll = shouldScroll; } public boolean isShouldScroll(){ return shouldScroll; } }
然后,您应该将此行为应用于xml布局中的AppBarLayout
然后你可以通过以下方式随意打开和关闭它:
CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) mAppBarLayout.getLayoutParams(); ((CustomAppBarLayoutBehavior)layoutParams.getBehavior()).setScrollBehavior(true);
要么
CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) mAppBarLayout.getLayoutParams(); ((CustomAppBarLayoutBehavior)layoutParams.getBehavior()).setScrollBehavior(false);
根据“ Bilthon”(此处)的答案,我认为在Kotlin中,此解决方案要短得多:
//allows to block scrolling of AppBarLayout /sf/ask/17360801/ class BlockableAppBarLayoutBehavior(context: Context, attrs: AttributeSet) : AppBarLayout.Behavior(context, attrs) { var isShouldScroll = false override fun onStartNestedScroll(parent: CoordinatorLayout, child: AppBarLayout, directTargetChild: View, target: View, nestedScrollAxes: Int, type: Int) = isShouldScroll override fun onTouchEvent(parent: CoordinatorLayout, child: AppBarLayout, ev: MotionEvent) = isShouldScroll && super.onTouchEvent(parent, child, ev) }
用法:
滚动时禁用更改其大小:
((app_bar.layoutParams as CoordinatorLayout.LayoutParams).behavior as BlockableAppBarLayoutBehavior).isShouldScroll = false
并启用:
((app_bar.layoutParams as CoordinatorLayout.LayoutParams).behavior as BlockableAppBarLayoutBehavior).isShouldScroll = false
如果您有RecyclerView,并且希望也阻止它滚动,则也可以使用我发现的此解决方案。