Android開發之ListView實現Item局部重新整理_Android

來源:互聯網
上載者:User

對於android中的ListView重新整理機制,大多數的程式員都是很熟悉的,修改或者添加adapter中的資料來源之後,然後調用notifyDataSetChanged()重新整理ListView。在這種模式下,我們會在getView中,根據不同的資料來源,讓控制項顯示不同的內容。這種模式是最常見的重新整理模式,當我們來回滑動ListView的時候,調用adapter的getView方法,然後listview對adapter返回的View進行繪製。這種模式下,View的顯示內容或狀態都記錄在adapter裡面的資料來源中,listview的更新頻率不頻繁,它隨著資料來源的變化而更新。

  但是雲棲社區小編在做公司項目的時候,有個下載模組,因為可能同時下載好幾個資料,所以用的listview展示所有正在下載的內容。因為下載進度要即時更新,所以要不停的調用notifyDateSetChanged重新整理資料。這樣會不停的重新繪製整個listview的介面,效能開銷非常大。而且如果每個item有圖片的話,每個item的圖片都需要重新載入,就算圖片做了記憶體緩衝,重新整理一下圖片也會閃一下,不停的重新整理就會導致各個item的圖片不停的閃,體驗一點都不好。

  那麼對於上面問題,有沒有解決辦法呢?當然是有的。我們可以針對某一個item進行局部更新,而不影響其它沒有修改的item。那麼具體如何?的呢?我們看下面的代碼。

 private void updateView(int itemIndex) {   //得到第一個可顯示控制項的位置,   int visiblePosition = mListView.getFirstVisiblePosition();   //只有當要更新的view在可見的位置時才更新,不可見時,跳過不更新   if (itemIndex - visiblePosition >= ) {    //得到要更新的item的view    View view = mListView.getChildAt(itemIndex - visiblePosition);    //調用adapter更新介面    mAdapter.updateView(view, itemIndex);   }  }

  這個函數主要是根據傳入的itemIndex來擷取第itemIndex的資料所顯示的view。itemIndex就是要修改的資料再List集合中的位置,比如我這裡下載進度有更新,發了一個廣播這裡接收到了,需要修改該下載內容的進度條,廣播接收器可以這麼寫:

 @Override   public void onReceive(Context context, Intent intent) {    AppContent appContent = intent.getParcelableExtra("appContent");    if(appContent == null) return;    int itemIndex = ;    for(AppContent appContent : mList) {     if(appContent.getUrl().equals(appContent.getUrl())) {      itemIndex = mList.indexOf(appContent);      appContent.setDownloadPercent(appContent.getDownloadPercent());      break;     }    }    updateView(itemIndex);   }

  下面看Adapter的具體代碼:

 public class AppContentAdapter extends BaseAdapter{  private List<AppContent> mDates = null;  private Context mContext;  public AppContentAdapter(Context context) {   this.mContext = context;  }  @Override  public int getCount() {   return mDates.size();  }  @Override  public Object getItem(int position) {   return mDates.get(position);  }  @Override  public long getItemId(int position) {   return position;  }  public void setDates(List<AppContent> mDates) {   this.mDates = mDates;  }  @Override  public View getView(int position, View convertView, ViewGroup parent) {   ViewHolder holder = null;   if (convertView == null) {    holder = new ViewHolder();    convertView = LayoutInflater.from(mContext).inflate(      R.layout.listitem_download, null);    holder.statusIcon = (DownloadPercentView) convertView.findViewById(R.id.status_icon);    holder.name = (TextView) convertView.findViewById(R.id.name);    holder.downloadPercent = (TextView) convertView.findViewById(R.id.download_percent);    holder.progressBar = (ProgressBar) convertView.findViewById(R.id.progressbar);    convertView.setTag(holder);   } else {    holder = (ViewHolder) convertView.getTag();   }   setData(holder, position);   return convertView;  }  /**  * 設定viewHolder的資料  * @param holder  * @param itemIndex  */  private void setData(ViewHolder holder, int itemIndex) {   AppContent appContent = mDates.get(itemIndex);   holder.name.setText(appContent.getName());   holder.progressBar.setProgress(appContent.getDownloadPercent());   setIconByStatus(holder.statusIcon, appContent.getStatus());   if(appContent.getStatus() == AppContent.Status.PENDING) {    holder.downloadPercent.setVisibility(View.INVISIBLE);   } else {    holder.downloadPercent.setVisibility(View.VISIBLE);    holder.statusIcon.setProgress(appContent.getDownloadPercent());    holder.downloadPercent.setText("下載進度:" + appContent.getDownloadPercent() + "%");   }  }  /**  * 局部重新整理  * @param view  * @param itemIndex  */  public void updateView(View view, int itemIndex) {   if(view == null) {    return;   }   //從view中取得holder   ViewHolder holder = (ViewHolder) view.getTag();   holder.statusIcon = (DownloadPercentView) view.findViewById(R.id.status_icon);   holder.name = (TextView) view.findViewById(R.id.name);   holder.downloadPercent = (TextView) view.findViewById(R.id.download_percent);   holder.progressBar = (ProgressBar) view.findViewById(R.id.progressbar);   setData(holder, itemIndex);  }  /**  * 根據狀態設定表徵圖  * @param downloadPercentView  * @param status  */  private void setIconByStatus(DownloadPercentView downloadPercentView, AppContent.Status status) {   downloadPercentView.setVisibility(View.VISIBLE);   if(status == AppContent.Status.PENDING) {    downloadPercentView.setStatus(DownloadPercentView.STATUS_PEDDING);   }   if(status == AppContent.Status.DOWNLOADING) {    downloadPercentView.setStatus(DownloadPercentView.STATUS_DOWNLOADING);   }   if(status == AppContent.Status.WAITING) {    downloadPercentView.setStatus(DownloadPercentView.STATUS_WAITING);   }   if(status == AppContent.Status.PAUSED) {    downloadPercentView.setStatus(DownloadPercentView.STATUS_PAUSED);   }   if(status == AppContent.Status.FINISHED) {    downloadPercentView.setStatus(DownloadPercentView.STATUS_FINISHED);   }  }  private class ViewHolder {   private DownloadPercentView statusIcon;   private TextView name;   private TextView downloadPercent;   private ProgressBar progressBar;  } }

以上內容是關於Android開發之ListView實現Item局部重新整理的全部內容,希望對大家有用,更多有關listview局部重新整理問題,請登入雲棲社區官網查詢,謝謝!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.