Android开发之ListView实现Item局部刷新

Android开发之ListView实现Item局部刷新,第1张

概述对于android中的ListView刷新机制,大多数的程序员都是很熟悉的,修改或者添加adapter中的数据源之后,然后调用notifyDataSetChanged()刷新ListView。在这种模式下,我们会在getView中,根据不同的数据源,让控件显示

对于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局部刷新问题,请登录编程小技巧官网查询,谢谢!

总结

以上是内存溢出为你收集整理的Android开发之ListView实现Item局部刷新全部内容,希望文章能够帮你解决Android开发之ListView实现Item局部刷新所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/web/1141919.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存