ListvView属于常用的控件。通常使用的时候,不会继承ListView进行自定义,而是通过Adapter的getView函数进行自定义。具体涉及到的设计模式之类的东西不考虑,原本打算总结一下ListView的优化,刚好看到了一篇文章,里面基本总结好了我学到的和用到的知识点: 《提升Android ListView性能的几个技巧》http://www.imlongluo.com/blog/?p=263。所以不在重复,只补充几点:
一,在实际使用和平时的观察发现,ListView中每个Item的自定义view具有相同点,基本都是图片加标题(标题下面可能显示具体信息),需要操作的可能会在右边加一个Button。所以这个两个布局准备好,基本遇到的时候就可以用了。另外View是可以隐藏的,所以不需要的时候把对应的按钮隐藏就可以了(如右侧图片中的按钮隐藏了就和左边一样),所以实际中可以只需要一个布局文件。
xml布局代码:
<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"
tools:context="com.wziwen.securemanager.MainActivity" >
<ImageView
android:id="@+id/imageview"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="4dp"
android:background="@drawable/ic_launcher" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:layout_toRightOf="@id/imageview"
android:orientation="horizontal" >
<LinearLayout
android:id="@+id/layout_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollHorizontally="true"
android:text="@string/hello_world"
android:textSize="20sp" />
<TextView
android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:scrollHorizontally="true"
android:text="@string/hello_world" />
</LinearLayout>
<Button
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentRight="true" />
</RelativeLayout>
</RelativeLayout>
二,异步加载的时候,如果快速滑动的时候不过不注意优化由于导致瞬间生成一堆后台线程,所以 《提升Android ListView性能的几个技巧》中说可以在fling操作的时候不去启动线程。这点是原来没有想到的。原来想的是:后台线程应该使用线程池管理起来,尤其是从网络下载图片的时候,由于涉及到的网络下载后Bitmap的生成都是属于好使的操作,这点尤其重要。当然,如果提前把图片现在到本地,在使用的时候直接生成Bitmap可以提高速度。实际中发现有些软件在网络不行的时候会卡住界面,或者运行后在手机保存一堆Icon图片,严重让人怀疑软件的专业性。从中吸取教训,一次性的图标不用的时候最好删除,至少在文件夹中表明图片不在图库显示(在文件夹中生成一个 .nomedia 的文件,主要前面有个点)。