Android 6.0使用Scrollview嵌套RecycleView时显示不全问题

在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会覆盖子类控件而直接获得焦点

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,852评论 25 709
  • 出现嵌套问题,主要是事件的分发机制及事件冲突,以下列举各种冲突及相关的解决办法。 一、ScrollView嵌套Li...
    黄海佳阅读 1,160评论 6 11
  • EnglishVersion ->_->:https://github.com/jiang111/awesome-...
    聂顺阅读 1,257评论 0 4
  • 沧海生红日, 云霞动远空。 水光波淼淼, 松柏色盈盈。 一梦关山远, 只身渭水行。 扶栏凝视久, 埋没万方雄。
    李缓之阅读 506评论 26 63
  • kabin阅读 163评论 0 1