Android之listview && adapter
今天我们讲的也是非常重要的一个控件listview—最常用也是最难的
一个ListView通常有两个职责。
(1)将数据填充到布局。
(2)处理用户的选择点击等操作。
第一点很好理解,ListView就是实现这个功能的。第二点也不难做到,在后面的学习中读者会发现,这非常简单。
一个ListView的创建需要3个元素。
(1)ListView中的每一列的View。
(2)填入View的数据或者图片等。
(3)连接数据与ListView的适配器。
也就是说,要使用ListView,首先要了解什么是适配器。适配器是一个连接数据和AdapterView(ListView就是一个典型的AdapterView,后面还会学习其他的)的桥梁,通过它能有效地实现数据与AdapterView的分离设置,使AdapterView与数据的绑定更加简便,修改更加方便
Android中提供了很多的Adapter,表4-5列出了常用的几个。
表4-5 常用适配器
Adapter |
含义 |
ArrayAdapter<T> |
用来绑定一个数组,支持泛型操作 |
SimpleAdapter |
用来绑定在xml中定义的控件对应的数据 |
SimpleCursorAdapter |
用来绑定游标得到的数据 |
BaseAdapter |
通用的基础适配器 |
其实适配器还有很多,要注意的是,各种Adapter只不过是转换的方式和能力不一样而已。
ListView使用SimpleAdapter
很多时候需要在列表中展示一些除了文字以外的东西,比如图片等。这时候可以使用SimpleAdapter。SimpleAdapter的使用也非常简单,同时它的功能也非常强大。可以通过它自定义ListView中的item的内容,比如图片、多选框等。
使用simpleAdapter的数据一般都是用HashMap构成的列表,列表的每一节对应ListView的每一行。通过SimpleAdapter的构造函数,将HashMap的每个键的数据映射到布局文件中对应控件上。这个布局文件一般根据自己的需要来自己定义。梳理一下使用SimpleAdapter的步骤。
(1)根据需要定义ListView每行所实现的布局。
(2)定义一个HashMap构成的列表,将数据以键值对的方式存放在里面。
(3)构造SimpleAdapter对象。
(4)将LsitView绑定到SimpleAdapter上。
BaseAdapter
在ListView的使用中,有时候还需要在里面加入按钮等控件,实现单独的操作。也就是说,这个ListView不再只是展示数据,也不仅仅是这一行要来处理用户的操作,而是里面的控件要获得用户的焦点。读者可以试试用SimpleAdapter添加一个按钮到ListView的条目中,会发现可以添加,但是却无法获得焦点,点击操作被ListView的Item所覆盖。这时候最方便的方法就是使用灵活的适配器BaseAdapter了。
当系统开始绘制ListView的时候,首先调用getCount()方法。得到它的返回值,即ListView的长度。然后系统调用getView()方法,根据这个长度逐一绘制ListView的每一行。也就是说,如果让getCount()返回1,那么只显示一行。而getItem()和getItemId()则在需要处理和取得Adapter中的数据时调用。那么getView如何使用呢?如果有10000行数据,就绘制10000次?这肯定会极大的消耗资源,导致ListView滑动非常的慢,那应该怎么做呢?
下面就通过微信的例子说明吧!
下面是布局文件
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:andro 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <ListView 8 android: 9 android:layout_width="match_parent" 10 android:layout_height="match_parent" > 11 </ListView> 12 13 </LinearLayout>
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:andro 3 android:layout_width="match_parent" 4 android:layout_height="wrap_content" 5 android:paddingLeft="@dimen/activity_horizontal_margin" 6 android:paddingRight="@dimen/activity_horizontal_margin" 7 android:paddingTop="@dimen/activity_horizontal_margin" 8 android:paddingBottom="@dimen/activity_horizontal_margin" 9 android:orientation="horizontal" > 10 11 <ImageView 12 android: 13 android:layout_width="60dp" 14 android:layout_height="60dp" 15 android:src="@drawable/houzi" /> 16 17 18 <LinearLayout 19 android:layout_weight="1" 20 android:layout_width="wrap_content" 21 android:layout_height="wrap_content" 22 android:layout_margin="5dp" 23 android:orientation="vertical" > 24 25 <LinearLayout 26 android:layout_weight="1" 27 android:layout_width="match_parent" 28 android:layout_height="wrap_content" 29 android:layout_marginBottom="3dp" 30 android:orientation="horizontal" > 31 32 <TextView 33 android:layout_weight="1" 34 android: 35 android:layout_width="wrap_content" 36 android:layout_height="wrap_content" 37 android:textSize="20sp" 38 android:textColor="#7F7F7F"/> 39 40 <TextView 41 android: 42 android:layout_width="wrap_content" 43 android:layout_height="wrap_content" 44 android:textSize="15sp" 45 android:textColor="#A9A9A9"/> 46 </LinearLayout> 47 <LinearLayout 48 android:layout_weight="1" 49 android:layout_width="match_parent" 50 android:layout_height="wrap_content" 51 android:layout_marginBottom="3dp" 52 android:orientation="horizontal" > 53 <TextView 54 android:layout_weight="1" 55 android: 56 android:layout_width="wrap_content" 57 android:layout_height="wrap_content" 58 android:textSize="15sp" 59 android:textColor="#A9A9A9"/> 60 <TextView 61 android: 62 android:layout_width="wrap_content" 63 android:layout_height="wrap_content" 64 android:textSize="15sp" 65 android:text="" 66 android:textColor="#FF0000"/> 67 </LinearLayout> 68 </LinearLayout> 69 70 71 72 </LinearLayout>