歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android中ListView的用法案例

Android中ListView的用法案例

日期:2017/3/1 10:47:42   编辑:Linux編程

ListView:

在Android開發中ListView是比較常用的組件,它以列表的形式展示具體內容,並且能夠根據數據的長度自適應顯示。抽空把對ListView的使用做了整理,並寫了個小例子,如下圖。

想實現列表的顯示,需要三個元素:

1.ListView 用來展示列表的數據,直觀來說就是一個存放數據行的容器,一般定義在布局文件中。(只有通過它才能把數據給顯示到屏幕上來)

2.適配器 用來把數據按照指定格式映射到ListView上得中介。(可以看做是ListView和數據之間連接的橋梁)

3.數據 具體的將被映射的字符串,圖片,組件等等。。。(不要把艷照映射上來喔。。。)

接下來,跟我這我一步步實現:

第一步:先創建一個布局文件,並且指定一個ListView,id為home_lv_msgList

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical"
  6. android:background="@drawable/send_content_bg"
  7. >
  8. <!-- 標題欄部分 -->
  9. <RelativeLayout
  10. android:layout_width="fill_parent"
  11. android:layout_height="wrap_content"
  12. android:background="@drawable/send_title_bg"
  13. >
  14. <ImageButton
  15. android:id="@+id/home_iv_edit"
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:background="@drawable/btn_edit"
  19. android:layout_alignParentLeft="true"
  20. android:layout_centerVertical="true"
  21. android:layout_marginLeft="10dip"
  22. />
  23. <TextView
  24. android:id="@+id/home_tv_showName"
  25. android:layout_width="wrap_content"
  26. android:layout_height="wrap_content"
  27. android:text="12345"
  28. android:textColor="#343434"
  29. android:textSize="20sp"
  30. android:layout_centerInParent="true"
  31. />
  32. <ImageButton
  33. android:id="@+id/home_btn_refreshBtn"
  34. android:layout_width="wrap_content"
  35. android:layout_height="wrap_content"
  36. android:layout_alignParentRight="true"
  37. android:layout_centerVertical="true"
  38. android:layout_marginRight="10dip"
  39. android:background="@drawable/btn_refresh"
  40. />
  41. </RelativeLayout>
  42. <RelativeLayout
  43. android:layout_width="fill_parent"
  44. android:layout_height="fill_parent"
  45. >
  46. <!-- 微博信息展示部分 -->
  47. <ListView
  48. android:id="@+id/home_lv_msgList"
  49. android:layout_width="fill_parent"
  50. android:layout_height="fill_parent"
  51. android:divider="@drawable/divider"
  52. android:dividerHeight="2dip"
  53. android:background="#BBFFFFFF"
  54. android:cacheColorHint="#00000000"
  55. android:fastScrollEnabled="true"
  56. android:focusable="true"
  57. android:layout_margin="0dip"
  58. android:layout_above="@+id/home_menuLayout"
  59. >
  60. </ListView>
  61. <!-- 進度條 -->
  62. <LinearLayout
  63. android:id="@+id/home_loadLayout"
  64. android:layout_width="wrap_content"
  65. android:layout_height="wrap_content"
  66. android:orientation="vertical"
  67. android:visibility="invisible"
  68. android:layout_centerInParent="true"
  69. >
  70. <ProgressBar
  71. android:id="@+id/home_loading"
  72. android:layout_width="31dip"
  73. android:layout_height="32dip"
  74. android:layout_gravity="center"
  75. style="@style/progressStyle"
  76. />
  77. <TextView
  78. android:layout_width="wrap_content"
  79. android:layout_height="wrap_content"
  80. android:text="正在載入"
  81. android:textSize="12dip"
  82. android:textColor="#9c9c9c"
  83. android:layout_gravity="center"
  84. android:layout_below="@+id/home_loading"
  85. />
  86. </LinearLayout>
  87. <!-- 底部菜單部分 -->
  88. <LinearLayout
  89. android:id="@+id/home_menuLayout"
  90. android:layout_width="fill_parent"
  91. android:layout_height="wrap_content"
  92. android:orientation="horizontal"
  93. android:background="@drawable/menu_bg1"
  94. android:layout_alignParentBottom="true"
  95. android:gravity="bottom"
  96. >
  97. <RelativeLayout
  98. android:layout_width="wrap_content"
  99. android:layout_height="wrap_content"
  100. android:layout_weight="1"
  101. >
  102. <ImageButton
  103. android:id="@+id/home_ib_homepage"
  104. android:layout_width="wrap_content"
  105. android:layout_height="wrap_content"
  106. android:background="@drawable/btn_home"
  107. android:layout_centerInParent="true"
  108. />
  109. </RelativeLayout>
  110. <RelativeLayout
  111. android:layout_width="wrap_content"
  112. android:layout_height="wrap_content"
  113. android:layout_weight="1"
  114. >
  115. <ImageButton
  116. android:id="@+id/home_ib_message"
  117. android:layout_width="wrap_content"
  118. android:layout_height="wrap_content"
  119. android:background="@drawable/btn_message"
  120. android:layout_centerInParent="true"
  121. />
  122. </RelativeLayout>
  123. <RelativeLayout
  124. android:id="@+id/home_myrecordLayout"
  125. android:layout_width="wrap_content"
  126. android:layout_height="wrap_content"
  127. android:layout_weight="1"
  128. >
  129. <ImageButton
  130. android:id="@+id/home_ib_myrecord"
  131. android:layout_width="wrap_content"
  132. android:layout_height="wrap_content"
  133. android:background="@drawable/btn_myrecord"
  134. android:layout_centerInParent="true"
  135. />
  136. </RelativeLayout>
  137. <RelativeLayout
  138. android:layout_width="wrap_content"
  139. android:layout_height="wrap_content"
  140. android:layout_weight="1"
  141. >
  142. <ImageButton
  143. android:id="@+id/home_ib_tail"
  144. android:layout_width="wrap_content"
  145. android:layout_height="wrap_content"
  146. android:background="@drawable/btn_tail"
  147. android:layout_centerInParent="true"
  148. />
  149. </RelativeLayout>
  150. <RelativeLayout
  151. android:layout_width="wrap_content"
  152. android:layout_height="wrap_content"
  153. android:layout_weight="1"
  154. >
  155. <ImageButton
  156. android:id="@+id/home_ib_more"
  157. android:layout_width="wrap_content"
  158. android:layout_height="wrap_content"
  159. android:background="@drawable/btn_more"
  160. android:layout_centerInParent="true"
  161. />
  162. </RelativeLayout>
  163. </LinearLayout>
  164. </RelativeLayout>
  165. </LinearLayout>

Copyright © Linux教程網 All Rights Reserved