AppBarLayout 禁止滑动

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;
        }
    }
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

氦客

你的鼓励是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值