解決RecyclerView在ScrollView中會置頂它上面的控制元件
通常情況下,需要在RecyclerView上加布局可以通過自己定義adapter然後add header來新增。
但是也還有一種方法來實現,那就是用ScrollView來巢狀,比如這樣的程式碼:
<ScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="30dp"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="16dp" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="16dp" /> <android.support.v7.widget.RecyclerView android:id="@+id/rv_homework" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> </ScrollView>
可是當這樣寫的時候,你會發現,RecyclerView會把它上面的檢視頂出螢幕,然後要手動向下滑才能看到,出現這個問題是因為RecyclerView搶了焦點,我們只需要在ScrollView 的唯一子佈局下面加上這麼一句
android:descendantFocusability="blocksDescendants" <LinearLayout android:descendantFocusability="blocksDendants" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="30dp">
android:descendantFocusability 該屬性是當一個為view獲取焦點時,定義viewGroup和其子控制元件兩者之間的關係。
屬性的值有三種:
afterDescendants:viewgroup只有當其子類控制元件不需要獲取焦點時才獲取焦點
blocksDescendants:viewgroup會覆蓋子類控制元件而直接獲得焦點