美文网首页Android
[转]Android6.0以上关于RecyclerView显示不

[转]Android6.0以上关于RecyclerView显示不

作者: Amy_LuLu__ | 来源:发表于2019-05-17 09:34 被阅读0次

    原文:https://www.cnblogs.com/zhangmiao14/p/9538979.html

    需求描述

    适配的时候发现Android 6.0以上,RecyclerView显示不全,以下是没有问题的。
    这个时候查看布局文件,可以看到RecyclerView是嵌套在ScrollView中的。

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <android.support.v7.widget.RecyclerView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:overScrollMode="never"
                android:paddingLeft="16dp"
                android:paddingRight="16dp" />
        </ScrollView>
    
    

    解决方法

    第一种解决方法

    方法

    在RecyclerView上在嵌套上一层RelativeLayout,添加属性android:descendantFocusability=”blocksDescendants”(当一个view获取焦点时,viewGroup会覆盖子类控件而直接获取焦点)解决嵌套卡顿的问题。

    注意,设立只能使用RelativeLayout,使用FrameLayout、LinearLayout等其他常用的布局控件是不行的。亲测。

    代码
     <ScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:descendantFocusability="blocksDescendants">
    
                <android.support.v7.widget.RecyclerView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:overScrollMode="never"
                    android:paddingLeft="16dp"
                    android:paddingRight="16dp" />
    
            </RelativeLayout>
        </ScrollView>
    
    

    第二种解决方法

    方法

    使用android.support.v4.widget.NestedScrollView替代ScrollView,并在代码中设置recyclerView.setNestedScrollingEnabled(false)(设置是否允许嵌套滑动)。既可以填ITEM显示不全的坑,又可以填嵌套滑动卡顿的坑。

    代码
        <android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <android.support.v7.widget.RecyclerView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:overScrollMode="never"
                android:paddingLeft="16dp"
                android:paddingRight="16dp" />
        </android.support.v4.widget.NestedScrollView>
    
    
    recyclerView.setNestedScrollingEnabled(false);
    
    

    总结

    相比较而言,个人觉得第二种解决方法更好,第一种方法布局嵌套,总有一种想让人优化的冲动,所以觉得第二种解决方法更好。

    相关文章

      网友评论

        本文标题:[转]Android6.0以上关于RecyclerView显示不

        本文链接:https://www.haomeiwen.com/subject/vrxnaqtx.html