天天看點

關于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

轉載于:https://www.cnblogs.com/widgetbox/p/10445997.html