1、Listview:列表控件,使用的时候高度和宽度最好是match-parent,此外,代码中使用还需要结合适配器。
2、ListView的使用及优化:(1) 使用ConvertView重用组件,即拖动时被遮住、看不见的控件,重用它,而非每次创建一个新的对象。当ListView第一次加载的时候,前面的数据ConvertView肯定是为null,所以我们在使用ConvertView对象的时候,先要判断是否为空,为空则从布局文件中解析一个新的view对象;不为空则直接拿来使用。(2)使用ConvertView.setTag()方法,每一次给布局文件中的控件设置值时,都需要调用findById(id)方法,重复查找会浪费时间,所以我们可以把控件的地址保存到一个对象中,再把这个对象保存在ConvertView中,下次对控件进行设值的时候,直接通过这个对象修改即可。(3)使用分页查询(PullToRefresh),使用AsyncTask(异步任务)加载数据,最少要重写以下这两个方法:doInBackground(后台执行)、onPostExecute(在doInBackground方法执行结束之后再运行,并且运行在UI线程当中 可以对UI空间进行设置)。优化的过程中需要使用LayoutInflater(布局解析器),用来把layout布局文件解析成一个View对象,不可以new,需要使用系统服务获得:inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE),下面有一个例子
listview_item.xml里的代码如下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <ImageView android:id="@+id/iv_listviewitem_image" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="3" android:padding="10dp" android:scaleType="fitXY" android:src="@drawable/book1" /> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="6" android:orientation="vertical" android:padding="10dp" > <TextView android:id="@+id/tv_listviewitme_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="狂人摄影日记" android:textColor="@color/red" android:textSize="20sp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:l