Android中自定义ListView

 

Android中自定义ListView

分类: Android组件   7677人阅读  评论(5)  收藏  举报

1. 自定义ListView,效果图:

    自定义ListView

2. 代码实现:

    (1)res/layout/main.xml实现:

[java:firstline[1]]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:orientation="vertical"  
  5.     android:layout_width="fill_parent"  
  6.     android:layout_height="fill_parent"  
  7.     >  
  8.       
  9.     <com.focus.fishme.SelfListView  
  10.         android:id="@+id/ListView"  
  11.         android:orientation="vertical"   
  12.         android:layout_width="fill_parent"  
  13.         android:layout_height="fill_parent"   
  14.         />  
  15.       
  16. </LinearLayout>  

    (2)ListView的Item布局实现:

[java:firstline[1]]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <LinearLayout   
  4.     xmlns:android = "http://schemas.android.com/apk/res/android"  
  5.     android:orientation = "vertical"   
  6.     android:layout_width = "fill_parent"  
  7.     android:layout_height = "fill_parent"  
  8.     >  
  9.   
  10.     <TextView   
  11.         android:id = "@+id/TextViewOne"  
  12.         android:layout_width = "wrap_content"   
  13.         android:layout_height = "wrap_content"  
  14.         android:layout_marginLeft = "10dip"   
  15.         android:textSize  =  "24sp"  
  16.         />  
  17.       
  18.     <TextView   
  19.         android:id = "@+id/TextViewTwo"   
  20.         android:layout_width = "wrap_content"  
  21.         android:layout_height = "wrap_content"  
  22.         android:layout_marginLeft = "10dip"   
  23.         />  
  24.       
  25.     <View   
  26.         android:layout_height = "1dip"   
  27.         android:layout_width = "fill_parent"  
  28.         android:background = "#FF0000"  
  29.         />  
  30.   
  31. </LinearLayout>  

    (3)主Activity实现:

[delphi:firstline[1]]  view plain copy
  1. package com.focus.fishme;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.HashMap;  
  5. import java.util.List;  
  6. import java.util.Map;  
  7.   
  8. import android.app.Activity;  
  9. import android.os.Bundle;  
  10.   
  11. public class SelfListViewActivity extends Activity { 
  12.      
  13.     @Override 
  14.     public void onCreate(Bundle savedInstanceState) { 
  15.         super.onCreate(savedInstanceState); 
  16.          
  17.         /** 
  18.          * 设置主布局。 
  19.          */ 
  20.         setContentView(R.layout.main); 
  21.          
  22.         /** 
  23.          * ListView数据集。 
  24.          */ 
  25.         List<Map<String , Object>> mSelfData = new ArrayList<Map<String,Object>>(); 
  26.          
  27.         /** 
  28.          * 获取ListView组件。 
  29.          */ 
  30.         SelfListView mSelfListView = (SelfListView) findViewById(R.id.ListView); 
  31.          
  32.         /** 
  33.          * 生成数据。 
  34.          */ 
  35.         for (int i = 0; i < 10; i++) { 
  36.             HashMap<String, Object> mMap = new HashMap<String, Object>(); 
  37.             mMap.put("key_name", "name" + i); 
  38.             mMap.put("value_name", "value" + i); 
  39.             mSelfData.add(mMap); 
  40.         }  
  41.   
  42.         /**  
  43.          * 自定义Adapter。  
  44.          */  
  45.         final SelfAdapter mSelfAdapter = new SelfAdapter(this, mSelfData,   
  46.                 R.layout.item, new String[] { "key_name", "value_name" }, new int[] { R.id.TextViewOne, R.id.TextViewTwo });  
  47.           
  48.         /**  
  49.          * 向ListView设置Adapter。  
  50.          */  
  51.         mSelfListView.setSelfAdapter(mSelfAdapter);  
  52.     }  
  53.       
  54. }  

    (4)ListView所用Adapter实现:

[java:firstline[1]]  view plain copy
  1. package com.focus.fishme;  
  2.   
  3. import java.util.List;  
  4. import java.util.Map;  
  5.   
  6. import android.content.Context;  
  7. import android.view.LayoutInflater;  
  8. import android.view.View;  
  9. import android.view.ViewGroup;  
  10. import android.widget.BaseAdapter;  
  11. import android.widget.TextView;  
  12.   
  13. public class SelfAdapter extends BaseAdapter {  
  14.   
  15.     private LayoutInflater mLayoutInflater;  
  16.   
  17.     private int mResource;  
  18.     private List<? extends Map<String, ?>> mSelfData;  
  19.     private String[] from;  
  20.     private int[] to;  
  21.   
  22.     public SelfAdapter(Context context, List<? extends Map<String, ?>> data, int resouce, String[] from, int[] to) {  
  23.         this.mSelfData = data;  
  24.         this.mResource = resouce;  
  25.         this.mSelfData = data;  
  26.         this.from = from;  
  27.         this.to = to;  
  28.         this.mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  29.     }  
  30.   
  31.     public int getCount() {  
  32.         return mSelfData.size();  
  33.     }  
  34.   
  35.     public Object getItem(int position) {  
  36.         return mSelfData.get(position);  
  37.     }  
  38.   
  39.     public String get(int position, Object key) {  
  40.         Map<String, ?> map = (Map<String, ?>) getItem(position);  
  41.         return map.get(key).toString();  
  42.     }  
  43.   
  44.     public long getItemId(int position) {  
  45.         return position;  
  46.     }  
  47.   
  48.     /** 
  49.      * 生成ListView的Item布局。 
  50.      */  
  51.     public View getView(int position, View convertView, ViewGroup parent) {  
  52.         if (convertView == null) {  
  53.             convertView = mLayoutInflater.inflate(mResource, null);  
  54.         }  
  55.           
  56.         Map<String, ?> item = mSelfData.get(position);  
  57.           
  58.         int count = to.length;  
  59.           
  60.         for (int i = 0; i < count; i++) {  
  61.             View v = convertView.findViewById(to[i]);  
  62.             bindView(v, item, from[i]);  
  63.         }  
  64.           
  65.         convertView.setTag(position);  
  66.           
  67.         return convertView;  
  68.     }  
  69.   
  70.     private void bindView(View view, Map<String, ?> item, String from) {  
  71.         Object data = item.get(from);  
  72.           
  73.         if (view instanceof TextView) {  
  74.             ((TextView) view).setText(data == null ? "" : data.toString());  
  75.         }  
  76.     }  
  77.       
  78. }  

    (5)自定义ListView实现:

[java:firstline[1]]  view plain copy
  1. package com.focus.fishme;  
  2.   
  3. import android.content.Context;  
  4. import android.util.AttributeSet;  
  5. import android.view.View;  
  6. import android.widget.BaseAdapter;  
  7. import android.widget.LinearLayout;  
  8.   
  9. public class SelfListView extends LinearLayout {  
  10.   
  11.     private BaseAdapter mSelfAdapter;  
  12.       
  13.     public SelfListView(Context context) {  
  14.         super(context);  
  15.     }  
  16.   
  17.     public SelfListView(Context context, AttributeSet attributeSet) {  
  18.         super(context, attributeSet);  
  19.     }  
  20.   
  21.     /** 
  22.      * 删除ListView中上一次渲染的View,并添加新View。 
  23.      */  
  24.     private void buildList() {  
  25.         if (mSelfAdapter == null) {  
  26.               
  27.         }  
  28.           
  29.         if (getChildCount() > 0) {  
  30.             removeAllViews();  
  31.         }  
  32.           
  33.         int count = mSelfAdapter.getCount();  
  34.           
  35.         for(int i = 0 ; i < count ; i++) {  
  36.             View view = mSelfAdapter.getView(i, nullnull);  
  37.             if (view != null) {  
  38.                 addView(view, i);  
  39.             }  
  40.         }  
  41.     }  
  42.       
  43.     public BaseAdapter getSelfAdapter() {  
  44.         return mSelfAdapter;  
  45.     }  
  46.   
  47.     /** 
  48.      * 设置Adapter。 
  49.      *  
  50.      * @param selfAdapter 
  51.      */  
  52.     public void setSelfAdapter(BaseAdapter selfAdapter) {  
  53.         this.mSelfAdapter = selfAdapter;  
  54.         buildList();  
  55.     }  
  56.       
  57. }  

猜你喜欢

转载自endual.iteye.com/blog/1744972