天天看點

ScrollView嵌套RecyclerView 在6.0以及以上版本顯示不完全

如果targetSdkVersion = 23,      
RecyclerView的高度為wrap_content。      
那麼清單會顯示不完全,比如本來5條資料,隻顯示2條出來。      

如果如果targetSdkVersion = 22,會正常顯示,不會有這個問題。

<ScrollView
    android:id="@+id/sv_credit"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:overScrollMode="never"
    android:scrollbars="none">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        android:orientation="vertical">


        <android.support.v7.widget.RecyclerView
            android:id="@+id/rvCreditPro"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:minHeight="150dp"
            android:overScrollMode="never"
            android:scrollbars="vertical" />
    </LinearLayout>
</ScrollView>      

解決辦法如下:

在RecyclerView 外面套一層RelativeLayout就可以了。

<ScrollView
    android:id="@+id/sv_credit"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:overScrollMode="never"
    android:scrollbars="none">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        android:orientation="vertical">

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

            <!--RelativeLayout 包裹在RecyclerView外面,是為了-->
            <!--解決targetSdkVersion >= 23 時的bug,否則ScrollView-->
            <!--嵌套RecyclerView ,會展示不完全。重寫onMeasure也沒用的。-->

            <android.support.v7.widget.RecyclerView
                android:id="@+id/rvCreditPro"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:minHeight="150dp"
                android:overScrollMode="never"
                android:scrollbars="vertical" />
        </RelativeLayout>

    </LinearLayout>
</ScrollView>      

繼續閱讀