关于ScrollView嵌套RecyclerView出现item显示不全的问题

最近使用ScrollView时,发现里面嵌套Listview显示不全,试过重写Listview的onMeasure(),并没有起作用。然后将ListView换成RecyclerView后,高度还是显示不全面。

查资料,有以下几种解决办法:

1、在RecyclerView最外面嵌套一层布局RelativeLayout,网上有人说需要添加属性android:descendantFocusability="blocksDescendants"。但是我发现滑动的时候界面有卡顿。

  这是ScrollView和RecyclerView嵌套滑动导致的卡顿,所以需将RecyclerView.setNestedScrollingEnabled(false)。这样就可以了。android:descendantFocusability这个属性有无均可。

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <android.support.v7.widget.RecyclerView
                android:id="@+id/rv_template"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="10dp"
                android:paddingBottom="24dp"
                android:scrollbars="none" />
        </RelativeLayout>

    recyclerview.setNestedScrollingEnabled(false);

 

2、这个问题只在23版本以上才会出现,23版本是android 6.0版本,所以当我们targetSdkVersion小于或者等于23的时候(也就是我们兼容到23版本)是没有问题的,一但兼容到23版本以上就会出现这个问题。

  所以可以将app的build.gradle的targetSdkVersion 和 compileSdkVersion设置成小于23,这个问题就可以顺利解决,但是现在市场上8.0已经非常普遍了,9.0手机也已经出了,只兼容到6.0版本显然不太友好,建议还是用第一种方法。

 

  By LiYing

posted @ 2019-02-27 19:11  WidgetBox  阅读(2827)  评论(0编辑  收藏  举报