Android ListView顯示不同樣式的item


先look圖

我們再使用listview時,listview的item大多時候都是一種樣式,在很多app中也很常見,但有時候根據需求,可能數據的數量不一樣,同個類型的數據顯示的位置不同,亦或者有的item需要圖片,有的不需要,但是這些又必須在同一個listview中顯示,這時我們就需要在listview中顯示多種樣式的item,首先我們需要考慮的是如何將不同數量的數據裝載到ArrayList<~>中呢,先看看下面的listViewItem,。

 1 package com.example.keranbin.myapplication;
 2 
 3 import java.util.HashMap;
 4 import java.util.Map;
 5 
 6 public class lIstViewItem
 7 {
 8     //用於區分listview顯示的不同item,告訴適配器我這是什么類型,listview適配器根據type決定怎么顯示
 9     public int type;
10     //將要顯示的數據用HashMap包裝好
11     public HashMap<String,Object> map ;
12     
13     public lIstViewItem(int type, HashMap<String, Object> map)
14     {
15         this.type = type;
16         this.map = map;
17     }
18 }

我們通過自定義一個listItem,即可將所有不同類型,不同數量的數據先組裝成統一類型listItem即可,然后用arrayList.add(listitem)即可。

 1  /**
 2      * 這里我們用三種不同的樣式進行測試
 3      **/
 4     private ArrayList<lIstViewItem> getDatas() {
 5 
 6         viewItemsArraylists = new ArrayList<lIstViewItem>();
 7         viewItemsArraylists.add(new lIstViewItem(2, getHashMapThreeType("汪星人", "汪星人喜歡吃骨頭", "2015-10-18")));
 8         viewItemsArraylists.add(new lIstViewItem(1, getHashMapSecondType("喵星人", "喵星喜歡吃魚")));
 9         viewItemsArraylists.add(new lIstViewItem(0, getHashMapFirstType("猴子")));
10         viewItemsArraylists.add(new lIstViewItem(0, getHashMapFirstType("老虎")));
11         viewItemsArraylists.add(new lIstViewItem(1, getHashMapSecondType("老母雞", "老母雞喜歡吃蟲子")));
12         return viewItemsArraylists;
13     }
14 
15     
16     //第一種樣式,只傳輸一個數據
17     private HashMap<String, Object> getHashMapFirstType(String firstTheme) {
18         HashMap<String, Object> hashMap = new HashMap<String, Object>();
19         hashMap.put("Theme", firstTheme);
20         return hashMap;
21     }
22 
23 
24     //第二種樣式,傳輸兩個數據
25     private HashMap<String, Object> getHashMapSecondType(String secondTheme, String secondContent) {
26         HashMap<String, Object> hashMap = new HashMap<String, Object>();
27         hashMap.put("Theme", secondTheme);
28         hashMap.put("Content", secondContent);
29         return hashMap;
30     }
31 
32     //第三種樣式,傳輸三個數據
33     private HashMap<String, Object> getHashMapThreeType(String threeTheme, String threeContent, String date) {
34         HashMap<String, Object> hashMap = new HashMap<String, Object>();
35         hashMap.put("Theme", threeTheme);
36         hashMap.put("Content", threeContent);
37         hashMap.put("Date", date);
38         return hashMap;
39     }

剩下的就是listViewAdapter的事情啦,和顯示一種樣式的listViewAdapter不同的一點是我們重寫實現父類baseAdapter的兩個方法。

 1   //返回當前布局的樣式type
 2     @Override
 3     public int getItemViewType(int position) {
 4         return listDatas.get(position).type;
 5     }
 6 
 7     //返回你有多少個不同的布局
 8     @Override
 9     public int getViewTypeCount() {
10         return 3;
11     }

然后在getView中根據需要進行判斷決定顯示那種樣式即可

  1 package com.example.keranbin.myapplication;
  2 
  3 import android.content.Context;
  4 import android.view.LayoutInflater;
  5 import android.view.View;
  6 import android.view.ViewGroup;
  7 import android.widget.BaseAdapter;
  8 import android.widget.Button;
  9 import android.widget.EditText;
 10 import android.widget.ImageView;
 11 import android.widget.TextView;
 12 
 13 import java.util.ArrayList;
 14 
 15 /**
 16  * Created by keranbin on 2015/10/13.
 17  */
 18 public class ListViewAdapter extends BaseAdapter {
 19     private LayoutInflater mLayoutInflater;
 20     private Context context;
 21     private ArrayList<lIstViewItem> listDatas;
 22 
 23 
 24     public ListViewAdapter(Context context, ArrayList<lIstViewItem> listDatas) {
 25         this.listDatas = listDatas;
 26         mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 27     }
 28 
 29     //返回當前布局的樣式type
 30     @Override
 31     public int getItemViewType(int position) {
 32         return listDatas.get(position).type;
 33     }
 34 
 35     //返回你有多少個不同的布局
 36     @Override
 37     public int getViewTypeCount() {
 38         return 3;
 39     }
 40 
 41     @Override
 42     public int getCount() {
 43         return listDatas.size();
 44     }
 45 
 46     @Override
 47     public Object getItem(int position) {
 48         return listDatas.get(position);
 49     }
 50 
 51     @Override
 52     public long getItemId(int position) {
 53         return position;
 54     }
 55 
 56     @Override
 57     public View getView(int position, View convertView, ViewGroup parent) {
 58         lIstViewItem listItem = listDatas.get(position);
 59         int Type = getItemViewType(position);
 60         ViewHolderfirstType viewHolderfirstType = null;
 61         ViewHoldersecondType viewHoldersecondType = null;
 62         ViewHolderThreeType viewHolderThreeType = null;
 63         if (convertView == null) {
 64             switch (Type) {
 65                 case 0:
 66                     viewHolderfirstType = new ViewHolderfirstType();
 67                     convertView = mLayoutInflater.inflate(R.layout.activity_first_type_item, null);
 68                     viewHolderfirstType.tv_first_theme = (TextView) convertView.findViewById(R.id.tv_first_theme);
 69                     viewHolderfirstType.tv_first_theme.setText(listItem.map.get("Theme").toString());
 70 //                  convertView.setTag(viewHolderfirstType);  //如果要復用,不同於一種樣式的listView ,這樣的寫法是錯誤的,具體原因看下面第一個小哥評論的說法
 71                     convertView.setTag(R.id.viewHolderfirstType, viewHolderfirstType);
 72                     break;
 73                 case 1:
 74                     viewHoldersecondType = new ViewHoldersecondType();
 75                     convertView = mLayoutInflater.inflate(R.layout.activity_second_type_item, null);
 76                     viewHoldersecondType.tv_second_content = (TextView) convertView.findViewById(R.id.tv_second_content);
 77                     viewHoldersecondType.btn_second_theme = (Button) convertView.findViewById(R.id.btn_second_theme);
 78                     viewHoldersecondType.tv_second_content.setText(listItem.map.get("Theme").toString());
 79                     viewHoldersecondType.btn_second_theme.setText(listItem.map.get("Content").toString());
 80 //                    convertView.setTag(viewHoldersecondType);  //如果要復用,不同於一種樣式的listView ,這樣的寫法是錯誤的,具體原因看下面第一個小哥評論的說法
 81                     convertView.setTag(R.id.viewHoldersecondType, viewHoldersecondType);
 82                     break;
 83                 case 2:
 84 
 85                     viewHolderThreeType = new ViewHolderThreeType();
 86                     convertView = mLayoutInflater.inflate(R.layout.activity_three_type_item, null);
 87                     viewHolderThreeType.tv_three_content = (TextView) convertView.findViewById(R.id.tv_three_content);
 88                     viewHolderThreeType.et_three_theme = (EditText) convertView.findViewById(R.id.et_three_theme);
 89                     viewHolderThreeType.tv_three_time = (TextView) convertView.findViewById(R.id.tv_three_time);
 90                     viewHolderThreeType.et_three_theme.setText(listItem.map.get("Theme").toString());
 91                     viewHolderThreeType.tv_three_content.setText(listItem.map.get("Content").toString());
 92                     viewHolderThreeType.tv_three_time.setText(listItem.map.get("Date").toString());
 93 //                    convertView.setTag(viewHolderThreeType);//如果要復用,不同於一種樣式的listView ,這樣的寫法是錯誤的,具體原因看下面第一個小哥評論的說法
 94                     convertView.setTag(R.id.viewHolderthreeType, viewHolderThreeType);
 95                     break;
 96             }
 97         } else {
 98             switch (Type) {
 99                 case 0:
100                     viewHolderfirstType = (ViewHolderfirstType) convertView.getTag(R.id.viewHolderfirstType);
101                     viewHolderfirstType.tv_first_theme.setText(listItem.map.get("Theme").toString());
102                     break;
103                 case 1:
104                     viewHoldersecondType = (ViewHoldersecondType) convertView.getTag(R.id.viewHoldersecondType);
105                     viewHoldersecondType.tv_second_content = (TextView) convertView.findViewById(R.id.tv_second_content);
106                     viewHoldersecondType.btn_second_theme = (Button) convertView.findViewById(R.id.btn_second_theme);
107                     viewHoldersecondType.tv_second_content.setText(listItem.map.get("Theme").toString());
108                     viewHoldersecondType.btn_second_theme.setText(listItem.map.get("Content").toString());
109                     break;
110                 case 2:
111                     viewHolderThreeType = (ViewHolderThreeType) convertView.getTag(R.id.viewHolderthreeType);
112                     viewHolderThreeType.tv_three_content = (TextView) convertView.findViewById(R.id.tv_three_content);
113                     viewHolderThreeType.et_three_theme = (EditText) convertView.findViewById(R.id.et_three_theme);
114                     viewHolderThreeType.tv_three_time = (TextView) convertView.findViewById(R.id.tv_three_time);
115                     viewHolderThreeType.et_three_theme.setText(listItem.map.get("Theme").toString());
116                     viewHolderThreeType.tv_three_content.setText(listItem.map.get("Content").toString());
117                     viewHolderThreeType.tv_three_time.setText(listItem.map.get("Date").toString());
118                     break;
119             }
120 
121 
122         }
123         return convertView;
124     }
125 
126 
127     class ViewHolderfirstType {
128         TextView tv_first_theme;
129     }
130 
131     class ViewHoldersecondType {
132         TextView tv_second_content;
133         Button btn_second_theme;
134     }
135 
136     class ViewHolderThreeType {
137         EditText et_three_theme;
138         TextView tv_three_content;
139         TextView tv_three_time;
140     }
141 }

 

第一種樣式頁面組件主要是一個TextView.

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     >
 6 
 7     <RelativeLayout
 8         android:layout_width="fill_parent"
 9         android:layout_height="100dp"
10         android:background="#454346">
11 
12         <TextView
13             android:id="@+id/tv_first_theme"
14             android:layout_width="wrap_content"
15             android:layout_height="wrap_content"
16             android:layout_centerHorizontal="true"
17             android:layout_centerVertical="true"
18             android:textSize="30dp"
19             android:textStyle="bold" />
20 
21     </RelativeLayout>
22 </RelativeLayout>

第二種樣式頁面組件主要是一個TextView和一個button.

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent">
 5 
 6     <RelativeLayout
 7         android:layout_width="fill_parent"
 8         android:layout_height="100dp"
 9         android:background="#834385">
10 
11         <Button
12             android:id="@+id/btn_second_theme"
13             android:layout_width="wrap_content"
14             android:layout_height="wrap_content"
15             android:textSize="30dp" />
16 
17         <TextView
18             android:id="@+id/tv_second_content"
19             android:layout_width="wrap_content"
20             android:layout_height="wrap_content"
21             android:layout_alignParentEnd="true"
22             android:layout_alignParentRight="true"
23             android:layout_centerVertical="true"
24             android:textSize="30dp" />
25     </RelativeLayout>
26 </RelativeLayout>

第三種樣式頁面組件主要是兩個TextView和一個EditText.

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent">
 5 
 6     <RelativeLayout
 7         android:layout_width="fill_parent"
 8         android:layout_height="100dp"
 9         android:background="#6434ff">
10 
11         <EditText
12             android:id="@+id/et_three_theme"
13             android:layout_width="200dp"
14             android:layout_height="wrap_content"
15             android:layout_alignParentEnd="true"
16             android:layout_alignParentRight="true"
17             android:textSize="30dp" />
18 
19         <TextView
20             android:id="@+id/tv_three_time"
21             android:layout_width="wrap_content"
22             android:layout_height="wrap_content"
23             android:layout_alignParentLeft="true"
24             android:layout_alignParentStart="true"
25             android:layout_centerVertical="true"
26             android:textSize="30dp" />
27 
28         <TextView
29             android:id="@+id/tv_three_content"
30             android:layout_width="wrap_content"
31             android:layout_height="wrap_content"
32             android:layout_alignEnd="@+id/et_three_theme"
33             android:layout_alignParentBottom="true"
34             android:layout_alignRight="@+id/et_three_theme"
35             android:textSize="20dp" />
36 
37 
38     </RelativeLayout>
39 
40 </RelativeLayout>

activity_main.xml文件非常簡單,就一個listView。

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:background="#ffffff">
 6 
 7     <ListView
 8         android:id="@+id/listView"
 9         android:layout_width="fill_parent"
10         android:layout_height="fill_parent"></ListView>
11 </RelativeLayout>

下面是MainActivity的代碼

 1 package com.example.keranbin.myapplication;
 2 
 3 import android.app.Activity;
 4 import android.os.Bundle;
 5 import android.widget.ListView;
 6 
 7 import java.util.ArrayList;
 8 import java.util.HashMap;
 9 
10 public class MainActivity extends Activity {
11     private ListView listView;                               //頁面listview
12     private ListViewAdapter listViewAdapter;                 //listview適配器
13     private ArrayList<lIstViewItem> viewItemsArraylists;     //Arraylist主要裝載的是傳給適配器的數據集合
14 
15     @Override
16     protected void onCreate(Bundle savedInstanceState) {
17         super.onCreate(savedInstanceState);
18         setContentView(R.layout.activity_main);
19 
20         //初始化頁面組件及一些數據
21         initView();
22         //為listview設置適配器
23         ListViewAdapter listViewAdapter = new ListViewAdapter(MainActivity.this, getDatas());
24         listView.setAdapter(listViewAdapter);
25     }
26 
27     //初始化頁面組件及一些數據
28     private void initView() {
29         listView = (ListView) this.findViewById(R.id.listView);
30         listViewAdapter = new ListViewAdapter(MainActivity.this, getDatas());
31     }
32 
33 
34     /**
35      * 這里我們用三種不同的樣式進行測試
36      **/
37     private ArrayList<lIstViewItem> getDatas() {
38 
39         viewItemsArraylists = new ArrayList<lIstViewItem>();
40         viewItemsArraylists.add(new lIstViewItem(2, getHashMapThreeType("汪星人", "汪星人喜歡吃骨頭", "2015-10-18")));
41         viewItemsArraylists.add(new lIstViewItem(1, getHashMapSecondType("喵星人", "喵星喜歡吃魚")));
42         viewItemsArraylists.add(new lIstViewItem(0, getHashMapFirstType("猴子")));
43         viewItemsArraylists.add(new lIstViewItem(0, getHashMapFirstType("老虎")));
44         viewItemsArraylists.add(new lIstViewItem(1, getHashMapSecondType("老母雞", "老母雞喜歡吃蟲子")));
45         return viewItemsArraylists;
46     }
47 
48 
49     //第一種樣式,只傳輸一個數據
50     private HashMap<String, Object> getHashMapFirstType(String firstTheme) {
51         HashMap<String, Object> hashMap = new HashMap<String, Object>();
52         hashMap.put("Theme", firstTheme);
53         return hashMap;
54     }
55 
56 
57     //第二種樣式,傳輸兩個數據
58     private HashMap<String, Object> getHashMapSecondType(String secondTheme, String secondContent) {
59         HashMap<String, Object> hashMap = new HashMap<String, Object>();
60         hashMap.put("Theme", secondTheme);
61         hashMap.put("Content", secondContent);
62         return hashMap;
63     }
64 
65     //第三種樣式,傳輸三個數據
66     private HashMap<String, Object> getHashMapThreeType(String threeTheme, String threeContent, String date) {
67         HashMap<String, Object> hashMap = new HashMap<String, Object>();
68         hashMap.put("Theme", threeTheme);
69         hashMap.put("Content", threeContent);
70         hashMap.put("Date", date);
71         return hashMap;
72     }
73 
74 
75 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM