風人園

          弱水三千,只取一瓢,便能解渴;佛法無邊,奉行一法,便能得益。
          隨筆 - 99, 文章 - 181, 評論 - 56, 引用 - 0
          數(shù)據(jù)加載中……

          Android 自定義ListView adapter(zt)

          http://daoshud1.iteye.com/blog/1874241

          本文講實現(xiàn)一個自定義列表的Android程序,程序將實現(xiàn)一個使用自定義的適配器(Adapter)綁定 
          數(shù)據(jù),通過contextView.setTag綁定數(shù)據(jù)有按鈕的ListView。 
          系統(tǒng)顯示列表(ListView)時,首先會實例化一個適配器,本文將實例化一個自定義的適配器。實現(xiàn) 
          自定義適配器,必須手動映射數(shù)據(jù),這時就需要重寫getView()方法,系統(tǒng)在繪制列表的每一行的時候 
          將調用此方法。 
          ListView在開始繪制的時候,系統(tǒng)自動調用getCount()函數(shù),根據(jù)函數(shù)返回值得到ListView的長度, 
          然后根據(jù)這個長度,調用getView()逐一畫出每一行。 
          具體使用方法可以參考下面代碼,只需記住Android自定義ListView三步驟: 
          第一步:準備主布局文件、組件布局文件等 
          第二步:獲取并整理數(shù)據(jù) 
          第三部:綁定數(shù)據(jù),這里我們是通過自己編寫Adapter類來完成的 
          1.首先新建一個list.XML 
          Java代碼  收藏代碼
          1. <?xml version="1.0" encoding="utf-8"?>  
          2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
          3.     android:layout_width="match_parent"  
          4.     android:layout_height="match_parent"  
          5.     android:orientation="vertical" >  
          6.     <LinearLayout android:layout_width="match_parent"  
          7.         android:layout_height="match_parent"  
          8.         android:orientation="horizontal" android:background="#f1e4f1">  
          9.         <ImageView   
          10.             android:id="@+id/image"  
          11.             android:layout_width="wrap_content"  
          12.             android:layout_height="wrap_content"/>  
          13.         <TextView   
          14.             android:id="@+id/title"  
          15.             android:layout_width="wrap_content"  
          16.             android:layout_height="wrap_content"  
          17.             android:textColor="#666872"/>  
          18.         <Button   
          19.             android:id="@+id/view"  
          20.             android:layout_width="wrap_content"  
          21.             android:layout_height="wrap_content"  
          22.             android:text="詳細"/>  
          23.     </LinearLayout>  
          24.     <TextView   
          25.         android:id="@+id/info"  
          26.         android:layout_width="wrap_content"  
          27.         android:layout_height="wrap_content"  
          28.         android:textColor="#666872"/>  
          29. </LinearLayout>  

          2、新建一個適配器類MyAdspter.java 
          Java代碼  收藏代碼
          1. public class MyAdspter extends BaseAdapter {  
          2.   
          3.     private List<Map<String, Object>> data;  
          4.     private LayoutInflater layoutInflater;  
          5.     private Context context;  
          6.     public MyAdspter(Context context,List<Map<String, Object>> data){  
          7.         this.context=context;  
          8.         this.data=data;  
          9.         this.layoutInflater=LayoutInflater.from(context);  
          10.     }  
          11.     /** 
          12.      * 組件集合,對應list.xml中的控件 
          13.      * @author Administrator 
          14.      */  
          15.     public final class Zujian{  
          16.         public ImageView image;  
          17.         public TextView title;  
          18.         public Button view;  
          19.         public TextView info;  
          20.     }  
          21.     @Override  
          22.     public int getCount() {  
          23.         return data.size();  
          24.     }  
          25.     /** 
          26.      * 獲得某一位置的數(shù)據(jù) 
          27.      */  
          28.     @Override  
          29.     public Object getItem(int position) {  
          30.         return data.get(position);  
          31.     }  
          32.     /** 
          33.      * 獲得唯一標識 
          34.      */  
          35.     @Override  
          36.     public long getItemId(int position) {  
          37.         return position;  
          38.     }  
          39.   
          40.     @Override  
          41.     public View getView(int position, View convertView, ViewGroup parent) {  
          42.         Zujian zujian=null;  
          43.         if(convertView==null){  
          44.             zujian=new Zujian();  
          45.             //獲得組件,實例化組件  
          46.             convertView=layoutInflater.inflate(R.layout.list, null);  
          47.             zujian.image=(ImageView)convertView.findViewById(R.id.image);  
          48.             zujian.title=(TextView)convertView.findViewById(R.id.title);  
          49.             zujian.view=(Button)convertView.findViewById(R.id.view);  
          50.             zujian.info=(TextView)convertView.findViewById(R.id.info);  
          51.             convertView.setTag(zujian);  
          52.         }else{  
          53.             zujian=(Zujian)convertView.getTag();  
          54.         }  
          55.         //綁定數(shù)據(jù)  
          56.         zujian.image.setBackgroundResource((Integer)data.get(position).get("image"));  
          57.         zujian.title.setText((String)data.get(position).get("title"));  
          58.         zujian.info.setText((String)data.get(position).get("info"));  
          59.         return convertView;  
          60.     }  
          61.   
          62. }  

          關于上面LayoutInflater的使用:在實際開發(fā)種LayoutInflater這個類還是非常有用的。它的作用類似 
          于 findViewById(),不同點是LayoutInflater是用來找layout下xml布局文件,并且會實例化!。 
          getView()的三個參數(shù):position表示將顯示的是第幾行,covertView是從布局文件中inflate來的布 
          局。我們用LayoutInflater的方法將定義好的list.xml文件提取成View實例用來顯示。然后將xml文件 
          中的各個組件實例化,這樣便可以將數(shù)據(jù)對應到各個組件上了。但是按鈕為了響應點擊事件,需要為 
          它添加點擊監(jiān)聽器,這樣就能捕獲點擊事件。 
          3、activity_main.xml中添加ListView控件 
          Java代碼  收藏代碼
          1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
          2.     xmlns:tools="http://schemas.android.com/tools"  
          3.     android:layout_width="match_parent"  
          4.     android:layout_height="match_parent"  
          5.     android:paddingBottom="@dimen/activity_vertical_margin"  
          6.     android:paddingLeft="@dimen/activity_horizontal_margin"  
          7.     android:paddingRight="@dimen/activity_horizontal_margin"  
          8.     android:paddingTop="@dimen/activity_vertical_margin"  
          9.     tools:context=".MainActivity" >  
          10.     <ListView   
          11.         android:id="@+id/list"  
          12.         android:layout_width="fill_parent"  
          13.         android:layout_height="fill_parent"></ListView>  
          14. </RelativeLayout>  

          4、在activity中調用ListView 
          Java代碼  收藏代碼
          1. public class MainActivity extends Activity {  
          2.   
          3.     private ListView listView=null;   
          4.     @Override  
          5.     protected void onCreate(Bundle savedInstanceState) {  
          6.         super.onCreate(savedInstanceState);  
          7.         setContentView(R.layout.activity_main);  
          8.         listView=(ListView)findViewById(R.id.list);  
          9.         List<Map<String, Object>> list=getData();  
          10.         listView.setAdapter(new MyAdspter(this, list));  
          11.     }  
          12.     @Override  
          13.     public boolean onCreateOptionsMenu(Menu menu) {  
          14.         getMenuInflater().inflate(R.menu.main, menu);  
          15.         return true;  
          16.     }  
          17.   
          18.     public List<Map<String, Object>> getData(){  
          19.         List<Map<String, Object>> list=new ArrayList<Map<String,Object>>();  
          20.         for (int i = 0; i < 10; i++) {  
          21.             Map<String, Object> map=new HashMap<String, Object>();  
          22.             map.put("image", R.drawable.ic_launcher);  
          23.             map.put("title""這是一個標題"+i);  
          24.             map.put("info""這是一個詳細信息"+i);  
          25.             list.add(map);  
          26.         }  
          27.         return list;  
          28.     }  
          29. }  


          posted on 2016-12-01 13:13 風人園 閱讀(177) 評論(0)  編輯  收藏 所屬分類: Android

          主站蜘蛛池模板: 九江县| 南涧| 隆德县| 崇义县| 土默特左旗| 望江县| 新余市| 越西县| 乳源| 泽州县| 卢氏县| 南木林县| 巢湖市| 全椒县| 宣恩县| 沅江市| 临夏县| 巫溪县| 安阳市| 桃源县| 湖北省| 京山县| 岑溪市| 扬州市| 兴海县| 道孚县| 绍兴市| 绥德县| 枣庄市| 凯里市| 浑源县| 宜章县| 贡觉县| 桐城市| 崇阳县| 广州市| 砚山县| 曲松县| 东城区| 丰镇市| 鹤峰县|