解決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

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和其子控件二者之間的關係。佈局

屬性的值有三種:spa

beforeDescendants:viewgroup會優先其子類控件而獲取到焦點

afterDescendants:viewgroup只有當其子類控件不須要獲取焦點時才獲取焦點

blocksDescendants:viewgroup會覆蓋子類控件而直接得到焦點
複製代碼
相關文章
相關標籤/搜索