詳解ListView中多種item的實現(xiàn)方式
大家都知道在實際開發(fā)時,對ListView的使用比較頻繁,其表現(xiàn)也非常復雜。本文將通過實例介紹ListView中多種item的實現(xiàn)方式,下面來一起看看吧。
使用ListView一般步驟:
- 設置顯示的ListView,設置顯示的每一項item的view布局文件
- 設置每個item顯示的數(shù)據(jù)
- 將數(shù)據(jù)顯示的View中,繼承BaseAdapter,重寫
getCount(),getItemId(),getItem(),getView()這個四個方法;
如果實現(xiàn)ListView的多種類型item的顯示,那么就要再重寫兩個方法
getViewTypeCount():得到總共item的顯示的種類數(shù),getItemViewType():得到每個item顯示的類型;為整型數(shù)據(jù);
實現(xià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ù)對象
可以看到這個ListView有兩種類型,一個是顯示字母,一個是顯示內容,所以數(shù)據(jù)模型的建立如下,使用int型的type對數(shù)據(jù)類型進行標識;標識的值必須從0開始計數(shù),有兩種類型,那么就取0,1這兩個值;
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ù)源這里就不提供了
二、準備兩種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>
三、設置顯示ListView的數(shù)據(jù)和布局的適配器
這里的ListView的item有兩種類型,所以getViewTypeCount()返回2;
在getItemViewType()返回的是每次繪制每一個item的view顯示的是何種類型,在數(shù)據(jù)模型StringBean有設置;
關于類型的整型設置,可能有很多人認為只要是任意的整型數(shù)字就可以了,其實不是這樣
item類型標識值必須從0開始計數(shù),如果item有兩種類型,那么類型標識值就是0,1
如果是不從0開始標識,那么會報ArrayIndexOutOfBoundsException數(shù)組下標越界的異常
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;
}
}
四、設置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);
}
}
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。
相關文章
Android繼承ViewGroup實現(xiàn)Scroll滑動效果的方法示例
這篇文章主要介紹了Android繼承ViewGroup實現(xiàn)Scroll滑動效果的方法,結合實例形式分析了Android滑動效果的原理及擴展ViewGroup實現(xiàn)滑動功能的相關操作技巧,需要的朋友可以參考下2017-08-08
Android實現(xiàn)自定義帶文字和圖片Button的方法
這篇文章主要介紹了Android實現(xiàn)自定義帶文字和圖片Button的方法,涉及Android針對Button按鈕的布局與設計技巧,需要的朋友可以參考下2015-05-05
Android中wifi與數(shù)據(jù)流量的切換監(jiān)聽詳解
本文主要介紹了Android中wifi與數(shù)據(jù)流量的切換監(jiān)聽的方法步驟。具有一定的參考價值,下面跟著小編一起來看下吧2017-01-01
Android長按imageview把圖片保存到本地的實例代碼
本文通過代碼給大家介紹了Android長按imageview把圖片保存到本地的實現(xiàn)方法,代碼簡單易懂,非常不錯,具有參考借鑒價值,需要的朋友參考下吧2017-12-12
關于如何使用Flutter開發(fā)執(zhí)行操作系統(tǒng)shell命令的工具詳解
本文主要介紹如何在Flutter應用中開發(fā)一個Android終端命令行工具,包括終端命令行頁面的布局設計、與Shell通信的基本原理、輸入輸出處理的基本技巧等,以及如何在具體應用中利用終端命令行工具來執(zhí)行系統(tǒng)命令和與用戶進行交互2023-06-06
Android使用Retrofit2.0技術仿微信發(fā)說說
這篇文章主要為大家詳細介紹了Android使用Retrofit2.0技術仿微信發(fā)說說,實現(xiàn)拍照,選圖庫,多圖案上傳功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-01-01

