• Android LIstView初次创建getview方法执行多次问题


      写listview优化的时候,发现Listview初次创建的时候会多次执行getView方法。

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.example.hang.myapplication.MainActivity">
            <ListView
                android:id="@+id/list_item1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
    </RelativeLayout>


    这是listview的布局,执行后结果为:

    06-30 20:04:14.094 1166-1166/com.example.hang.myapplication D/hello: convertView为空0
    06-30 20:04:14.104 1166-1166/com.example.hang.myapplication D/hello: convertView为空1
    06-30 20:04:14.104 1166-1166/com.example.hang.myapplication D/hello: convertView为空2
    06-30 20:04:14.114 1166-1166/com.example.hang.myapplication D/hello: convertView为空3
    06-30 20:15:31.944 13339-13339/com.example.hang.myapplication D/hello: convertView为空0
    06-30 20:15:31.944 13339-13339/com.example.hang.myapplication D/hello: convertView不为空1
    06-30 20:15:31.954 13339-13339/com.example.hang.myapplication D/hello: convertView不为空2
    06-30 20:15:31.954 13339-13339/com.example.hang.myapplication D/hello: convertView不为空3
    06-30 20:15:31.954 13339-13339/com.example.hang.myapplication D/hello: convertView不为空0
    06-30 20:15:31.954 13339-13339/com.example.hang.myapplication D/hello: convertView不为空1
    06-30 20:15:31.954 13339-13339/com.example.hang.myapplication D/hello: convertView不为空2
    06-30 20:15:31.954 13339-13339/com.example.hang.myapplication D/hello: convertView不为空3
    06-30 20:15:31.984 13339-13339/com.example.hang.myapplication D/hello: convertView不为空0
    06-30 20:15:31.994 13339-13339/com.example.hang.myapplication D/hello: convertView为空1
    06-30 20:15:31.994 13339-13339/com.example.hang.myapplication D/hello: convertView为空2
    06-30 20:15:31.994 13339-13339/com.example.hang.myapplication D/hello: convertView为空3
    06-30 20:15:32.054 13339-13339/com.example.hang.myapplication D/hello: convertView为空0
    06-30 20:15:32.054 13339-13339/com.example.hang.myapplication D/hello: convertView不为空1
    06-30 20:15:32.054 13339-13339/com.example.hang.myapplication D/hello: convertView不为空2
    06-30 20:15:32.054 13339-13339/com.example.hang.myapplication D/hello: convertView不为空3
    06-30 20:15:32.054 13339-13339/com.example.hang.myapplication D/hello: convertView不为空0
    06-30 20:15:32.054 13339-13339/com.example.hang.myapplication D/hello: convertView不为空1
    06-30 20:15:32.054 13339-13339/com.example.hang.myapplication D/hello: convertView不为空2
    06-30 20:15:32.054 13339-13339/com.example.hang.myapplication D/hello: convertView不为空3
    06-30 20:15:32.124 13339-13339/com.example.hang.myapplication D/hello: convertView不为空0
    06-30 20:15:32.134 13339-13339/com.example.hang.myapplication D/hello: convertView不为空1
    06-30 20:15:32.134 13339-13339/com.example.hang.myapplication D/hello: convertView不为空2
    06-30 20:15:32.134 13339-13339/com.example.hang.myapplication D/hello: convertView不为空3
    06-30 20:15:32.134 13339-13339/com.example.hang.myapplication D/hello: convertView不为空0
    06-30 20:15:32.134 13339-13339/com.example.hang.myapplication D/hello: convertView不为空1
    06-30 20:15:32.134 13339-13339/com.example.hang.myapplication D/hello: convertView不为空2
    06-30 20:15:32.134 13339-13339/com.example.hang.myapplication D/hello: convertView不为空3

    这显然不正常,执行了多次getview方法。经过群友的提示修改为fill_partent后结果显示正常。

    06-30 20:04:14.094 1166-1166/com.example.hang.myapplication D/hello: convertView为空0
    06-30 20:04:14.104 1166-1166/com.example.hang.myapplication D/hello: convertView为空1
    06-30 20:04:14.104 1166-1166/com.example.hang.myapplication D/hello: convertView为空2
    06-30 20:04:14.114 1166-1166/com.example.hang.myapplication D/hello: convertView为空3

    后来联想到UI的优化,恍然大悟~

      RelativeLayouts经常需要measure所有子节点两次才能把子节点合理的布局。如果子节点设置了weights属性,LinearLayouts也需要measure这些节点两次,才能获得精确的展示尺寸。 

      在UI布局优化中,推荐扁平式布局。也是因为view开始被measure时,该view所有的子view都会被重新layout,再把该view传递给它的父view,如此重复一直到最顶部的根view。layout完成之后,所有的view都被渲染到屏幕上,并不是只有用户看得见的view才会被渲染,所有的view都会。这样一个view会被多次measure。getiew也执行了多次。

    后来我嵌套了一层RelativeLayout,执行后发现getview执行次数比不嵌套多了一次~,嵌套linearlayout是没有任何变化的

    06-30 20:25:18.314 13339-13339/com.example.hang.myapplication D/hello: convertView为空0
    06-30 20:25:18.324 13339-13339/com.example.hang.myapplication D/hello: convertView不为空1
    06-30 20:25:18.324 13339-13339/com.example.hang.myapplication D/hello: convertView不为空2
    06-30 20:25:18.324 13339-13339/com.example.hang.myapplication D/hello: convertView不为空3
    06-30 20:25:18.324 13339-13339/com.example.hang.myapplication D/hello: convertView不为空0
    06-30 20:25:18.324 13339-13339/com.example.hang.myapplication D/hello: convertView不为空1
    06-30 20:25:18.324 13339-13339/com.example.hang.myapplication D/hello: convertView不为空2
    06-30 20:25:18.324 13339-13339/com.example.hang.myapplication D/hello: convertView不为空3
    06-30 20:25:18.324 13339-13339/com.example.hang.myapplication D/hello: convertView不为空0
    06-30 20:25:18.324 13339-13339/com.example.hang.myapplication D/hello: convertView不为空1
    06-30 20:25:18.324 13339-13339/com.example.hang.myapplication D/hello: convertView不为空2
    06-30 20:25:18.334 13339-13339/com.example.hang.myapplication D/hello: convertView不为空3
    06-30 20:25:18.334 13339-13339/com.example.hang.myapplication D/hello: convertView不为空0
    06-30 20:25:18.334 13339-13339/com.example.hang.myapplication D/hello: convertView不为空1
    06-30 20:25:18.334 13339-13339/com.example.hang.myapplication D/hello: convertView不为空2
    06-30 20:25:18.334 13339-13339/com.example.hang.myapplication D/hello: convertView不为空3
    06-30 20:25:18.344 13339-13339/com.example.hang.myapplication D/hello: convertView不为空0
    06-30 20:25:18.344 13339-13339/com.example.hang.myapplication D/hello: convertView为空1
    06-30 20:25:18.354 13339-13339/com.example.hang.myapplication D/hello: convertView为空2
    06-30 20:25:18.354 13339-13339/com.example.hang.myapplication D/hello: convertView为空3
    06-30 20:25:18.404 13339-13339/com.example.hang.myapplication D/hello: convertView为空0
    06-30 20:25:18.404 13339-13339/com.example.hang.myapplication D/hello: convertView不为空1
    06-30 20:25:18.404 13339-13339/com.example.hang.myapplication D/hello: convertView不为空2
    06-30 20:25:18.414 13339-13339/com.example.hang.myapplication D/hello: convertView不为空3
    06-30 20:25:18.414 13339-13339/com.example.hang.myapplication D/hello: convertView不为空0
    06-30 20:25:18.414 13339-13339/com.example.hang.myapplication D/hello: convertView不为空1
    06-30 20:25:18.414 13339-13339/com.example.hang.myapplication D/hello: convertView不为空2
    06-30 20:25:18.414 13339-13339/com.example.hang.myapplication D/hello: convertView不为空3
    06-30 20:25:18.414 13339-13339/com.example.hang.myapplication D/hello: convertView不为空0
    06-30 20:25:18.414 13339-13339/com.example.hang.myapplication D/hello: convertView不为空1
    06-30 20:25:18.414 13339-13339/com.example.hang.myapplication D/hello: convertView不为空2
    06-30 20:25:18.414 13339-13339/com.example.hang.myapplication D/hello: convertView不为空3
    06-30 20:25:18.414 13339-13339/com.example.hang.myapplication D/hello: convertView不为空0
    06-30 20:25:18.414 13339-13339/com.example.hang.myapplication D/hello: convertView不为空1
    06-30 20:25:18.414 13339-13339/com.example.hang.myapplication D/hello: convertView不为空2
    06-30 20:25:18.414 13339-13339/com.example.hang.myapplication D/hello: convertView不为空3

      解决方案,设置为fill_partent或者设置为固定的数值,优化UI布局。这个问题也可能会导致加载图片等错位。

  • 相关阅读:
    win10自带邮箱应用无法查看qq邮箱应用解决办法
    Ubuntu紫色背景颜色代码
    VMware中对Linux虚拟机的网络配置静态IP的配置
    CentOS 7在VMware 12中共享文件看不见的问题?
    C++中让人忽视的左值和右值
    C++ allocator类学习理解
    C++11新特性 -----> 右值引用 &&
    重新认识new
    关于C++中nothrow的某某某
    stopPropagation, preventDefault 和 return false 的区别
  • 原文地址:https://www.cnblogs.com/yuhanghzsd/p/5631150.html
Copyright © 2020-2023  润新知