[android]滑动冲突 ScrollView+ViewPager+RecyclerView

本文提供了一种解决ScrollView与ViewPager结合使用时显示问题的方法,并介绍了一种自定义的LinearLayoutManager来解决ViewPager中Fragment内RecyclerView滑动卡顿的问题。

都是在网上找的会附赠网址,侵权即删

第一个是先解决ScrollView+ViewPager不显示问题 网上的其他解决方案俺就不解释了 不好用就是不好用

这个解决方案是重新测量高度 去自适应 嵌套进普通ScrollView即可

转载地址https://blog.youkuaiyun.com/g401946949/article/details/53187319

<com.guanyueyun.mylook.view.CustomViewPager
            android:id="@+id/view_home_main_viewpager"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/white" />
public class CustomViewPager extends ViewPager {

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

    public CustomViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int height = 0;
        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
            int h = child.getMeasuredHeight();
            if (h > height)height = h;
        }

        heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

然后解决viewpager里的fragment里的RecyclerView

我写的商品详细,所以我不需要点击事件,但是他滑动到这里一定会卡,没有滑动感,所以我屏蔽了RecyclerView的滑动

若有其他解决办法也可留言,我只是针对我自己的项目写出对应解决办法

转载https://blog.youkuaiyun.com/lengqi0101/article/details/52874762

public class CustomLinearLayoutManager extends LinearLayoutManager {
    private boolean isScrollEnabled = true;

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

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

    @Override
    public boolean canScrollVertically() {
        //Similarly you can customize "canScrollHorizontally()" for managing horizontal scroll
        return isScrollEnabled && super.canScrollVertically();
    }
}
  CustomLinearLayoutManager linearLayoutManager = new CustomLinearLayoutManager(mContext);
        linearLayoutManager.setScrollEnabled(false);
        mDevicesRV.setLayoutManager(linearLayoutManager);

亲测可用

然后下一章单说这个问题。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值