欢迎大家和我分享
欢迎转载,但请保留文章原始出处:
哈哩波特大:https://www.jianshu.com/u/b7d80b891802
res:layout
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/dp_15">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
Java:activity
//解决数据加载不完的问题
recycler_view.setNestedScrollingEnabled(false);
//当知道Adapter内Item的改变不会影响RecyclerView宽高的时候,可以设置为true让RecyclerView避免重新计算大小
recycler_view.setHasFixedSize(true);
//解决数据加载完成后, 没有停留在顶部的问题
recycler_view.setFocusable(false);
方案一
嵌套一层RelativeLayout
添加属性 android:descendantFocusability="blocksDescendants"
首先该属性android:descendantFocusability的含义是:当一个view获取焦点时,定义ViewGroup和其子控件两者之间的关系。
它一共有3个属性值,它们分别是:
beforeDescendants:viewGroup会优先子类控件而获取焦点
afterDescendants:viewGroup只有当子类控件不需要获取焦点的时候才去获取焦点
blocksDescendants:viewGroup会覆盖子类控件而直接获取焦点
方案二
首先在xml布局中将你的ScrollView替换成android.support.v4.widget.NestedScrollView
,并在java代码中设置recyclerView.setNestedScrollingEnabled(false);