Customize ListView ----自定义 listview

1. ListView with Icon

(1)效果图

listview_icon

(2)实现代码

1)listview.xml

Xml代码 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="horizontal"   
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent">  
  6.     <ListView android:id="@+id/listview"  
  7.         android:layout_width="fill_parent"  
  8.         android:layout_height="wrap_content"  
  9.     />     
  10. </LinearLayout>  

2)simple_list_item_icon.xml

Xml代码 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="horizontal" android:layout_width="wrap_content"  
  4.     android:layout_height="wrap_content">  
  5.     <ImageView android:id="@+id/simple_list_item_icon_iv"   
  6.         android:layout_width="wrap_content"  
  7.         android:layout_height="wrap_content"  
  8.         android:baselineAlignBottom="true">  
  9.     </ImageView>  
  10.     <TextView android:id="@+id/simple_list_item_icon_tv"   
  11.         android:layout_width="wrap_content"   
  12.         android:layout_height="wrap_content"  
  13.         android:textSize="24dip">  
  14.     </TextView>     
  15. </LinearLayout>  

3)IconSimpleAdapter.java

Java代码 
  1. public class IconSimpleAdapter extends BaseAdapter {  
  2.     private LayoutInflater mInflater;  
  3.     private List<Map<String, Object>> mList;  
  4.     private String[] mIndex;  
  5.   
  6.     public IconSimpleAdapter(Context context, String[] index, List<Map<String, Object>> list) {  
  7.         mInflater = LayoutInflater.from(context);  
  8.         mList = list;  
  9.         mIndex = index;  
  10.     }  
  11.   
  12.     @Override  
  13.     public int getCount() {  
  14.         return mList.size();  
  15.     }  
  16.   
  17.     @Override  
  18.     public Object getItem(int position) {  
  19.         return mList.get(position);  
  20.     }  
  21.   
  22.     @Override  
  23.     public long getItemId(int position) {  
  24.         return position;  
  25.     }  
  26.   
  27.     @Override  
  28.     public View getView(int position, View convertView, ViewGroup parent) {  
  29.         ViewHolder holder = null;  
  30.         if (convertView == null) {  
  31.             Bitmap mIcon = (Bitmap) mList.get(position).get(mIndex[0]);  
  32.             String mItem = " " + (String) mList.get(position).get(mIndex[1]);  
  33.               
  34.             convertView = mInflater.inflate(R.layout.simple_list_item_icon, null);  
  35.             holder = new ViewHolder();  
  36.             holder.text = (TextView) convertView.findViewById(R.id.simple_list_item_icon_tv);  
  37.             holder.icon = (ImageView) convertView.findViewById(R.id.simple_list_item_icon_iv);  
  38.             holder.text.setText(mItem);  
  39.             holder.icon.setImageBitmap(mIcon);  
  40.             convertView.setTag(holder);  
  41.         } else {  
  42.             holder = (ViewHolder) convertView.getTag();  
  43.         }  
  44.         return convertView;  
  45.     }  
  46.       
  47.     private class ViewHolder {  
  48.         TextView text;  
  49.         ImageView icon;  
  50.     }  
  51. }  

4)IconListActivity.java

Java代码 
  1. public class IconListActivity extends Activity {  
  2.     private final String ITEM_ICON = "icon";  
  3.     private final String ITEM_TEXT = "text";  
  4.     protected void onCreate(Bundle savedInstanceState) {  
  5.         super.onCreate(savedInstanceState);  
  6.         setContentView(R.layout.listview);  
  7.           
  8.         List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();  
  9.         Map<String, Object> item = new HashMap<String, Object>();  
  10.         Bitmap bm1 = BitmapFactory.decodeResource(getResources(), R.drawable.a);  
  11.         item.put(ITEM_ICON, bm1);  
  12.         item.put(ITEM_TEXT, "中秋节");  
  13.         data.add(item);  
  14.         item = new HashMap<String, Object>();  
  15.         Bitmap bm2 = BitmapFactory.decodeResource(getResources(), R.drawable.b);  
  16.         item.put(ITEM_ICON, bm2);  
  17.         item.put(ITEM_TEXT, "端午节");  
  18.         data.add(item);  
  19.         item = new HashMap<String, Object>();  
  20.         Bitmap bm3 = BitmapFactory.decodeResource(getResources(), R.drawable.c);  
  21.         item.put(ITEM_ICON, bm3);  
  22.         item.put(ITEM_TEXT, "重阳节");  
  23.         data.add(item);  
  24.         item = new HashMap<String, Object>();  
  25.         Bitmap bm4 = BitmapFactory.decodeResource(getResources(), R.drawable.d);  
  26.         item.put(ITEM_ICON, bm4);  
  27.         item.put(ITEM_TEXT, "清明节");  
  28.         data.add(item);  
  29.           
  30.         ListView lv = (ListView) findViewById(R.id.listview);  
  31.         IconSimpleAdapter adapter = new IconSimpleAdapter(thisnew String[] {ITEM_ICON, ITEM_TEXT}, data);  
  32.         lv.setAdapter(adapter);  
  33.     }  
  34. }  

 

2. ListView with 3Rows

(1)效果图

listview_3rows

(2)实现代码

1)hs_activity.xml

Xml代码 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="horizontal"   
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:id="@+id/hs_linearlayout">  
  7.     <ListView android:id="@+id/hs_activity_lv"  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="fill_parent"  
  10.     />  
  11. </LinearLayout>  

2)simple_list_item_3.xml

Xml代码 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"   
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent">  
  6.       
  7.     <TextView android:id="@+id/simple_list_item_3_tv1"   
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"   
  10.         android:textSize="10dip"/>  
  11.   
  12.     <TextView android:id="@+id/simple_list_item_3_tv2"  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"   
  15.         android:textSize="18dip"/>  
  16.       
  17.     <TextView android:id="@+id/simple_list_item_3_tv3"  
  18.         android:layout_width="wrap_content"  
  19.         android:layout_height="wrap_content"   
  20.         android:textSize="12dip"/>  
  21. </LinearLayout>  

3)ThreeRowsListActivity.java

Java代码 
  1. public class ThreeRowsListActivity extends Activity {  
  2.     protected void onCreate(Bundle savedInstanceState) {  
  3.         super.onCreate(savedInstanceState);  
  4.         setContentView(R.layout.hs_activity);  
  5.           
  6.         List<Map<String, Object>> data = new ArrayList<Map<String,Object>>();  
  7.         Map<String, Object> item = new HashMap<String, Object>();  
  8.         item.put("category""便民,超市");  
  9.         item.put("name""中百超市");  
  10.         item.put("address""武汉街道口2547号");  
  11.         data.add(item);  
  12.           
  13.         item = new HashMap<String, Object>();  
  14.         item.put("category""五星级酒店");  
  15.         item.put("name""希尔顿大酒店");  
  16.         item.put("address""上海宝山路3651号");  
  17.         data.add(item);  
  18.           
  19.         item = new HashMap<String, Object>();  
  20.         item.put("category""写字楼,办公");  
  21.         item.put("name""中环假日酒店");  
  22.         item.put("address""北京菜户营1号");  
  23.         data.add(item);  
  24.           
  25.           
  26.         ListView listView = (ListView) findViewById(R.id.hs_activity_lv);  
  27.         ListAdapter adapter = new SimpleAdapter(this, data, R.layout.simple_list_item_3,   
  28.                 new String[] {"category""name""address"},   
  29.                 new int[] {R.id.simple_list_item_3_tv1, R.id.simple_list_item_3_tv2, R.id.simple_list_item_3_tv3});  
  30.         listView.setAdapter(adapter);  
  31.           
  32.     }  
  33. }  

 

3. ListView with Icon

(1)How to get android default icons

1)http://www.darshancomputing.com/android/1.5-drawables.html

2)android.jar

(2)Classic Code

1)main.xml

Xml代码 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent">   
  6.     <ListView android:id="@id/android:list"  
  7.         android:layout_width="fill_parent"  
  8.         android:layout_height="fill_parent"  
  9.         android:layout_weight="1"  
  10.         android:drawSelectorOnTop="false"  
  11.     />  
  12. </LinearLayout>  

 2)row.xml

Xml代码 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:id="@+id/vw1"  
  5.     android:layout_width="fill_parent"  
  6.     android:layout_height="wrap_content"  
  7.     android:orientation="horizontal">      
  8.   
  9.     <ImageView android:id="@+id/img"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.     />  
  13.   
  14.     <LinearLayout  
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="wrap_content"  
  17.         android:orientation="vertical">  
  18.         <TextView android:id="@+id/text1"  
  19.             android:textSize="12sp"  
  20.             android:textStyle="bold"  
  21.             android:layout_width="fill_parent"  
  22.             android:layout_height="wrap_content"/>  
  23.         <TextView android:id="@+id/text2"  
  24.             android:textSize="12sp"  
  25.             android:layout_width="fill_parent"  
  26.             android:layout_height="wrap_content"  
  27.     />  
  28.     </LinearLayout>  
  29.   
  30. </LinearLayout>  

 3)ResourceExplorer.java

Java代码 
  1. public class ResourceExplorer extends ListActivity {  
  2.     public void onCreate(Bundle savedInstanceState) {  
  3.         super.onCreate(savedInstanceState);  
  4.         setContentView(R.layout.main);  
  5.   
  6.         // define the list which holds the information of the list  
  7.         List<Map<String, Object>> resourceNames = new ArrayList<Map<String, Object>>();  
  8.   
  9.         // define the map which will hold the information for each row  
  10.         Map<String, Object> data;  
  11.   
  12.         // hard coded numbers retrieved from  
  13.         // http://code.google.com/android/reference/android/R.drawable.html  
  14.         for (int idx = 17301504; idx <= 17301655; idx++) {  
  15.             data = new HashMap<String, Object>();  
  16.             try {  
  17.                 String stg = Resources.getSystem().getResourceName(idx);  
  18.                 data.put("line1", stg);  
  19.                 data.put("line2", idx);  
  20.                 data.put("img", idx);  
  21.                 resourceNames.add(data);  
  22.             } catch (Resources.NotFoundException nfe) {  
  23.             }  
  24.         }  
  25.   
  26.         SimpleAdapter notes = new SimpleAdapter(this, resourceNames,  
  27.                 R.layout.row, new String[] { "line1""line2""img" },  
  28.                 new int[] { R.id.text1, R.id.text2, R.id.img });  
  29.         setListAdapter(notes);  
  30.     }  
  31. }  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值