zoukankan      html  css  js  c++  java
  • Android6.0以上关于RecyclerView显是不全的问题

    Android6.0以上关于RecyclerView显是不全的问题

    需求描述

    适配的时候发现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);
    

    总结

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

  • 相关阅读:
    【FPGA篇章四】FPGA状态机:三段式以及书写方法
    【FPGA篇章三】FPGA常用语句:Verilog基本语法要素
    【FPGA篇章二】FPGA开发流程:详述每一环节的物理含义和实现目标
    【FPGA篇章一】FPGA工作原理:详细介绍FPGA实现编程逻辑的机理
    学习python随笔记
    Spring中的@Bean注解、@Configuration注解、@Value
    什么是Maven项目
    SpringBoot(四)thymeleaf+MyBatis+MySql
    SpringBoot(三)thymeleaf+JPA+MySql
    SpringBoot(二)thymeleaf模板的引入
  • 原文地址:https://www.cnblogs.com/zhangmiao14/p/9538979.html
Copyright © 2011-2022 走看看