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.