Android Programming records ListView marking Row State _android

Source: Internet
Author: User

This example describes the way the Android programming record ListView tag row state. Share to everyone for your reference, specific as follows:

Small demo Nothing special, the most special is to minimize the use of notifydatasetchanged, the overhead is too big, of course, use will save a lot of work, but sometimes will encounter other problems, project experience that will have this possibility, nonsense not much, directly on the key code.

@Override public void Onitemclick (adapterview<?> adapterview, view view, int pos, long arg3) {/** * in ListView
  , the value of Getchildat (index) can only be a child of the current visible region (the list can be scrolled)! 1, so if you want to get the front will appear return null value problem, 2, Getchildcount and GetCount will get a different value (a large number of) 3, if the use of Getchildat (index). Findviewbyid (...)
  If you set a value, the scrolling list values change.
   You need to use getfirstvisibleposition () to get the first visible position, using the current position-getfirstvisibleposition (), and then using the Getchildat value!
  * * */int now_pos = Pos-adapterview.getfirstvisibleposition ();
  View v = adapterview.getchildat (now_pos);
  ImageView ImageView = (imageview) V.findviewbyid (r.id.image);
   if (imageview.getvisibility () = = view.visible) {imageview.setvisibility (view.gone);
  Adapter.setstate (Pos,nonbiaozhi);
   }else{imageview.setvisibility (view.visible);
   Adapter.setstate (Pos,biaozhi);
   GetView adjustment, but very cost performance//adapter.notifydatasetchanged ();
  Do not use Notifydatasetchanged,getview will not refresh, improve efficiency Refreshlistview (POS, ImageView); } public void Refreshlistview (int pos, ImageView imageview) {
  if (pre!= pos &&!hasmap.isempty ()) {ImageView image = (ImageView) hasmap.get (pre);
   Image.setvisibility (View.gone);
  Hasmap.remove (pre);
  Pre = POS;
Hasmap.put (pre, ImageView);

 }

The above is a fragment of the method in the activity, and the following is the Baseadapter method fragment:

Set tag public
void SetState (int pos,int state) {
  if (state = = Mainactivity.biaozhi) {
   //Every Click Clears the list, keeping the unique option
   map.clear ();
   Map.put (POS, 1);
  } else{
   Map.Remove (POS);
  }
Check the tag public
boolean getState (int p) {
  if (!map.isempty () && Map.containskey (p)) {return
   true;
  }else return
   false;
}

GetView Code:

The row
if (getState (position)) {
 holder.imageView.setVisibility (view.visible) of a fixed display token;
} else
 holder.imageView.setVisibility (view.gone);

Attach the project download, you will get the title of the effect of the operation. Full instance code code click here to download the site.
I hope this article will help you with the Android program.

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.