AppBarLayout 禁止滑动
有时候,有这样的需求,AppBarLayout是放在Activity中的,其中有几个Fragment需要AppBarLayout进行滑动,而另几个Fragment并不希望AppBarLayout进行滑动。
或者是CollapsingToolbarLayout布局其他的情况,也可以适用。
解决方法1
禁止AppBarLayout滑动
主要是在AppBarLayout的Behavior中,设置setDragCallback回调,将canDrag方法返回false,从而阻止滑动。
解决方法2
对于实现了NestedScrollingChild接口的View,通过继承,实现其NestedScrollingChild的方法实现,进行空实现,从而不触发Behavior事件。
比如Recyclerview,用这个Recyclerview来替代Recyclerview,即可不触发AppBarLayout的滑动。
/**
* 可以控制NestedScrollingChild2接口的Recyclerview
*
* @author EthanCo
* @since 2017/12/8
*/
public class NoNestedRecyclerView extends RecyclerView {
boolean isNestedEnable = false;
public NoNestedRecyclerView(Context context) {
super(context);
}
public NoNestedRecyclerView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public NoNestedRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public boolean isNestedEnable() {
return isNestedEnable;
}
public void setNestedEnable(boolean nestedEnable) {
isNestedEnable = nestedEnable;
}
@Override
public boolean startNestedScroll(int axes, int type) {
if (isNestedEnable) {
return super.startNestedScroll(axes, type);
} else {
return false;
}
}
@Override
public void stopNestedScroll(int type) {
if (isNestedEnable) {
super.stopNestedScroll(type);
}
}
@Override
public boolean hasNestedScrollingParent(int type) {
if (isNestedEnable) {
return super.hasNestedScrollingParent(type);
} else {
return false;
}
}
@Override
public boolean dispatchNestedScroll(int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int[] offsetInWindow, int type) {
if (isNestedEnable) {
return super.dispatchNestedScroll(dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, offsetInWindow, type);
} else {
return false;
}
}
@Override
public boolean dispatchNestedPreScroll(int dx, int dy, int[] consumed, int[] offsetInWindow, int type) {
if (isNestedEnable) {
return super.dispatchNestedPreScroll(dx, dy, consumed, offsetInWindow, type);
} else {
return false;
}
}
}