AppBarLayout 禁止滑动

来源:互联网 发布:php时间戳转换年月日 编辑:程序博客网 时间:2024/06/09 14:25

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;        }    }}
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 六个月宝宝着凉拉肚子怎么办 六个月婴儿着凉拉肚子怎么办 怀孕六个月着凉拉肚子怎么办 六个月的婴儿消化不好怎么办 六个月婴儿不好好吃奶怎么办 六个月婴儿消化不好怎么办 六个月婴儿肠胃不好怎么办 受了惊吓后睡眠不好怎么办 宝宝夏季吃饭不爱带围嘴怎么办 3岁宝宝爱看电视怎么办 新生儿晚上哭闹不睡觉怎么办 档案丢了怎么办失业证 三岁宝宝不合群怎么办 阴唇手术半月了疤痕增生怎么办 宝宝小阴唇黏连怎么办 6个月宝宝阴唇粘连怎么办? 高一孩子不爱上学怎么办 3岁宝宝外阴发红怎么办 40爸妈离婚我该怎么办 小儿吃糖卡住了怎么办 额头撞墙上肿了怎么办 宝宝额头撞肿了怎么办 小孩额头撞肿了怎么办 宝宝撞到额头肿了怎么办 小孩子上一年级语文很差了怎么办? 楼下说小孩太吵怎么办 托班的小孩太吵怎么办 宝宝两岁只会简单的词怎么办 我儿子的视力低怎么办 小孩子课文看书都不会读怎么办 宝宝衣服买小了怎么办 拉拉裤腰围小了怎么办 一周九个月宝宝发烧怎么办 8个月的婴儿37.7怎么办 宝宝发烧37度3怎么办 2岁宝宝发烧37度怎么办 3个月新生儿发烧怎么办 生完孩子肚子松弛怎么办 6个月婴儿高烧怎么办 7个月婴儿高烧怎么办 11个月婴儿高烧怎么办