天天看點

關于6.0ScrollView嵌套RecyclerView高度不正确顯示不全問題解決辦法

針對ScrollView和RecyclerView的嵌套問題,網上有很多解決辦法.

但是我隻用一個辦法成功解決.下面記錄下來

<ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="測試文字1"
                 />

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

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


            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="測試文字2" />

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

                <android.support.v7.widget.RecyclerView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    />
            </RelativeLayout>
        </LinearLayout>
</ScrollView>
           

主要問題出在布局上,6.0需要我們在RecyclerView外層在套一層RelativeLayout.即可完美解決ScrollView嵌套RecyclerView高度問題.

另:推薦在代碼中加入

RecyclerView.setNestedScrollingEnabled(false);
           

可消除滑動時粘滞感!完畢!