android:
id="@+id/lvw_custom_description"
android:
layout_width="wrap_content"
android:
layout_height="wrap_content"
android:
layout_margin="5dip"
android:
textSize="10dip"
android:
text="item描述..."/>
LinearLayout>
LinearLayout>
2、取得用于ListView的数据集合,类型是ArrayList
/**
*取得用于ListView的数据
*@return
*/
privateArrayListgetItems(){
ArrayListitems=newArrayList();
for(inti=0;i<5;i++){
//***********************************************
//*每一个map中的数据对应与ListView中的一个item*
//*在我自定义的布局文件中,一个item包括:
*
//*1、图片(lvw_custom_img)*
//*2、名称(lvw_custom_name)*
//*3、描述(lvw_custom_description)*
//*所以map中也至少需要包括这三项数据*
//***********************************************
HashMapmap=newHashMap();
//图片,key值可以随便取,映射关系会在实例化Adapter时定义,但我喜欢将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);
}
returnitems;
}
3、为ListView设置SimpleAdapter:
//取得ListView实例
ListViewlvwCustom=(ListView)findViewById(R.id.lvw_custom);
//要在ListView中显示的数据集合
ArrayListitems=getItems();
//************************************************************************
//*new一个SimpleAdapter*
//*items为数据集合*
//*R.layout.lvw_custom为自定义的ListView布局文件*
//*第四个参数为map中德key集合*
//*第五个参数为自定义布局文件中空间的资源id集合,与第四个参数要一一对应*
//************************************************************************
SimpleAdapteradapter=newSimpleAdapter(this,items,R.layout.lvw_custom,
newString[]{"lvw_custom_img","lvw_custom_name","lvw_custom_description"},
newint[]{R.id.lvw_custom_img,R.id.lvw_custom_name,R.id.lvw_custom_description});
//位ListView设置Adapter
lvwCustom.setAdapter(adapter);
三、显示非资源id类型图片的ListView
通过上面的例子可以看到Map中图片项的value是资源id,这是针对项目中已存在的图片文件,为什么要用资源id而不是其他(比如Bitmap类型)呢,这是因为adapter的bindView()方法是负责解析图片并将其显示到ImageView中,但它只针对资源id类型做了判断。
然而有一种情况,比如你的图片是从网络读取的Bitmap类型,你就需要对代码进行改写了。
分析SimpleAdapter的源码,发现getView()方法是负责够造界面布局的的,而getView又是调用bindView来往控件里填充值的,所以我这里对bindView()方法进行改写。
新建一个类CustomImageAdapter,代码完全copy自SimpleAdapter,找到bindView方法,对其进行改写:
privatevoidbindView(intposition,Viewview){
finalMapdataSet=mData.get(position);
if(dataSet==null){
return;
}
finalViewBinderbinder=mViewBinder;
finalView[]holder=mHolders.get(view);
finalString[]from=mFrom;
finalint[]to=mTo;
finalintcount=to.length;
for(inti=0;ifinalViewv=holder[i];
if(v!
=null){
finalObjectdata=
Stringtext=data=null?
"":
data.toString();
if(text==null){
text="";
}
booleanbound=false;
if(binder!
=null){
bound=binder.setViewValue(v,data,text);
}
if(!
bound){
if(vinstanceofCheckable){
if(datainstanceofBoolean){
((Checkable)v).setChecked((Boolean)data);
}else{
thrownewIllegalStateException(v.getClass().getName()+
"shouldbeboundtoaBoolean,nota"+data.getClass());
}
}elseif(vinstanceofTextView){
//Note:
keeptheinstanceofTextViewcheckatthebottomofthese
//ifssincealotofviewsareTextViews(e.g.CheckBoxes).
setViewText((TextView)v,text);
}elseif(vinstanceofImageView){
if(datainstanceofInteger){
setViewImage((ImageView)v,(Integer)data);
}
//这里增加对Bitmap类型的判断
elseif(datainstanceofBitmap){
((ImageView)v).setImageBitmap((Bitmap)data);
}
else{
setViewImage((ImageView)v,text);
}
}else{
thrownewIllegalStateException(v.getClass().getName()+"isnota"+
"viewthatcanbeboundsbythisSimpleAdapter");
}
}
}
}
}
然后像自定义ListView的步骤一样使用就行了,只是把SimpleAdapter替换为CustomImageAdapter,Map中图片项的value变为Bitmap类型了。
四、Item使用不同布局的ListView
通过前面的例子可以看到,ListView的所有item使用的都是相同的布局,如果想使用不同的布局呢?
这个例子是我从以前做的音乐播放器代码里找的,效果图:
MP3的封面图片突然不显示了,不知道咋回事。
步骤:
1、在Map中存放的键值对中多增加一项布局类型:
/**
*根据playlistId获得歌曲列表,用于ListView显示
*
*@paramplaylistId
*0表示所有歌曲
*@return
*/
privateList
//取得符合条件的所有歌曲
Listsongs=getSongs(playlistId);
//构造SongList的数据
List
Mapmap1=newHashMap();
//这里的Type是用于在getView方法中判断并构造布局的
map1.put("type",TYPE_BTN);
map1.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){
intsize=songs.size();
for(inti=0;iSongsong=songs.get(i);
Mapmap=newHashMap();
//这里的Type是用于在getView方法中判断并构造布局的
map.put("type",TYPE_SONG_LIST);
map.put("id",song.getId());
Bitmapbm=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);
}
}
returnlist;
}
2、为不同的item布局分别定义不同的布局文件。
list_songs.xml:
xmlversion="1.0"encoding="utf-8"?
>
xmlns:
android="
android:
layout_width="fill_parent"
android:
layout_height="fill_parent"
>
android:
id="@+id/songlist_cover_img"
android:
layout_width="48dip"
android:
layout_height="48dip"
android:
layout_alignParentLeft="true"
android:
layout_centerVertical="true"
android:
layout_marginLeft="6dip"
/>
android:
orientation="vertical"
android:
layout_width="fill_parent"
android:
layout_height="wrap_content"
android:
layout_toRightOf="@id/songlist_cover_img"
android:
layout_alignParentRight="true"
android:
layout_centerVertical="true">
android:
id="@+id/songlist_song_name"
android:
layout_width="wrap_content"
android:
layout_height="wrap_content"
android:
layout_marginTop="6dip"
android:
layout_marginLeft="6dip"
android:
layout_marginBottom="3dip"
android:
textSize="24dip"
android:
textColor="@drawable/black">
TextView>
android:
layout_width="fill_parent"
android:
layout_height="wrap_content"
android:
layout_marginBottom="6dip">
android:
id="@+id/songlist_song_album"
android:
layout_width="wrap_content"
android:
layout_height="wrap_content"
android:
layout_marginTop="3dip"
android:
layout_marginLeft="6dip"
android:
textSize="10dip"
android:
textColor="@drawable/black">
TextView>
android:
id="@+id/songlist_song_artist"
android:
layout_width="wrap_content"
android:
layout_height="wrap_content"
android:
layout_marginTop="3dip"
android:
layout_marginRight="6dip"
android:
layout_alignParentRight="true"
android:
textSize="10dip"
android:
textColor="@drawable/black">
TextView>
RelativeLayout>
LinearLayout>
RelativeLayout>
list_main_btn.xml:
xmlversion="1.0"encoding="utf-8"?
>
xmlns:
android="
android:
orientation="horizontal"
android:
layout_width="fill_parent"
android:
layout_height="fill_parent"
>
android:
id="@+id/mainlist_btn_name"
android:
layout_width="wrap_content"
android:
layout_height="wrap_content"
android:
layout_marginLeft="6dip"
android:
layout_marginTop="10dip"
android:
layout_marginBottom="10dip"
android:
layout_gravity="center_vertical"
android:
textSize="28dip"
android:
textColor="@drawable/black"/>
android:
id="@+id/mainlist_btn_img"
android:
layout_width="wrap_content"
android:
layout_height="wrap_content"
android:
layout_gravity="center_vertical"
android:
layout_marginLeft="6dip"
/>
LinearLayout>
3、自定义Adapter类SongListAdapter,继承自BaseAd