安卓6.0以上ScrollView嵌套RecyclerView条目无法全部显示

安卓6.0以上ScrollView嵌套RecyclerView条目无法全部显示

一般情况下,应当避免ScrollView嵌套RecyclerView, 但有时候就需进行嵌套, 以前嵌套listView会导致条目显示不完整, 如果嵌套RecyclerView在6.0以下系统, 初了滑动不太流畅外其他功能全部正常, recyclerView.setNestedScrollingEnabled(false) 可以解决滑动不流畅.

但是如果是6.0以上系统, 列表不能全部显示, 然后就找了下解决方法, 是在当前recycleView控件上在加一个父布局RelativeLayout 这样就可以解决, 添加其他父布局好像是不可以. 这是一个单独的layout,include_punch_field.xml;

<RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/punch_stub_recycle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </RelativeLayout>

这样解决虽然干脆利落, 但是又多了一层布局的嵌套, 而且还是RelativeLayout, 如果在6.0以下系统, 显然没有必要, 所以我就做了下版本判断, 如果系统版本>=23就使用带有RelativeLayout为父布局的recycleView, 否则的话就不用它.

使用ViewStub, 不使用的时候是不占用布局嵌套层次的, 所以当使用的时候在加载.

 <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

          <ViewStub
            android:id="@+id/punch_field_stub"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout="@layout/include_punch_field"//RelativeLayout父布局内容
            />

         <android.support.v7.widget.RecyclerView
                android:id="@+id/punch_field_recycle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

    </FrameLayout>

加载ViewStub 返回一个recycleView, 如果是>=6.0系统就加载viewStub, 否则的话, 还是用不带有RelativeLayout父布局容器的recycleView, ViewStub 只能加载一次, 多次调用会报空指针.

public RecyclerView getRecycleView() {
        if (Build.VERSION.SDK_INT >= 23) {
            ViewStub punch_field_stub = (ViewStub) findViewById(R.id.punch_field_stub);
            punch_field_stub.inflate();//加载
            return (RecyclerView) findViewById(R.id.punch_stub_recycle);
        }
        return (RecyclerView) findViewById(R.id.punch_field_recycle);
    }
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值