大家都知道在實(shí)際開發(fā)時(shí),對(duì)ListView的使用比較頻繁,其表現(xiàn)也非常復(fù)雜。本文將通過實(shí)例介紹ListView中多種item的實(shí)現(xiàn)方式,下面來一起看看吧。
使用ListView一般步驟:
getCount() , getItemId() , getItem() , getView()這個(gè)四個(gè)方法;如果實(shí)現(xiàn)ListView的多種類型item的顯示,那么就要再重寫兩個(gè)方法
getViewTypeCount() :得到總共item的顯示的種類數(shù),getItemViewType() :得到每個(gè)item顯示的類型;為整型數(shù)據(jù);實(shí)現(xiàn)的效果如下:

一、準(zhǔn)備填充的數(shù)據(jù)模型
1、解析json數(shù)據(jù)源
json數(shù)據(jù)放在res下的raw文件夾下:
[ { "letter": "A", "cities": [ "安慶", "安徽", "安全" ] }, { "letter": "B", "cities": [ "包頭", "寶鋼", "渤海", "本溪", "蚌埠" ] }, { "letter": "C", "cities": [ "長春", "長城", "長沙", "常州", "郴州", "重慶" ] }, { "letter": "D", "cities": [ "東莞", "東山", "大連", "大慶" ] }]2、建立數(shù)據(jù)對(duì)象
可以看到這個(gè)ListView有兩種類型,一個(gè)是顯示字母,一個(gè)是顯示內(nèi)容,所以數(shù)據(jù)模型的建立如下,使用int型的type對(duì)數(shù)據(jù)類型進(jìn)行標(biāo)識(shí);標(biāo)識(shí)的值必須從0開始計(jì)數(shù),有兩種類型,那么就取0,1這兩個(gè)值;
public class StringBean { String letter; String city; int type; public String getLetter() { return letter; } public void setLetter(String letter) { this.letter = letter; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public int getType() { return type; } public void setType(int type) { this.type = type; } @Override public String toString() { return "StringBean{" + "letter='" + letter + '/'' + ", city='" + city + '/'' + ", type=" + type + '}'; }}解析json數(shù)據(jù)填充成集合數(shù)據(jù)源這里就不提供了
二、準(zhǔn)備兩種item類型的布局文件
1、顯示字母的type_layout.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"> <TextView android:id="@+id/tvType" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#999" android:text="A" android:textSize="20sp" /></LinearLayout>
2、顯示城市city_layout.xml的布局文件
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/tvCity" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20sp" /></LinearLayout>
三、設(shè)置顯示ListView的數(shù)據(jù)和布局的適配器
這里的ListView的item有兩種類型,所以getViewTypeCount()返回2;
在getItemViewType()返回的是每次繪制每一個(gè)item的view顯示的是何種類型,在數(shù)據(jù)模型StringBean有設(shè)置;
關(guān)于類型的整型設(shè)置,可能有很多人認(rèn)為只要是任意的整型數(shù)字就可以了,其實(shí)不是這樣
item類型標(biāo)識(shí)值必須從0開始計(jì)數(shù),如果item有兩種類型,那么類型標(biāo)識(shí)值就是0,1
如果是不從0開始標(biāo)識(shí),那么會(huì)報(bào)ArrayIndexOutOfBoundsException數(shù)組下標(biāo)越界的異常
public class ListAdapter extends BaseAdapter { ArrayList<StringBean>list; Context context; LayoutInflater inflater; ListAdapter(ArrayList<StringBean>list,Context context){ this.list=list; this.context=context; inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public int getCount() { return list.size(); } @Override public Object getItem(int i) { return list.get(i); } @Override public long getItemId(int i) { return i; } @Override public View getView(int position, View converView, ViewGroup viewGroup) { View view=converView; StringBean bean=list.get(position); int type=bean.getType(); if(type==0){ if(view==null){ view=inflater.inflate(R.layout.type_layout,viewGroup,false); } TextView type_text= (TextView) view.findViewById(R.id.tvType); type_text.setText(bean.getLetter()); }else if (type==1){ if(converView==null){ view=inflater.inflate(R.layout.city_layout,viewGroup,false); } TextView city_text= (TextView) view.findViewById(R.id.tvCity); city_text.setText(bean.getCity()); } return view; } @Override public int getItemViewType(int i) { return list.get(i).getType(); } @Override public int getViewTypeCount() { return 2; }}四、設(shè)置ListView
ListView的布局文件,在這里就不給出了
public class MainActivity extends AppCompatActivity { ArrayList<StringBean> list; ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initBean(); initView(); } public void initBean(){ UserDao dao=new UserDao(this); list=dao.getList(); } public void initView(){ listView= (ListView) findViewById(R.id.listView); ListAdapter adapter=new ListAdapter(list,this); listView.setAdapter(adapter); }}總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。
新聞熱點(diǎn)
疑難解答
圖片精選