[关闭]
@linux1s1s 2019-02-15T16:36:53.000000Z 字数 5131 阅读 4524

Android ListView 多种布局--初步

AndroidWidget 2015-07


提出问题

先看下面这个ListView

此处输入图片的描述

该如何实现上面这个需求?

解决问题

我们知道ListView数据绑定类BaseAdapter有个getItemViewType,这个方法完全满足上面的要求,下面写个简单的Demo实现上面的需求。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent">
  6. <ListView
  7. android:layout_width="match_parent"
  8. android:layout_height="match_parent"
  9. android:id="@+id/list_view"
  10. android:divider="@color/list_view_divider_color"
  11. android:dividerHeight="1dp" />
  12. </LinearLayout>
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:orientation="vertical"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent">
  7. <TextView
  8. android:id="@+id/item_list_view_type_title"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:layout_gravity="center_vertical"
  12. android:gravity="center"
  13. android:textColor="@android:color/black"
  14. android:textSize="@dimen/item_list_view_type_title_size"
  15. android:padding="8dp"
  16. tools:ignore="RtlHardcoded,RtlSymmetry" />
  17. </LinearLayout>
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <TextView xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="wrap_content"
  5. android:layout_height="wrap_content"
  6. android:gravity="center"
  7. android:textColor="@android:color/black"
  8. android:textSize="@dimen/item_list_view_type_title_size"
  9. android:padding="8dp"
  10. android:drawablePadding="10dp"
  11. android:drawableRight="@mipmap/ic_launcher"
  12. android:id="@+id/item_list_view_type_content"
  13. tools:ignore="RtlHardcoded">
  14. </TextView>
  1. import android.app.Activity;
  2. import android.os.Bundle;
  3. import android.view.LayoutInflater;
  4. import android.view.View;
  5. import android.view.ViewGroup;
  6. import android.widget.BaseAdapter;
  7. import android.widget.ListView;
  8. import android.widget.TextView;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. public class ListViewTypeActivity extends Activity
  12. {
  13. @Override
  14. protected void onCreate(Bundle savedInstanceState)
  15. {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.activity_listview_type);
  18. initView();
  19. }
  20. private void initView()
  21. {
  22. ListView listView = (ListView) findViewById(R.id.list_view);
  23. List<ItemFeed> data = new ArrayList<>();
  24. for (int i = 0; i < 20; i++)
  25. {
  26. ItemFeed feed = new ItemFeed();
  27. if (i % 5 == 0)
  28. {
  29. feed.setItemType(ItemType.TITLE);
  30. feed.setText("TypeListView_Item: Title" + i);
  31. }
  32. else
  33. {
  34. feed.setItemType(ItemType.CONTENT);
  35. feed.setText("TypeListView_Item: Content" + i);
  36. }
  37. data.add(feed);
  38. }
  39. BaseAdapter listAdapter = new TypedListAdapter(data);
  40. listView.setAdapter(listAdapter);
  41. }
  42. private class TypedListAdapter extends BaseAdapter
  43. {
  44. private List<ItemFeed> data;
  45. public TypedListAdapter(List<ItemFeed> datas)
  46. {
  47. this.data = datas;
  48. }
  49. @Override
  50. public int getViewTypeCount()
  51. {
  52. return 2;
  53. }
  54. @Override
  55. public int getItemViewType(int position)
  56. {
  57. final ItemFeed feed = data.get(position);
  58. if (feed != null)
  59. {
  60. return feed.getItemType().getValue();
  61. }
  62. return super.getItemViewType(position);
  63. }
  64. @Override
  65. public int getCount()
  66. {
  67. return data == null ? 0 : data.size();
  68. }
  69. @Override
  70. public Object getItem(int position)
  71. {
  72. return data == null ? null : data.get(position);
  73. }
  74. @Override
  75. public long getItemId(int position)
  76. {
  77. return position;
  78. }
  79. @Override
  80. public View getView(int position, View convertView, ViewGroup parent)
  81. {
  82. final ItemFeed feed = data.get(position);
  83. final LayoutInflater inflater = LayoutInflater.from(ListViewTypeActivity.this);
  84. ItemTitleHolder titleHolder;
  85. ItemContentHolder contentHolder;
  86. int type = getItemViewType(position);
  87. switch (type)
  88. {
  89. case 0:
  90. if (convertView == null)
  91. {
  92. convertView = inflater.inflate(R.layout.item_listview_type_title, parent, false);
  93. titleHolder = new ItemTitleHolder(convertView);
  94. convertView.setTag(titleHolder);
  95. }
  96. else
  97. {
  98. titleHolder = (ItemTitleHolder) convertView.getTag();
  99. }
  100. titleHolder.refreshUI(feed);
  101. break;
  102. case 1:
  103. if (convertView == null)
  104. {
  105. convertView = inflater.inflate(R.layout.item_listview_type_content, parent, false);
  106. contentHolder = new ItemContentHolder(convertView);
  107. convertView.setTag(contentHolder);
  108. }
  109. else
  110. {
  111. contentHolder = (ItemContentHolder) convertView.getTag();
  112. }
  113. contentHolder.refreshUI(feed);
  114. break;
  115. }
  116. return convertView;
  117. }
  118. }
  119. private class ItemTitleHolder
  120. {
  121. private TextView title;
  122. public ItemTitleHolder(View convertView)
  123. {
  124. if (convertView != null)
  125. {
  126. title = (TextView) convertView.findViewById(R.id.item_list_view_type_title);
  127. }
  128. }
  129. public void refreshUI(ItemFeed feed)
  130. {
  131. if (title != null && feed != null)
  132. {
  133. title.setText(feed.getText());
  134. }
  135. }
  136. }
  137. private class ItemContentHolder
  138. {
  139. private TextView content;
  140. public ItemContentHolder(final View convertView)
  141. {
  142. if (convertView != null)
  143. {
  144. content = (TextView) convertView.findViewById(R.id.item_list_view_type_content);
  145. }
  146. }
  147. public void refreshUI(ItemFeed feed)
  148. {
  149. if (content != null && feed != null)
  150. {
  151. content.setText(feed.getText());
  152. }
  153. }
  154. }
  155. private class ItemFeed
  156. {
  157. public ItemType getItemType()
  158. {
  159. return itemType;
  160. }
  161. public void setItemType(ItemType itemType)
  162. {
  163. this.itemType = itemType;
  164. }
  165. public String getText()
  166. {
  167. return text;
  168. }
  169. public void setText(String text)
  170. {
  171. this.text = text;
  172. }
  173. private ItemType itemType;
  174. private String text;
  175. }
  176. private enum ItemType
  177. {
  178. TITLE(0),
  179. CONTENT(1);
  180. public int getValue()
  181. {
  182. return value;
  183. }
  184. private int value;
  185. ItemType(int value)
  186. {
  187. this.value = value;
  188. }
  189. }
  190. }

运行结果

此处输入图片的描述

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注