歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android開發教程:OnScrollListener實現ListView滾屏時不加載數據

Android開發教程:OnScrollListener實現ListView滾屏時不加載數據

日期:2017/3/1 10:30:29   编辑:Linux編程

使用ListView過程中,如果滾動加載數據的操作比較費時,很容易在滾屏時出現屏幕卡住的現象,一個解決的辦法就是不要在滾動時加載數據,而是等到滾動停止後再進行數據的加載。這同樣要實現OnScrollListener接口,關於該接口的簡要描述見這篇文章(http://www.linuxidc.com/Linux/2012-02/54745.htm),這裡直接進行代碼的分析:

[java]
  1. package hust.iprai.asce1885;
  2. import Android.app.ListActivity;
  3. import android.content.Context;
  4. import android.os.Bundle;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import android.widget.AbsListView;
  9. import android.widget.AbsListView.OnScrollListener;
  10. import android.widget.BaseAdapter;
  11. import android.widget.TextView;
  12. public class MainActivity extends ListActivity implements OnScrollListener {
  13. private TextView mStatus; //顯示滾屏狀態
  14. private boolean mBusy = false; //標識是否存在滾屏操作
  15. /**
  16. * 自定義Adapter,實現ListView中view的顯示
  17. *
  18. */
  19. private class SlowAdapter extends BaseAdapter {
  20. private LayoutInflater mInflater;
  21. public SlowAdapter(Context context) {
  22. mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  23. }
  24. /**
  25. * 列表中元素個數取決於數據的個數
  26. */
  27. public int getCount() {
  28. return mStrings.length;
  29. }
  30. /**
  31. * 我們的模擬數據是從數組中獲取的,因此這裡直接返回索引值就可以獲取相應的數據了
  32. */
  33. public Object getItem(int position) {
  34. return position;
  35. }
  36. /**
  37. * 使用數組的索引作為唯一的id
  38. */
  39. public long getItemId(int position) {
  40. return position;
  41. }
  42. /**
  43. * 獲取List中每一行的view
  44. */
  45. public View getView(int position, View convertView, ViewGroup parent) {
  46. TextView text;
  47. //給text賦值
  48. if (null == convertView) {
  49. text = (TextView) mInflater.inflate(android.R.layout.simple_list_item_1, parent, false);
  50. } else {
  51. text = (TextView) convertView;
  52. }
  53. if (!mBusy) {
  54. //當前不處於加載數據的忙碌時期(沒有滾屏),則顯示數據
  55. text.setText(mStrings[position]);
  56. //這裡約定將tag設置為null說明這個view已經有了正確的數據
  57. text.setTag(null);
  58. } else {
  59. //當前處於滾屏階段,不加載數據,直接顯示數據加載中提示
  60. text.setText("Loading...");
  61. //tag非空說明這個view仍然需要進行數據加載並顯示
  62. text.setTag(this);
  63. }
  64. return text;
  65. }
  66. }
  67. @Override
  68. public void onCreate(Bundle savedInstanceState) {
  69. super.onCreate(savedInstanceState);
  70. setContentView(R.layout.main);
  71. mStatus = (TextView) findViewById(R.id.status);
  72. mStatus.setText("Idle");
  73. //使用自定義的ListAdapter將數據映射到TextView中
  74. setListAdapter(new SlowAdapter(this));
  75. //設置滾動監聽器
  76. getListView().setOnScrollListener(this);
  77. }
  78. public void onScroll(AbsListView view, int firstVisibleItem,
  79. int visibleItemCount, int totalItemCount) {
  80. }
  81. public void onScrollStateChanged(AbsListView view, int scrollState) {
  82. switch (scrollState) {
  83. case OnScrollListener.SCROLL_STATE_IDLE: //Idle態,進行實際數據的加載顯示
  84. mBusy = false;
  85. int first = view.getFirstVisiblePosition();
  86. int count = view.getChildCount();
  87. for (int i = 0; i < count; i++) {
  88. TextView tv = (TextView) view.getChildAt(i);
  89. if (tv.getTag() != null) { //非null說明需要加載數據
  90. tv.setText(mStrings[first + i]);
  91. tv.setTag(null);
  92. }
  93. }
  94. mStatus.setText("Idle");
  95. break;
  96. case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
  97. mBusy = true;
  98. mStatus.setText("Touch Scroll");
  99. break;
  100. case OnScrollListener.SCROLL_STATE_FLING:
  101. mBusy = true;
  102. mStatus.setText("Fling");
  103. break;
  104. default:
  105. mStatus.setText("Are you kidding me!");
  106. break;
  107. }
  108. }
  109. private String[] mStrings = {
  110. "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam",
  111. "Abondance", "Ackawi", "Acorn", "Adelost", "Affidelice au Chablis",
  112. "Afuega'l Pitu", "Airag", "Airedale", "Aisy Cendre",
  113. "Allgauer Emmentaler", "Alverca", "Ambert", "American Cheese",
  114. "Ami du Chambertin", "Anejo Enchilado", "Anneau du Vic-Bilh",
  115. "Anthoriro", "Appenzell", "Aragon", "Ardi Gasna", "Ardrahan",
  116. "Armenian String", "Aromes au Gene de Marc", "Asadero", "Asiago",
  117. "Aubisque Pyrenees", "Autun", "Avaxtskyr", "Baby Swiss", "Babybel",
  118. "Baguette Laonnaise", "Bakers", "Baladi", "Balaton", "Bandal",
  119. "Banon", "Barry's Bay Cheddar", "Basing", "Basket Cheese",
  120. "Bath Cheese", "Bavarian Bergkase", "Baylough", "Beaufort",
  121. "Beauvoorde", "Beenleigh Blue", "Beer Cheese", "Bel Paese",
  122. "Bergader", "Bergere Bleue", "Berkswell", "Beyaz Peynir",
  123. "Bierkase", "Bishop Kennedy", "Blarney", "Bleu d'Auvergne",
  124. "Bleu de Gex", "Bleu de Laqueuille", "Bleu de Septmoncel",
  125. "Bleu Des Causses", "Blue", "Blue Castello", "Blue Rathgore",
  126. "Blue Vein (Australian)", "Blue Vein Cheeses", "Bocconcini",
  127. "Bocconcini (Australian)", "Boeren Leidenkaas", "Bonchester",
  128. "Bosworth"};
  129. }
下面是布局文件main.xml:

[html]
  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 android:id="@android:id/list"
  7. android:layout_width="match_parent"
  8. android:layout_height="0dip"
  9. android:layout_weight="1"
  10. android:drawSelectorOnTop="false"/>
  11. <TextView android:id="@+id/status"
  12. android:layout_width="match_parent"
  13. android:layout_height="wrap_content"
  14. android:paddingLeft="8dip"
  15. android:paddingRight="8dip"/>
  16. </LinearLayout>
程序運行結果如下圖所示:

Copyright © Linux教程網 All Rights Reserved