Android Development ListView Implement Item local Refresh _android

Source: Internet
Author: User

Most programmers are familiar with the ListView refresh mechanism in Android, modifying or adding a data source in adapter, and then invoking Notifydatasetchanged () to refresh ListView. In this mode, we will let the control display different content in the GetView according to the different data sources. This pattern is the most common refresh mode, when we slide the ListView back and forth, call the adapter GetView method, and then ListView to draw the view returned by adapter. In this mode, the view display content or state are recorded in the adapter inside the data source, ListView Update frequency is not frequent, it updates as the data source changes.

But the cloud-dwelling community in the company project, there is a download module, because it may download several data at the same time, so use the ListView to display all the content is downloading. Because the download progress to be updated in real time, you have to keep calling notifydatesetchanged to refresh the data. This will continue to redraw the entire ListView interface, the performance overhead is very high. And if each item has pictures, each item's picture needs to reload, even if the picture did the memory cache, refresh the picture will flash, keep the refresh will cause the picture of each item keep flashing, experience is not good.

So is there a solution to the above problem? Of course it is. We can update the item locally without affecting other item that is not modified. So how to achieve it? Let's look at the code below.

 private void Updateview (int itemindex) {
   //Get the first position to display the control,
   int visibleposition = Mlistview.getfirstvisibleposition ();
   If the view that you want to update is updated only when it is visible, when it is not visible, skip not updating
   if (itemindex-visibleposition >=) {
    //Get view view of item to be updated
    Mlistview.getchildat (itemindex-visibleposition);
    Invoke Adapter Update Interface
    madapter.updateview (view, itemindex);
   }
  

This function is mainly based on the incoming itemindex to get the view that the ItemIndex data shows. ItemIndex is the data to be modified in the list of the location, such as my download progress here has been updated, sent a broadcast here received, you need to modify the content of the download progress bar, broadcast receiver can write:

 @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);
   }

  Here's a look at adapter's specific code:

 public class Appcontentadapter extends baseadapter{private list<appcontent> mdates = null;
  Private context Mcontext;
  Public Appcontentadapter {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; /** * Set viewholder data * @param holder * @param itemindex/private void SetData (viewholder holder, int item
   Index) {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 ("Download progress:" + appcontent.getdownloadpercent () + "%");
}}/** * Local refresh  * @param view * @param itemindex */public void Updateview (view view, int itemindex) {if (view = = null) {R
   Eturn;
   ///From view to obtain 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); /** * Icon * @param downloadpercentview * @param status/private void Seticonbystatus (DOWNLOADPERCENTV
   Iew 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; }
 }

The above content is about the Android development of the ListView implementation of the item local refresh all the content, hope for everyone useful, more about ListView local refresh problem, please login Cloud Habitat Community website inquiries, thank you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.