美文网首页Android
不可滑动LayoutManager

不可滑动LayoutManager

作者: 吃泥巴的猫 | 来源:发表于2018-12-06 15:25 被阅读6次

RecycleView不可滑动,固定高度,设置setNestedScrollingEnabled(false)后滑动时上下有滑动越界效果,需要重写LayoutManger使LayoutManager不可滑动

LinearLayoutManger:

/**
 * 不可滑动
 */
public class CustomLinearLayoutManager extends LinearLayoutManager {
    private boolean isScrollEnabled = true;

    public CustomLinearLayoutManager(Context context) {
        super(context);
    }

    public void setScrollEnabled(boolean flag) {
        this.isScrollEnabled = flag;
    }

    /**
     * 禁止滑动
     * canScrollHorizontally(禁止横向滑动)
     *
     * @return
     */
    @Override
    public boolean canScrollHorizontally() {
        return isScrollEnabled && super.canScrollVertically();
    }

    /**
     * 禁止滑动
     * canScrollVertically(禁止竖向滑动)
     *
     * @return
     */
    @Override
    public boolean canScrollVertically() {
        return isScrollEnabled && super.canScrollVertically();
    }
}

GridLayoutManager:

/**
 * 不可滑动
 */
public class CustomGridLayoutManager extends GridLayoutManager {
    private boolean isScrollEnabled = true;

    public CustomGridLayoutManager(Context context, int spanCount) {
        super(context, spanCount);
    }

    public void setScrollEnabled(boolean flag) {
        this.isScrollEnabled = flag;
    }

    /**
     * 禁止滑动
     * canScrollHorizontally(禁止横向滑动)
     *
     * @return
     */
    @Override
    public boolean canScrollHorizontally() {
        return isScrollEnabled && super.canScrollVertically();
    }

    /**
     * 禁止滑动
     * canScrollVertically(禁止竖向滑动)
     *
     * @return
     */
    @Override
    public boolean canScrollVertically() {
        return isScrollEnabled && super.canScrollVertically();
    }
}

相关文章

网友评论

    本文标题:不可滑动LayoutManager

    本文链接:https://www.haomeiwen.com/subject/kpsocqtx.html