android之ListView自定义布局

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

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

 先看效果:

android之ListView自定义布局

首先定义一个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适配器


上图:

android之ListView自定义布局