Android 自定義ListView adapter(zt)
http://daoshud1.iteye.com/blog/1874241
本文講實(shí)現(xiàn)一個(gè)自定義列表的Android程序,程序?qū)?shí)現(xiàn)一個(gè)使用自定義的適配器(Adapter)綁定
數(shù)據(jù),通過(guò)contextView.setTag綁定數(shù)據(jù)有按鈕的ListView。
系統(tǒng)顯示列表(ListView)時(shí),首先會(huì)實(shí)例化一個(gè)適配器,本文將實(shí)例化一個(gè)自定義的適配器。實(shí)現(xiàn)
自定義適配器,必須手動(dòng)映射數(shù)據(jù),這時(shí)就需要重寫(xiě)getView()方法,系統(tǒng)在繪制列表的每一行的時(shí)候
將調(diào)用此方法。
ListView在開(kāi)始繪制的時(shí)候,系統(tǒng)自動(dòng)調(diào)用getCount()函數(shù),根據(jù)函數(shù)返回值得到ListView的長(zhǎng)度,
然后根據(jù)這個(gè)長(zhǎng)度,調(diào)用getView()逐一畫(huà)出每一行。
具體使用方法可以參考下面代碼,只需記住Android自定義ListView三步驟:
第一步:準(zhǔn)備主布局文件、組件布局文件等
第二步:獲取并整理數(shù)據(jù)
第三部:綁定數(shù)據(jù),這里我們是通過(guò)自己編寫(xiě)Adapter類(lèi)來(lái)完成的
1.首先新建一個(gè)list.XML
2、新建一個(gè)適配器類(lèi)MyAdspter.java
關(guān)于上面LayoutInflater的使用:在實(shí)際開(kāi)發(fā)種LayoutInflater這個(gè)類(lèi)還是非常有用的。它的作用類(lèi)似
于 findViewById(),不同點(diǎn)是LayoutInflater是用來(lái)找layout下xml布局文件,并且會(huì)實(shí)例化!。
getView()的三個(gè)參數(shù):position表示將顯示的是第幾行,covertView是從布局文件中inflate來(lái)的布
局。我們用LayoutInflater的方法將定義好的list.xml文件提取成View實(shí)例用來(lái)顯示。然后將xml文件
中的各個(gè)組件實(shí)例化,這樣便可以將數(shù)據(jù)對(duì)應(yīng)到各個(gè)組件上了。但是按鈕為了響應(yīng)點(diǎn)擊事件,需要為
它添加點(diǎn)擊監(jiān)聽(tīng)器,這樣就能捕獲點(diǎn)擊事件。
3、activity_main.xml中添加ListView控件
4、在activity中調(diào)用ListView

數(shù)據(jù),通過(guò)contextView.setTag綁定數(shù)據(jù)有按鈕的ListView。
系統(tǒng)顯示列表(ListView)時(shí),首先會(huì)實(shí)例化一個(gè)適配器,本文將實(shí)例化一個(gè)自定義的適配器。實(shí)現(xiàn)
自定義適配器,必須手動(dòng)映射數(shù)據(jù),這時(shí)就需要重寫(xiě)getView()方法,系統(tǒng)在繪制列表的每一行的時(shí)候
將調(diào)用此方法。
ListView在開(kāi)始繪制的時(shí)候,系統(tǒng)自動(dòng)調(diào)用getCount()函數(shù),根據(jù)函數(shù)返回值得到ListView的長(zhǎng)度,
然后根據(jù)這個(gè)長(zhǎng)度,調(diào)用getView()逐一畫(huà)出每一行。
具體使用方法可以參考下面代碼,只需記住Android自定義ListView三步驟:
第一步:準(zhǔn)備主布局文件、組件布局文件等
第二步:獲取并整理數(shù)據(jù)
第三部:綁定數(shù)據(jù),這里我們是通過(guò)自己編寫(xiě)Adapter類(lèi)來(lái)完成的
1.首先新建一個(gè)list.XML
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <LinearLayout android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="horizontal" android:background="#f1e4f1">
- <ImageView
- android:id="@+id/image"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"/>
- <TextView
- android:id="@+id/title"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textColor="#666872"/>
- <Button
- android:id="@+id/view"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="詳細(xì)"/>
- </LinearLayout>
- <TextView
- android:id="@+id/info"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textColor="#666872"/>
- </LinearLayout>
2、新建一個(gè)適配器類(lèi)MyAdspter.java
- public class MyAdspter extends BaseAdapter {
- private List<Map<String, Object>> data;
- private LayoutInflater layoutInflater;
- private Context context;
- public MyAdspter(Context context,List<Map<String, Object>> data){
- this.context=context;
- this.data=data;
- this.layoutInflater=LayoutInflater.from(context);
- }
- /**
- * 組件集合,對(duì)應(yīng)list.xml中的控件
- * @author Administrator
- */
- public final class Zujian{
- public ImageView image;
- public TextView title;
- public Button view;
- public TextView info;
- }
- @Override
- public int getCount() {
- return data.size();
- }
- /**
- * 獲得某一位置的數(shù)據(jù)
- */
- @Override
- public Object getItem(int position) {
- return data.get(position);
- }
- /**
- * 獲得唯一標(biāo)識(shí)
- */
- @Override
- public long getItemId(int position) {
- return position;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- Zujian zujian=null;
- if(convertView==null){
- zujian=new Zujian();
- //獲得組件,實(shí)例化組件
- convertView=layoutInflater.inflate(R.layout.list, null);
- zujian.image=(ImageView)convertView.findViewById(R.id.image);
- zujian.title=(TextView)convertView.findViewById(R.id.title);
- zujian.view=(Button)convertView.findViewById(R.id.view);
- zujian.info=(TextView)convertView.findViewById(R.id.info);
- convertView.setTag(zujian);
- }else{
- zujian=(Zujian)convertView.getTag();
- }
- //綁定數(shù)據(jù)
- zujian.image.setBackgroundResource((Integer)data.get(position).get("image"));
- zujian.title.setText((String)data.get(position).get("title"));
- zujian.info.setText((String)data.get(position).get("info"));
- return convertView;
- }
- }
關(guān)于上面LayoutInflater的使用:在實(shí)際開(kāi)發(fā)種LayoutInflater這個(gè)類(lèi)還是非常有用的。它的作用類(lèi)似
于 findViewById(),不同點(diǎn)是LayoutInflater是用來(lái)找layout下xml布局文件,并且會(huì)實(shí)例化!。
getView()的三個(gè)參數(shù):position表示將顯示的是第幾行,covertView是從布局文件中inflate來(lái)的布
局。我們用LayoutInflater的方法將定義好的list.xml文件提取成View實(shí)例用來(lái)顯示。然后將xml文件
中的各個(gè)組件實(shí)例化,這樣便可以將數(shù)據(jù)對(duì)應(yīng)到各個(gè)組件上了。但是按鈕為了響應(yīng)點(diǎn)擊事件,需要為
它添加點(diǎn)擊監(jiān)聽(tīng)器,這樣就能捕獲點(diǎn)擊事件。
3、activity_main.xml中添加ListView控件
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- tools:context=".MainActivity" >
- <ListView
- android:id="@+id/list"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"></ListView>
- </RelativeLayout>
4、在activity中調(diào)用ListView
- public class MainActivity extends Activity {
- private ListView listView=null;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- listView=(ListView)findViewById(R.id.list);
- List<Map<String, Object>> list=getData();
- listView.setAdapter(new MyAdspter(this, list));
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- public List<Map<String, Object>> getData(){
- List<Map<String, Object>> list=new ArrayList<Map<String,Object>>();
- for (int i = 0; i < 10; i++) {
- Map<String, Object> map=new HashMap<String, Object>();
- map.put("image", R.drawable.ic_launcher);
- map.put("title", "這是一個(gè)標(biāo)題"+i);
- map.put("info", "這是一個(gè)詳細(xì)信息"+i);
- list.add(map);
- }
- return list;
- }
- }

posted on 2016-12-01 13:13 風(fēng)人園 閱讀(177) 評(píng)論(0) 編輯 收藏 所屬分類(lèi): Android