在Android6.0以下的系统上,使用Scrollview嵌套RecycleView时显示是正常的,而在6.0系统上,却出现了高度显示不正常的问题.为此我在网上查了好几种方法.
1.重写RecycleView,我是用这种方法解决了问题
/**
* 适配6.0 scrollv嵌套recyclev显示不全
*/
public class InnerRecycleView extends RecyclerView {
public InnerRecycleView(Context context) {
super(context);
}
public InnerRecycleView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
setNestedScrollingEnabled(false);
}
}
2.在RecycleView外面包含一层RelativeLayout布局
这个方法是在stackoverflow上找到的:
http://stackoverflow.com/questions/27083091/recyclerview-inside-scrollview-is-not-working
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_goods"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
这里要注意的关键属性是:
android:descendantFocusability="blocksDescendants"
Defines the relationship between the ViewGroup and its descendants when looking for a View to take focus.Must be one of the following constant values.
api.jpg
该属性是当view获取焦点时,定义viewGroup和其子控件两者之间的关系.
属性的值有三种:
beforeDescendants: viewgroup会优先其子类控件而获取到焦点
afterDescendants: viewgroup只有当其子类控件不需要获取焦点时才获取焦点
blocksDescendants: viewgroup会覆盖子类控件而直接获得焦点
网友评论