Android 自定義ListView示例詳解
本文講實(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
<?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; } }
以上就是對(duì)Android ListView 的簡(jiǎn)單實(shí)現(xiàn),有興趣的小伙伴可以參考下。
相關(guān)文章
Android App的運(yùn)行環(huán)境及Android系統(tǒng)架構(gòu)概覽
這篇文章主要介紹了Android App的運(yùn)行環(huán)境及Android系統(tǒng)架構(gòu)概覽,并對(duì)應(yīng)用程序進(jìn)程間隔離機(jī)制等知識(shí)點(diǎn)作了介紹,需要的朋友可以參考下2016-03-03ViewFlipper實(shí)現(xiàn)文字輪播效果
這篇文章主要為大家詳細(xì)介紹了ViewFlipper實(shí)現(xiàn)文字輪播效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08Android Studio 實(shí)現(xiàn)九宮格功能
這篇文章主要介紹了Android Studio 實(shí)現(xiàn)九宮格,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04Android入門(mén)之ScrollView的使用教程
我們經(jīng)??梢钥吹皆谑謾C(jī)里正在垂直加載一堆的數(shù)據(jù),然后過(guò)一會(huì)加載得內(nèi)容過(guò)多,到了手機(jī)的底部了,垂直方向就會(huì)出現(xiàn)一個(gè)“滾動(dòng)條”。本文就來(lái)通過(guò)一些示例和大家介紹下ScrollView(滾動(dòng)條)的使用,感興趣的可以了解一下2022-11-11Android BSearchEdit 搜索結(jié)果選擇框的實(shí)例代碼
EditText搜索結(jié)果下拉框、自動(dòng)or回調(diào)模式、可diy、使用超簡(jiǎn)便。這篇文章主要介紹了Android BSearchEdit 搜索結(jié)果選擇框的實(shí)例代碼,需要的朋友可以參考下2019-10-10鴻蒙開(kāi)源第三方組件之連續(xù)滾動(dòng)圖像組件功能
這篇文章主要介紹了鴻蒙開(kāi)源第三方組件之連續(xù)滾動(dòng)圖像組件功能,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04Android模擬器"Failed To Allocate memory 8"錯(cuò)誤如何解決
這篇文章主要介紹了Android模擬器"Failed To Allocate memory 8"錯(cuò)誤如何解決的相關(guān)資料,需要的朋友可以參考下2017-03-03