国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

Android開(kāi)發(fā)之ListView實(shí)現(xiàn)Item局部刷新

2020-04-11 11:22:04
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

對(duì)于android中的ListView刷新機(jī)制,大多數(shù)的程序員都是很熟悉的,修改或者添加adapter中的數(shù)據(jù)源之后,然后調(diào)用notifyDataSetChanged()刷新ListView。在這種模式下,我們會(huì)在getView中,根據(jù)不同的數(shù)據(jù)源,讓控件顯示不同的內(nèi)容。這種模式是最常見(jiàn)的刷新模式,當(dāng)我們來(lái)回滑動(dòng)ListView的時(shí)候,調(diào)用adapter的getView方法,然后listview對(duì)adapter返回的View進(jìn)行繪制。這種模式下,View的顯示內(nèi)容或狀態(tài)都記錄在adapter里面的數(shù)據(jù)源中,listview的更新頻率不頻繁,它隨著數(shù)據(jù)源的變化而更新。

  但是武林網(wǎng)小編在做公司項(xiàng)目的時(shí)候,有個(gè)下載模塊,因?yàn)榭赡芡瑫r(shí)下載好幾個(gè)數(shù)據(jù),所以用的listview展示所有正在下載的內(nèi)容。因?yàn)橄螺d進(jìn)度要實(shí)時(shí)更新,所以要不停的調(diào)用notifyDateSetChanged刷新數(shù)據(jù)。這樣會(huì)不停的重新繪制整個(gè)listview的界面,性能開(kāi)銷非常大。而且如果每個(gè)item有圖片的話,每個(gè)item的圖片都需要重新加載,就算圖片做了內(nèi)存緩存,刷新一下圖片也會(huì)閃一下,不停的刷新就會(huì)導(dǎo)致各個(gè)item的圖片不停的閃,體驗(yàn)一點(diǎn)都不好。

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

 private void updateView(int itemIndex) { //得到第一個(gè)可顯示控件的位置, int visiblePosition = mListView.getFirstVisiblePosition(); //只有當(dāng)要更新的view在可見(jiàn)的位置時(shí)才更新,不可見(jiàn)時(shí),跳過(guò)不更新 if (itemIndex - visiblePosition >= ) { //得到要更新的item的view View view = mListView.getChildAt(itemIndex - visiblePosition); //調(diào)用adapter更新界面 mAdapter.updateView(view, itemIndex); } }

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

 @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; } /** * 設(shè)置viewHolder的數(shù)據(jù) * @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("下載進(jìn)度:" + 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); } /** * 根據(jù)狀態(tài)設(shè)置圖標(biāo) * @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; } }

其實(shí)這些代碼就是我上篇博文《AsyncTask實(shí)現(xiàn)多任務(wù)多線程下載》的例子中的,如果需要可以去下載。

以上內(nèi)容是關(guān)于Android開(kāi)發(fā)之ListView實(shí)現(xiàn)Item局部刷新的全部?jī)?nèi)容,希望對(duì)大家有用,更多有關(guān)listview局部刷新問(wèn)題,請(qǐng)登錄武林網(wǎng)官網(wǎng)查詢,謝謝!

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 独山县| 泌阳县| 怀远县| 巫溪县| 江津市| 金塔县| 繁昌县| 平阳县| 兴宁市| 庆城县| 衡山县| 兰考县| 青河县| 南汇区| 岗巴县| 阳东县| 大竹县| 大石桥市| 喀喇沁旗| 白河县| 舒城县| 南充市| 台北县| 辽源市| 利津县| 海丰县| 新郑市| 昌黎县| 应用必备| 松阳县| 泗洪县| 延吉市| 松原市| 阿克| 钟山县| 徐州市| 徐州市| 乐亭县| 华阴市| 奎屯市| 桃园市|