解决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会覆盖子类控件而直接得到焦点
复制代码
相关文章
相关标签/搜索