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,发现界面一点都不会卡了,所以接受一个教训就是布局如果要刷新的话,建议还是不要嵌套太多层,毕竟重绘很耗时间。

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

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

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • sublime怎么运行php(sublime运行python)

    前言:我用的是phpstudy安装包,安装php环境,因为是个人PC也没什么敏感文件,所以用phpstudy安装环境是可以的。phpstudy存在安全问题,之前也讨论过了。我用的是sublime编辑器,美观,配置环境稍微有点烦。配置步骤下载、安装phpstudy官网安装时注意不要选择中文安装目录,否则报错,Apache无法启动。安装后找到目录php.exe的所在目录,复制这个路

    2022年4月15日
  • 一步到位分布式开发Zookeeper实现集群管理

    一步到位分布式开发Zookeeper实现集群管理

    2022年3月12日
  • Spring Cloud 微服务架构图「建议收藏」

    Spring Cloud 微服务架构图「建议收藏」SpringCloud微服务总体架构图上面图中技术名词理解:1、Sleuth-链路跟踪为服务之间调用提供链路追踪。通过Sleuth可以很清楚的了解到一个服务请求经过了哪些服务,每个服务处理花费了多长。从而让我们可以很方便的理清各微服务间的调用关系。2、断路器(Hystrix)在微服务架构中,根据业务来拆分成一个个的服务,服务与服务之间可以相互调…

    2022年4月30日
  • accept 函数_case when函数

    accept 函数_case when函数2.关于AcceptEx  使用此函数时,要包含头文:Mswsock.h,同时要链接:Mswsock.lib。可在源程序中加入下面的语句,这样在编译时,将自动链接Mswsock.lib。  #pragmacomment(lib,”Mswsock.lib”)    下面是使用AcceptEx函数的示例代码:  #defineSTRICT  #define_WIN32_WINNT0x050

    2022年9月29日
  • vue webpak版本 查看_vue升级webpack版本到webpack4

    vue webpak版本 查看_vue升级webpack版本到webpack4用vue来实验下,目的是把webpack3升级至^4.0更新思路是先更新看是否废弃掉了yarnglobaladd@vue/cli-initvueinitwebpackhello_vuecdhello_vuenpmstart需要先验证工程是正确的,可以运行后去看下webpack版本发现是3.6npminstallwebpack@latest-Dnpminstallw…

    2022年8月10日
  • CSS,font-family,好看常用的中文字体

    CSS,font-family,好看常用的中文字体

    2021年9月20日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

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