1、Android学习之ListViewAndroid学习之ListView该类容从网上搜集并总结:在android应用开发中,ListView是使用频率非常高的一个组件,基本上稍微复杂点的布局都会用到它,利用它可以让你的界面美观,有层次。ListView可以用来作为数据显示的容器,也可以作为界面的布局。学习ListView需要关注的内容大概有三点:显示、数据适配器以及各种事件的监听器。内容有点多,这里先只讲如何让ListView达到你想要的显示效果。 一、普通的ListView 普通的ListView是指每一个item只显示一条文本数据,程序运行效果图如下:代码:protected void o
2、nCreate(Bundle savedInstanceState) / TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.layout_listview_simple); /取得ListView实例 ListView lvwSimple = (ListView)findViewById(R.id.lvw_simple); /要在ListView中显示的数据集合 String items = new String item1, item2, item3, ite
3、m4, item5; /new一个ArrayAdapter,android.R.layout.simple_list_item_1为ListView显示的布局文件 ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, items); /位ListView设置Adapter lvwSimple.setAdapter(adapter);二、自定义ListView上面的那种方法只能显示简单的文字信息,显然在很多场合下都不够用,比如你想要在一个item中显示图片,显示多行文字,这就需要你自定
4、义ListView的布局了。效果图如下:步骤:1、分析你想要实现的布局效果,自定义布局文件lvw_custom.xml,该布局文件针对的是ListView的item,而不是整个ListView:xml version=1.0 encoding=utf-8? LinearLayoutLinearLayout2、取得用于ListView的数据集合,类型是ArrayList,每一个Map对应于ListView的一个item,多个map就构成了ListView的数据集合:/* * 取得用于ListView的数据 * return */ private ArrayList getItems() Arra
5、yList items = new ArrayList(); for(int i = 0; i 5; i+) /* /* 每一个map中的数据对应与ListView中的一个item * /* 在我自定义的布局文件中,一个item包括: * /* 1、图片(lvw_custom_img) * /* 2、名称(lvw_custom_name) * /* 3、描述(lvw_custom_description) * /* 所以map中也至少需要包括这三项数据 * /* HashMap map = new HashMap(); /图片,key值可以随便取,映射关系会在实例化Adapter时定义,但我喜
6、欢将key与布局文件中定义的id取同样的值 /value值为图片的资源id map.put(lvw_custom_img, R.drawable.custom); /名称 map.put(lvw_custom_name, item名称); /描述 map.put(lvw_custom_description, item描述); items.add(map); return items; /hashmap/hashmap/hashmap3、为ListView设置SimpleAdapter:/取得ListView实例 ListView lvwCustom = (ListView)findViewB
7、yId(R.id.lvw_custom); /要在ListView中显示的数据集合 ArrayList items = getItems(); /* /* new一个SimpleAdapter * /* items为数据集合 * /* R.layout.lvw_custom为自定义的ListView布局文件 * /* 第四个参数为map中德key集合 * /* 第五个参数为自定义布局文件中空间的资源id集合,与第四个参数要一一对应 * /* SimpleAdapter adapter = new SimpleAdapter(this, items, R.layout.lvw_custom, n
8、ew String lvw_custom_img,lvw_custom_name,lvw_custom_description, new int R.id.lvw_custom_img, R.id.lvw_custom_name, R.id.lvw_custom_description); /位ListView设置Adapter lvwCustom.setAdapter(adapter);/hashmap三、显示非资源id类型图片的ListView通过上面的例子可以看到Map中图片项的value是资源id,这是针对项目中已存在的图片文件,为什么要用资源id而不是其他(比如Bitmap类型)呢,
9、这是因为adapter的bindView()方法是负责解析图片并将其显示到ImageView中,但它只针对资源id类型做了判断。然而有一种情况,比如你的图片是从网络读取的Bitmap类型,你就需要对代码进行改写了。分析SimpleAdapter的源码,发现getView()方法是负责够造界面布局的的,而getView又是调用bindView来往控件里填充值的,所以我这里对bindView()方法进行改写。新建一个类CustomImageAdapter,代码完全copy自SimpleAdapter,找到bindView方法,对其进行改写:private void bindView(int pos
10、ition, View view) final Map dataSet = mData.get(position); if (dataSet = null) return; final ViewBinder binder = mViewBinder; final View holder = mHolders.get(view); final String from = mFrom; final int to = mTo; final int count = to.length; for (int i = 0; i count; i+) final View v = holderi; if (v
11、 != null) final Object data = String text = data = null ? : data.toString(); if (text = null) text = ; boolean bound = false; if (binder != null) bound = binder.setViewValue(v, data, text); if (!bound) if (v instanceof Checkable) if (data instanceof Boolean) (Checkable) v).setChecked(Boolean) data);
12、 else throw new IllegalStateException(v.getClass().getName() + should be bound to a Boolean, not a + data.getClass(); else if (v instanceof TextView) / Note: keep the instanceof TextView check at the bottom of these / ifs since a lot of views are TextViews (e.g. CheckBoxes). setViewText(TextView) v,
13、 text); else if (v instanceof ImageView) if (data instanceof Integer) setViewImage(ImageView) v, (Integer) data); /这里增加对Bitmap类型的判断 else if(data instanceof Bitmap) (ImageView)v).setImageBitmap(Bitmap)data); else setViewImage(ImageView) v, text); else throw new IllegalStateException(v.getClass().getN
14、ame() + is not a + view that can be bounds by this SimpleAdapter); 然后像自定义ListView的步骤一样使用就行了,只是把SimpleAdapter替换为CustomImageAdapter,Map中图片项的value变为Bitmap类型了。四、Item使用不同布局的ListView通过前面的例子可以看到,ListView的所有item使用的都是相同的布局,如果想使用不同的布局呢?这个例子是我从以前做的音乐播放器代码里找的,效果图:MP3的封面图片突然不显示了,不知道咋回事。步骤:1、在Map中存放的键值对中多增加一项布局类型
15、:/* * 根据playlistId获得歌曲列表,用于ListView显示 * * param playlistId * 0表示所有歌曲 * return */ private List getSongList(int playlistId) / 取得符合条件的所有歌曲 List songs = getSongs(playlistId); / 构造SongList的数据 List list = new ArrayList(); Map map1 = new HashMap(); /这里的Type是用于在getView方法中判断并构造布局的 map1.put(type, TYPE_BTN); m
16、ap1.put(id, ID_RANDOM_PLAY); map1.put(mainlist_btn_name, 随机播放); map1.put(mainlist_btn_img, R.drawable.list_random_icon); list.add(map1); if (songs != null) int size = songs.size(); for (int i = 0; i size; i+) Song song = songs.get(i); Map map = new HashMap(); /这里的Type是用于在getView方法中判断并构造布局的 map.put(t
17、ype, TYPE_SONG_LIST); map.put(id, song.getId(); Bitmap bm = MusicHelper.getArtwork(this, song.getId(), song .getAlbumId(), true); map.put(songlist_cover_img, bm); map.put(songlist_song_name, song.getTitle(); map.put(songlist_song_album, song.getAlbum(); map.put(songlist_song_artist, song.getArtist(); list.add(map); return list; /map/map TextView TextView TextViewRelativeLayoutLinearLayoutRelativeLayoutlist_main_btn.xml:xml version=1.0 encoding=utf-8? LinearLayout3、自定义Adapter类SongListAdapter,继承自BaseAd
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1