自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Android奇巧:ListView實(shí)現(xiàn)Item局部刷新

移動開發(fā)
但是博主在做公司項目的時候,有個下載模塊,因為可能同時下載好幾個數(shù)據(jù),所以用的listview展示所有正在下載的內(nèi)容。因為下載進(jìn)度要實(shí)時更新,所以要不停的調(diào)用notifyDateSetChanged刷新數(shù)據(jù)。這樣會不停的重新繪制整個listview的界面,性能開銷非常大。

對于ListView數(shù)據(jù)的刷新大家都知道,改變Adapter的數(shù)據(jù)源,然后調(diào)用Adapter的notifyDateSetChanged()方法即可。

但是博主在做公司項目的時候,有個下載模塊,因為可能同時下載好幾個數(shù)據(jù),所以用的listview展示所有正在下載的內(nèi)容。因為下載進(jìn)度要實(shí)時更新,所以要不停的調(diào)用notifyDateSetChanged刷新數(shù)據(jù)。這樣會不停的重新繪制整個listview的界面,性能開銷非常大。而且如果每個item有圖片的話,每個item的圖片都需要重新加載,就算圖片做了內(nèi)存緩存,刷新一下圖片也會閃一下,不停的刷新就會導(dǎo)致各個item的圖片不停的閃,體驗一點(diǎn)都不好。

那么對于上面問題,有沒有解決辦法呢?當(dāng)然是有的。我們可以針對某一個item進(jìn)行局部更新,而不影響其它沒有修改的item。那么具體如何實(shí)現(xiàn)的呢?我們看下面的代碼。

  1. 1 private void updateView(int itemIndex) { 
  2. 2 //得到***個可顯示控件的位置, 
  3. 3 int visiblePosition = mListView.getFirstVisiblePosition(); 
  4. 4 //只有當(dāng)要更新的view在可見的位置時才更新,不可見時,跳過不更新 
  5. 5 if (itemIndex - visiblePosition >= 0) { 
  6. 6 //得到要更新的item的view 
  7. 7 View view = mListView.getChildAt(itemIndex - visiblePosition); 
  8. 8 //調(diào)用adapter更新界面 
  9. 9 mAdapter.updateView(view, itemIndex); 
  10. 10 } 
  11. 11 } 

這個函數(shù)主要是根據(jù)傳入的itemIndex來獲取第itemIndex的數(shù)據(jù)所顯示的view。itemIndex就是要修改的數(shù)據(jù)再List集合中的位置,比如我這里下載進(jìn)度有更新,發(fā)了一個廣播這里接收到了,需要修改該下載內(nèi)容的進(jìn)度條,廣播接收器可以這么寫:
 

 

  1.  1 @Override 
  2. 2 public void onReceive(Context context, Intent intent) { 
  3. 3 AppContent appContent = intent.getParcelableExtra("appContent"); 
  4. 4 if(appContent == nullreturn
  5. 5 int itemIndex = 0
  6. 6 for(AppContent appContent1 : mList) { 
  7. 7 if(appContent.getUrl().equals(appContent1.getUrl())) { 
  8. 8 itemIndex = mList.indexOf(appContent1); 
  9. 9 appContent1.setDownloadPercent(appContent.getDownloadPercent()); 
  10. 10 break
  11. 11 } 
  12. 12 } 
  13. 13 updateView(itemIndex); 
  14. 14 } 
  15.  
  16. 下面看Adapter的具體代碼: 
  17.  
  18. 1 public class AppContentAdapter extends BaseAdapter{ 
  19. 2 
  20. 3 private List<AppContent> mDates = null
  21. 4 private Context mContext; 
  22. 5 
  23. 6 public AppContentAdapter(Context context) { 
  24. 7 this.mContext = context; 
  25. 8 } 
  26. 9 
  27. 10 @Override 
  28. 11 public int getCount() { 
  29. 12 return mDates.size(); 
  30. 13 } 
  31. 14 
  32. 15 @Override 
  33. 16 public Object getItem(int position) { 
  34. 17 return mDates.get(position); 
  35. 18 } 
  36. 19 
  37. 20 @Override 
  38. 21 public long getItemId(int position) { 
  39. 22 return position; 
  40. 23 } 
  41. 24 
  42. 25 public void setDates(List<AppContent> mDates) { 
  43. 26 this.mDates = mDates; 
  44. 27 } 
  45. 28 
  46. 29 @Override 
  47. 30 public View getView(int position, View convertView, ViewGroup parent) { 
  48. 31 ViewHolder holder = null
  49. 32 if (convertView == null) { 
  50. 33 holder = new ViewHolder(); 
  51. 34 convertView = LayoutInflater.from(mContext).inflate( 
  52. 35 R.layout.listitem_download, null); 
  53. 36 holder.statusIcon = (DownloadPercentView) convertView.findViewById(R.id.status_icon); 
  54. 37 holder.name = (TextView) convertView.findViewById(R.id.name); 
  55. 38 holder.downloadPercent = (TextView) convertView.findViewById(R.id.download_percent); 
  56. 39 holder.progressBar = (ProgressBar) convertView.findViewById(R.id.progressbar); 
  57. 40 convertView.setTag(holder); 
  58. 41 } else { 
  59. 42 holder = (ViewHolder) convertView.getTag(); 
  60. 43 } 
  61. 44 setData(holder, position); 
  62. 45 return convertView; 
  63. 46 } 
  64. 47 
  65. 48 /** 
  66. 49 * 設(shè)置viewHolder的數(shù)據(jù) 
  67. 50 * @param holder 
  68. 51 * @param itemIndex 
  69. 52 */ 
  70. 53 private void setData(ViewHolder holder, int itemIndex) { 
  71. 54 AppContent appContent = mDates.get(itemIndex); 
  72. 55 holder.name.setText(appContent.getName()); 
  73. 56 holder.progressBar.setProgress(appContent.getDownloadPercent()); 
  74. 57 setIconByStatus(holder.statusIcon, appContent.getStatus()); 
  75. 58 if(appContent.getStatus() == AppContent.Status.PENDING) { 
  76. 59 holder.downloadPercent.setVisibility(View.INVISIBLE); 
  77. 60 } else { 
  78. 61 holder.downloadPercent.setVisibility(View.VISIBLE); 
  79. 62 holder.statusIcon.setProgress(appContent.getDownloadPercent()); 
  80. 63 holder.downloadPercent.setText("下載進(jìn)度:" + appContent.getDownloadPercent() + "%"); 
  81. 64 } 
  82. 65 } 
  83. 66 
  84. 67 
  85. 68 /** 
  86. 69 * 局部刷新 
  87. 70 * @param view 
  88. 71 * @param itemIndex 
  89. 72 */ 
  90. 73 public void updateView(View view, int itemIndex) { 
  91. 74 if(view == null) { 
  92. 75 return
  93. 76 } 
  94. 77 //從view中取得holder 
  95. 78 ViewHolder holder = (ViewHolder) view.getTag(); 
  96. 79 holder.statusIcon = (DownloadPercentView) view.findViewById(R.id.status_icon); 
  97. 80 holder.name = (TextView) view.findViewById(R.id.name); 
  98. 81 holder.downloadPercent = (TextView) view.findViewById(R.id.download_percent); 
  99. 82 holder.progressBar = (ProgressBar) view.findViewById(R.id.progressbar); 
  100. 83 setData(holder, itemIndex); 
  101. 84 } 
  102. 85 
  103. 86 /** 
  104. 87 * 根據(jù)狀態(tài)設(shè)置圖標(biāo) 
  105. 88 * @param downloadPercentView 
  106. 89 * @param status 
  107. 90 */ 
  108. 91 private void setIconByStatus(DownloadPercentView downloadPercentView, AppContent.Status status) { 
  109. 92 downloadPercentView.setVisibility(View.VISIBLE); 
  110. 93 if(status == AppContent.Status.PENDING) { 
  111. 94 downloadPercentView.setStatus(DownloadPercentView.STATUS_PEDDING); 
  112. 95 } 
  113. 96 if(status == AppContent.Status.DOWNLOADING) { 
  114. 97 downloadPercentView.setStatus(DownloadPercentView.STATUS_DOWNLOADING); 
  115. 98 } 
  116. 99 if(status == AppContent.Status.WAITING) { 
  117. 100 downloadPercentView.setStatus(DownloadPercentView.STATUS_WAITING); 
  118. 101 } 
  119. 102 if(status == AppContent.Status.PAUSED) { 
  120. 103 downloadPercentView.setStatus(DownloadPercentView.STATUS_PAUSED); 
  121. 104 } 
  122. 105 if(status == AppContent.Status.FINISHED) { 
  123. 106 downloadPercentView.setStatus(DownloadPercentView.STATUS_FINISHED); 
  124. 107 } 
  125. 108 } 
  126. 109 
  127. 110 private class ViewHolder { 
  128. 111 private DownloadPercentView statusIcon; 
  129. 112 private TextView name; 
  130. 113 private TextView downloadPercent; 
  131. 114 private ProgressBar progressBar; 
  132. 115 } 
  133. 116 } 

詳細(xì)示例參考:https://github.com/liuling07/MultiTaskAndThreadDownload

責(zé)任編輯:chenqingxiang 來源: 博客園
相關(guān)推薦

2013-08-07 10:47:58

Android特效ListView

2013-08-07 10:35:02

AndroidListView拖拽

2015-03-26 13:14:53

javascriptjs callback實(shí)現(xiàn)調(diào)用

2014-07-16 13:08:24

ListViewItem View

2011-04-11 13:43:35

popupwindowlistviewAndroid

2013-11-05 11:25:21

Android 4.4系統(tǒng)詳解

2012-12-26 15:19:09

Android開發(fā)ListView

2011-05-27 15:02:15

Android ListView

2011-04-11 14:14:29

checkboxlistviewAndroid

2013-03-27 09:17:17

Android開發(fā)AndroidList

2011-06-03 10:48:23

Android ListView

2014-12-30 11:51:35

ListViewItem View

2013-04-02 13:04:07

ListView平滑滾

2014-12-17 09:46:30

AndroidListView最佳實(shí)踐

2013-07-10 10:21:22

Android Lis

2013-07-17 16:33:02

下拉刷新listvie滾動到底部加載Android開發(fā)學(xué)習(xí)

2017-11-08 14:34:20

圖片fresco程序員

2010-01-12 10:48:54

VB.NET LIST

2015-07-28 14:39:02

IOS技巧

2023-12-13 10:07:56

地圖自動駕駛
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號