android listview 滑动卡顿问题解决

android listview 滑动卡顿问题解决之前在使用listview进行每次通知一来,根据判断是否有这个标志,就更新listview所绑定的数据源,通知更新priceAd.notifyDataSetChanged();,也用了网上的建议使用viewHolder进行listview的item复用机制,但还是会出现卡顿的现象,经过分析,原来是listview的item布局嵌套太多,导致刷新的时候,重绘过多,造成卡顿的现象,以下是另一片博客的分

大家好,又见面了,我是你们的朋友全栈君。

        之前在使用listview进行每次通知一来,根据判断是否有这个标志,就更新listview所绑定的数据源,通知更新priceAd.notifyDataSetChanged();,也用了网上的建议使用viewHolder进行listview的item复用机制,但还是会出现卡顿的现象,经过分析,原来是listview的item布局嵌套太多,导致刷新的时候,重绘过多,造成卡顿的现象,以下是另一片博客的分析贴出来,大家讨论感谢这位前辈:http://blog.csdn.net/knighttools/article/details/18259485:

解决与分析:
通过百度,View在Draw的时候分成两个阶段:measure和layout,在measure阶段时主要就是为了计算两个参数:height和width。而且要注意的是,这是个递归的过程,从顶向下,DecorView开始依次调用自己子元素的measure。计算完成这两个参数后就开始layout,最后再是draw的调用。
对于ListView,当然每一个Item都会被调用measure方法,而在这个过程中getView和getCount会被调用,而且看用户的需求,可能会有很多次调用。

而为什么会有很多组次调用呢?

问题就在于在layout中的决定ListView或者它的父元素的height和width属性的定义了。fill_parent会好一点,计算方法会比较简单,只要跟父元素的大小相似就行,但是即使是fill_parent,也不能给View当饭吃,还是要计算出来具体的dip,所以measure还是会被调用,只是可能比wrap_content的少一点。至于自适应的它会一直考量它的宽和高,根据内容(也就是它的子Item)计算宽高。可能这个measure过程会反复执行,如果父元素也是wrap_content,这个过程会更加漫长。

所以,解决方法就是尽量避免自适应,除非是万不得已,固定大小或者填充的效果会比较好一些。

于是我们把listview与他父控件的所有高度与宽度都设置为fill_parent,果然getview调用正常了,注意是所有的高度和宽度!

当发现初始化adapter的时候正常调用之后,我们再来尝试滑动listview,发现每出现一个item,当前视图显示的item又调用了一次getview,通过刚哥的这篇帖子,定位到问题在我的getview中使用了// notifyDataSetChanged();   把这行去掉 listview 就已经宣告不再卡顿了!

附带刚哥的listview卡顿终极解决方案原帖:刚哥的Listview卡顿终极解决方案。

     根据他的提示,我的确发现自己的item的布局,嵌套了很多层的linearLayout,把它们删掉,改成权重布局,先前的布局如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@color/background"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/price_quote_inst"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="25dp"
android:layout_weight="1"
android:gravity="right"
android:singleLine="true"
android:text="WYB/TEST"
android:textColor="@color/white"
android:textSize="20dp" >
</TextView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_gravity="bottom|right"
android:layout_weight="2"
android:gravity="bottom|right"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="時間:"
android:textColor="@color/white"
android:textSize="16dp" >
</TextView>
<TextView
android:id="@+id/price_quote_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="14:34:47"
android:textColor="@color/white"
android:textSize="16dp" >
</TextView>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="horizontal" >
<LinearLayout
android:id="@+id/sellMktt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/pair_sell_select"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:gravity="center"
android:text="@string/activity_price_pricewarning_add4modify_sellprice"
android:textColor="@color/white" />
<TextView
android:id="@+id/price_quote_sellprice"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="0.7023"
android:textSize="20dp"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:layout_width="2dp"
android:layout_height="wrap_content" />
<LinearLayout
android:id="@+id/buyMktt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/ordertrade_pair_buy_select"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:gravity="center"
android:text="@string/activity_price_pricewarning_add4modify_buyprice"
android:textColor="@color/gray" />
<TextView
android:id="@+id/price_quote_buyprice"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="0.7023"
android:textSize="20dp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=":"
android:textColor="@color/white"
android:textSize="14sp" />
<TextView
android:id="@+id/price_quote_low"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0.6844"
android:textColor="@color/white"
android:textSize="14sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=":"
android:textColor="@color/white"
android:textSize="14sp" />
<TextView
android:id="@+id/price_quote_updown"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-1.91"
android:textColor="@color/white"
android:textSize="14sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_weight="2"
android:text=":"
android:textColor="@color/white"
android:textSize="14sp" />
<TextView
android:id="@+id/price_quote_high"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="1"
android:text="0.7130"
android:textColor="@color/white"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1.2px"
android:layout_marginLeft="14dp"
android:layout_marginRight="14dp"
android:layout_marginTop="5dp"
android:background="@color/white" />
<TextView
android:id="@+id/hidepricemenu"
android:layout_width="match_parent"
android:layout_height="30dp"
android:visibility="gone" />
</LinearLayout>

将android:layout_width都改成fill_parent,并且将linearLayout都删除掉,改成权重布局,如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="10dp"
android:background="@color/background"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/price_quote_inst"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="25dp"
android:layout_weight="1"
android:gravity="right"
android:singleLine="true"
android:text="WYB/TEST"
android:textColor="@color/white"
android:textSize="20dp" >
</TextView>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_gravity="bottom|right"
android:layout_weight="2"
android:gravity="bottom|right"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="時間:"
android:textColor="@color/white"
android:textSize="16dp" >
</TextView>
<TextView
android:id="@+id/price_quote_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="14:34:47"
android:textColor="@color/white"
android:textSize="16dp" >
</TextView>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="horizontal" >
<LinearLayout
android:id="@+id/sellMktt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/pair_sell_select"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:gravity="center"
android:text="@string/activity_price_pricewarning_add4modify_sellprice"
android:textColor="@color/white" />
<TextView
android:id="@+id/price_quote_sellprice"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="0.7023"
android:textSize="20dp"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:layout_width="2dp"
android:layout_height="wrap_content" />
<LinearLayout
android:id="@+id/buyMktt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/ordertrade_pair_buy_select"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:gravity="center"
android:text="@string/activity_price_pricewarning_add4modify_buyprice"
android:textColor="@color/gray" />
<TextView
android:id="@+id/price_quote_buyprice"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="0.7023"
android:textSize="20dp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:orientation="horizontal" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=":"
android:textColor="@color/white"
android:textSize="14sp" />
<TextView
android:id="@+id/price_quote_low"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="0.6844"
android:textColor="@color/white"
android:textSize="14sp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=":"
android:textColor="@color/white"
android:textSize="14sp" />
<TextView
android:id="@+id/price_quote_updown"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="-1.91"
android:textColor="@color/white"
android:textSize="14sp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_weight="1"
android:text=":"
android:textColor="@color/white"
android:textSize="14sp" />
<TextView
android:id="@+id/price_quote_high"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="1"
android:text="0.7130"
android:textColor="@color/white"
android:textSize="14sp" />
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1.2px"
android:layout_marginLeft="14dp"
android:layout_marginRight="14dp"
android:layout_marginTop="5dp"
android:background="@color/white" />
<TextView
android:id="@+id/hidepricemenu"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:visibility="gone" />
</LinearLayout>

    重启手机app,发现界面一点都不会卡了,所以接受一个教训就是布局如果要刷新的话,建议还是不要嵌套太多层,毕竟重绘很耗时间。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/163298.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • 如何实现dede首页栏目文章指定调用

    如何实现dede首页栏目文章指定调用

  • 电商仓库erp软件_电商交易系统和订单系统

    电商仓库erp软件_电商交易系统和订单系统1、系统独立部署、永久使用,自行管理绑定和授权店铺账号,所有数据都保存在自己的数据库中,多账号使用不关联。2、支持前端的自定义开发和后端功能定制。3、零售、分销、批发、营销方式全面支持,支持兰亭、DX等批量发货、定期对账结算。4、ERP、商品、库存、订单、list售价、list库存实时同步。5、灵活的商品注册:支持多款式、组合品、商品图片6、将从平台下载的订单自动快速导入、自动派单、分配库存。7、具备每天300…

  • 分治法-汉诺塔问题

    分治法-汉诺塔问题

  • 如何查看Vue项目vue的版本号

    如何查看Vue项目vue的版本号如果是用vue-cli创建的项目,则找到项目根目录下的”package.json”文件如果是要查看vue-cli的版本号的话,则键盘Win+R,输入cmd,再在cmd里面输入vue-V

  • es6模板字符串_js循环字符串

    es6模板字符串_js循环字符串相比ES5的拼接字符串,ES6毫无疑问是简单明了,又清晰可维护。原始的字符串拼接真的是把整个人都拼傻,并且在处理的过程中会出现很多小坑,小问题。$(‘#ulList’).html(`&lt;ul&gt;&lt;li&gt;浙江&lt;/li&gt;&lt;li&gt;杭州&lt;/li&gt;&lt;/ul&gt;`);上面就是用es6的…

  • 临界区保护_临界地带

    临界区保护_临界地带1临界区保护1.1问题引入首先看一下如下问题:原因分析:根本原因在于读-改-写过程中随时会被打断,再恢复运行时写,导致打断过程中其它写的效果被覆盖。1.2临界区概念临界区的概念如下:临界区指的是访问多个任务共享资源的一段代码。当有任务进入临界区时,其它任务必须等待直至该任务离开临界区,以确定共享资源的访问不会冲突。由于共享资源的访问存在于任务与任务之间、任务与中断I…

    2022年10月30日

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号