注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

东东的博客

江南烟雨,同大家一起分享

 
 
 

日志

 
 

自定义ListView  

2009-12-08 15:13:28|  分类: android相关 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

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>  
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="horizontal" 
 android:layout_width="fill_parent"
 android:layout_height="fill_parent">
 <ListView android:id="@+id/listview"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
 /> 
</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>  
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="horizontal" android:layout_width="wrap_content"
 android:layout_height="wrap_content">
 <ImageView android:id="@+id/simple_list_item_icon_iv" 
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:baselineAlignBottom="true">
 </ImageView>
 <TextView android:id="@+id/simple_list_item_icon_tv" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content"
  android:textSize="24dip">
 </TextView> 
</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. }  
public class IconSimpleAdapter extends BaseAdapter {
 private LayoutInflater mInflater;
 private List<Map<String, Object>> mList;
 private String[] mIndex;

 public IconSimpleAdapter(Context context, String[] index, List<Map<String, Object>> list) {
  mInflater = LayoutInflater.from(context);
  mList = list;
  mIndex = index;
 }

 @Override
 public int getCount() {
  return mList.size();
 }

 @Override
 public Object getItem(int position) {
  return mList.get(position);
 }

 @Override
 public long getItemId(int position) {
  return position;
 }

 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
  ViewHolder holder = null;
  if (convertView == null) {
   Bitmap mIcon = (Bitmap) mList.get(position).get(mIndex[0]);
   String mItem = " " + (String) mList.get(position).get(mIndex[1]);
   
   convertView = mInflater.inflate(R.layout.simple_list_item_icon, null);
   holder = new ViewHolder();
   holder.text = (TextView) convertView.findViewById(R.id.simple_list_item_icon_tv);
   holder.icon = (ImageView) convertView.findViewById(R.id.simple_list_item_icon_iv);
   holder.text.setText(mItem);
   holder.icon.setImageBitmap(mIcon);
   convertView.setTag(holder);
  } else {
   holder = (ViewHolder) convertView.getTag();
  }
  return convertView;
 }
 
 private class ViewHolder {
  TextView text;
  ImageView icon;
 }
}

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. }  
public class IconListActivity extends Activity {
 private final String ITEM_ICON = "icon";
 private final String ITEM_TEXT = "text";
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.listview);
  
  List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
  Map<String, Object> item = new HashMap<String, Object>();
  Bitmap bm1 = BitmapFactory.decodeResource(getResources(), R.drawable.a);
  item.put(ITEM_ICON, bm1);
  item.put(ITEM_TEXT, "中秋节");
  data.add(item);
  item = new HashMap<String, Object>();
  Bitmap bm2 = BitmapFactory.decodeResource(getResources(), R.drawable.b);
  item.put(ITEM_ICON, bm2);
  item.put(ITEM_TEXT, "端午节");
  data.add(item);
  item = new HashMap<String, Object>();
  Bitmap bm3 = BitmapFactory.decodeResource(getResources(), R.drawable.c);
  item.put(ITEM_ICON, bm3);
  item.put(ITEM_TEXT, "重阳节");
  data.add(item);
  item = new HashMap<String, Object>();
  Bitmap bm4 = BitmapFactory.decodeResource(getResources(), R.drawable.d);
  item.put(ITEM_ICON, bm4);
  item.put(ITEM_TEXT, "清明节");
  data.add(item);
  
  ListView lv = (ListView) findViewById(R.id.listview);
  IconSimpleAdapter adapter = new IconSimpleAdapter(this, new String[] {ITEM_ICON, ITEM_TEXT}, data);
  lv.setAdapter(adapter);
 }
}

 

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>  
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="horizontal" 
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:id="@+id/hs_linearlayout">
 <ListView android:id="@+id/hs_activity_lv"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
 />
</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>  
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" 
 android:layout_width="fill_parent"
 android:layout_height="fill_parent">
 
 <TextView android:id="@+id/simple_list_item_3_tv1" 
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" 
  android:textSize="10dip"/>

 <TextView android:id="@+id/simple_list_item_3_tv2"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" 
  android:textSize="18dip"/>
 
 <TextView android:id="@+id/simple_list_item_3_tv3"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" 
  android:textSize="12dip"/>
</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. }  
public class ThreeRowsListActivity extends Activity {
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.hs_activity);
  
  List<Map<String, Object>> data = new ArrayList<Map<String,Object>>();
  Map<String, Object> item = new HashMap<String, Object>();
  item.put("category", "便民,超市");
  item.put("name", "中百超市");
  item.put("address", "武汉街道口2547号");
  data.add(item);
  
  item = new HashMap<String, Object>();
  item.put("category", "五星级酒店");
  item.put("name", "希尔顿大酒店");
  item.put("address", "上海宝山路3651号");
  data.add(item);
  
  item = new HashMap<String, Object>();
  item.put("category", "写字楼,办公");
  item.put("name", "中环假日酒店");
  item.put("address", "北京菜户营1号");
  data.add(item);
  
  
  ListView listView = (ListView) findViewById(R.id.hs_activity_lv);
  ListAdapter adapter = new SimpleAdapter(this, data, R.layout.simple_list_item_3, 
    new String[] {"category", "name", "address"}, 
    new int[] {R.id.simple_list_item_3_tv1, R.id.simple_list_item_3_tv2, R.id.simple_list_item_3_tv3});
  listView.setAdapter(adapter);
  
 }
}

 

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>  
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"> 
    <ListView android:id="@id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
   android:drawSelectorOnTop="false"
  />
</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>  
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/vw1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">    

    <ImageView android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView android:id="@+id/text1"
            android:textSize="12sp"
            android:textStyle="bold"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>
        <TextView android:id="@+id/text2"
            android:textSize="12sp"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
    />
    </LinearLayout>

</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.     }  

转自:http://wayfarer.javaeye.com/blog/473369
  评论这张
 
阅读(2571)| 评论(1)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017