android图文混排之ListView自定义布局 – ma12an的专栏 – 博客频道 – CSDN.NET

前面总结了如何应用android自带的布局到ListView中去,现在如果想让自己的ListView更生动,只需要将自己定义的布局应用到ListView中,在布局中添加一些生动的元素,比如,图标。

一、为ListView每个item添加一个图标

 先看效果:


首先定义一个entry.xml布局文件:


<?xml version=”1.0″ encoding=”utf-8″?>

<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android

android:layout_width=”fill_parent”

android:layout_height=”60dp”

android:orientation=”horizontal” >

<!– 图标 –>

<ImageView

android:layout_width=”48dp”

android:layout_height=”48dp”

android:src=”@drawable/preview”

android:layout_gravity=”center”

android:layout_marginLeft=”10dp”

/>

<!– 这里引用android自带的布局simple_list_item_1 –>

<include

layout=”@android:layout/simple_list_item_1″

/>

</LinearLayout>

 

 

然后将此布局文件应用到ListView中去:

 


public class ListViewDemo extends ListActivity {

 

private List<Map<String,String>> data = new ArrayList<Map<String,String>>();

 

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

Map<String, String> map1 = new HashMap<String, String>();

map1.put(“姓名”, “A君”);

data.add(map1);

Map<String, String> map2 = new HashMap<String, String>();

map2.put(“姓名”, “B君”);

data.add(map2);

setListAdapter(new SimpleAdapter(this,data,R.layout.entry,

new String[]{“姓名”},

new int[]{android.R.id.text1}

));

}

 

 

二、动态设置图标

以上例子,处理的非常简单,全部图标都一样,现在要想动态设置每个item中元素,需要继承Adapter子类,通过重写getView()函数,为每个item都设置不同的风格。

下面创建BaseAdapter的子类MyAdapter:


private class MyAdapter extends SimpleAdapter{

//icons存储图标的id

private int[] icons = null;

 

private Context context;

public MyAdapter(Context context, List<? extends Map<String, ?>> data,

int resource, String[] from, int[] to,int[] icons) {

super(context, data, resource, from, to);

// TODO Auto-generated constructor stub

this.icons = icons;//传入存储图标id的数组

this.context = context;

}

/* (non-Javadoc)

* 重写getView函数,由于SimpleAdapter能处理TextView,仅仅是不能设置图标

*/所以,只需要在调用父类的getView函数的基础上,设置上相应的图标即可

@Override

public View getView(int position, View convertView, ViewGroup parent) {

// TODO Auto-generated method stub

ViewHolder holder = null;

if(convertView == null){

holder = new ViewHolder();

convertView = super.getView(position, convertView, parent);//调用父类的getView函数,设置TextView

holder.imageView = (ImageView)convertView.findViewById(R.id.myimageview);

convertView.setTag(holder);

}else {

holder = (ViewHolder)convertView.getTag();

}

//从icons中随机选出一个,并设置作为item的图标

Random random = new Random();

int index = random.nextInt(icons.length);

holder.imageView.setImageResource(icons[index]);

 

return convertView;

}

 

private class ViewHolder{

ImageView imageView;

}

}

 

MyAdapter继承自SimpleAdapter,重写getView()函数,使listView中每个item的图标都是随机选择的。对MyAdapter的应用如下:


//数组存储图标id

private int[] myicons = { R.drawable.preview,R.drawable.preview1,R.drawable.preview2,R.drawable.preview3};

 


setListAdapter(new MyAdapter(this, data, R.layout.entry, new String[]{“姓名”},

new int[]{android.R.id.text1}, myicons));//为ListView设置MyAdapter适配器

 

上图:

 

 

来源URL:http://cache.baiducontent.com/c?m=9f65cb4a8c8507ed4fece7631046893b4c4380146d96864968d4e414c422461e1465e3e87b3f4344959e2d3956b21f0baca36d2c761e25b29ecb8b49d7afd7756fde28672c4ac61c49850eafba5154b237e65bfede1df0caf72592de8c889e12179104406d81809c2a0003ba1ee76446f4d09d&p=9d74cc108f8512a05abd9b7e0e1cbb&newp=8a769a47a48411a058ee9e36520c9e231610db2151d0da6738&user=baidu&fm=sc&query=android+%CA%D3%C6%B5listview%B2%BC%BE%D6&qid=82ce0a2f0002e4f9&p1=8