Recently to do a small function encountered such a problem, ListView and Baseadapter in combination, about the point of praise when the picture repeats, such as: I in the 1th item point of Praise and then heart-type replaced by red, but later every several item will appear a red heart, Respond to the incident is right, do not know what the problem, please the big God Answer ""
The above is a small brother in the forum posted on the issue of the problem, and I encountered the same problem, the following have a lot of enthusiastic comments the man gave the idea, I think, originally so simple ah.
First give the implementation code, and finally the idea is good.
I have edited this blog again, coupled with the animation and collection of the effect of the comments of the Buddies said collection and point of praise can not be carried out at the same time, the picture will be confused, I gave him the idea, he still did not realize, hey, I would like to go again, modify the changes, Bo Lord really care about everyone ah
Effect chart
Mainactivity.java
public class Mainactivity extends activity {@Override protected void onCreate (Bundle savedinstancestate) {Super
. OnCreate (Savedinstancestate);
Setcontentview (R.layout.activity_main);
The simulated data content set is list<contentbean> data = new arraylist<contentbean> ();
for (int i = 0; i < i++) {Contentbean bean = new Contentbean ();
The defaults are assigned to them. Currently there is no point to praise Bean.setzanfocus (false);
Bean.setzannum (i);
By default they are assigned a value that is not currently bean.setshoucanfocus (false);
Bean.setshoucannum (i);
Data.add (Bean);
} ListView ListView = (ListView) Findviewbyid (R.id.listview);
Listview.setadapter (New Myadapter (This,data)); } main.xml <relativelayout xmlns:android= "http://schemas.android.com/apk/res/android xmlns:tools=" http:// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Tools:context" = "Com.example.zan.MainActivity" > <listview android:id= "@+id/listview" anDroid:layout_width= "Match_parent" android:layout_height= "match_parent" > </ListView> </relativelayout&
Gt
Contentbean.java public class Contentbean {Private Boolean zanfocus, Shoucanfocus;
private int zannum, shoucannum;
public Boolean Isshoucanfocus () {return shoucanfocus;
} public void Setshoucanfocus (Boolean shoucanfocus) {this.shoucanfocus = Shoucanfocus;
public int Getshoucannum () {return shoucannum;
The public void setshoucannum (int shoucannum) {this.shoucannum = Shoucannum;
public Boolean Iszanfocus () {return zanfocus;
} public void Setzanfocus (Boolean zanfocus) {this.zanfocus = Zanfocus;
public int Getzannum () {return zannum;
The public void setzannum (int zannum) {this.zannum = Zannum; } Myadapter.java public class Myadapter extends Baseadapter {list<contentbean> data = new ARRAYLIST<CONTENTB
Ean> ();
Context context; Public Myadapter, List<contEntbean> data) {This.context = context;
This.data = data; @Override public int GetCount () {return data.size ();//returns 20 data} @Override public Object getitem (int arg0
) {return data.get (arg0);
@Override public long getitemid (int position) {//TODO auto-generated method stub return position; @Override Public View getview (final int position, View Convertview, ViewGroup parent) {Final Viewholder Holde
R
Final Contentbean bean = data.get (position); if (Convertview = = null) {Convertview = Layoutinflater.from (context). Inflate (R.layout.item, parent, false
);
Holder = new Viewholder ();
Holder.zan_img = (ImageView) Convertview.findviewbyid (r.id.zan_img);
Holder.zan_num = (TextView) Convertview.findviewbyid (r.id.zan_num);
Holder.shoucan_img = (ImageView) Convertview.findviewbyid (r.id.shoucan_img);
Holder.shoucan_num = (TextView) Convertview.findviewbyid (r.id.shoucan_num); COnvertview.settag (holder);
else {holder = (Viewholder) convertview.gettag (); ///Remove the bean if the record state is true, then set the focus point to the IMG image if (Bean.iszanfocus ()) {Holder.zan_img.setImageResource (R.draw
Able.zan_focus);
else {holder.zan_img.setImageResource (r.drawable.zan_release); ///Remove the Release collection picture if (Bean.isshoucanfocus ()) {Holder.shoucan_img.setImageResou in the bean if the record state is true
Rce (R.drawable.shoucang_focus);
else {holder.shoucan_img.setImageResource (r.drawable.shoucang_release);
//Set the number of Holder.zan_num.setText (Bean.getzannum () + "");
Set the number of collections Holder.shoucan_num.setText (Bean.getshoucannum () + ""); Holder.zan_img.setOnClickListener (New Onclicklistener () {@Override public void OnClick (View v) {//obtained
Take the last time you have been clicked Boolean flag = Bean.iszanfocus ();
Judge the current flag is the point of praise or cancel praise, is the words to the bean value minus 1, otherwise add 1 if (flag) {Bean.setzannum (Bean.getzannum ()-1); } ElSE {bean.setzannum (bean.getzannum () + 1);
///Reverse store record to achieve cancellation point-praise function Bean.setzanfocus (!flag);
Animationtools.scale (HOLDER.ZAN_IMG);
}
});
Holder.shoucan_img.setOnClickListener (New Onclicklistener () {@Override public void OnClick (View v) {
Gets whether the last has been clicked Boolean flag = Bean.isshoucanfocus ();
To determine whether the current flag is a collection or collection, it is to reduce the bean value by 1, or add 1 if (flag) {Bean.setshoucannum (Bean.getshoucannum ()-1);
else {bean.setshoucannum (Bean.getshoucannum () + 1);
///Reverse store record, implement Cancel Collection function Bean.setshoucanfocus (!flag);
Animation Animationtools.scale (holder.shoucan_img);
}
});
return convertview;
Private class Viewholder {private ImageView zan_img,shoucan_img;
Private TextView Zan_num,shoucan_num; } item.xml <?xml version= "1.0" encoding= "Utf-8"?> <linearlayout "xmlns:android="
Apk/res/android " Android:layout_width= "Match_parent" android:layout_height= "50DP" android:gravity= "Center" android:orientation= " Horizontal "> <imageview android:id=" @+id/zan_img "android:layout_width=" Wrap_content "Android:layout_" height= "Wrap_content"/> <textview android:id= "@+id/zan_num" android:layout_width= "Wrap_content" an droid:layout_height= "Wrap_content"/> </LinearLayout> Animationtools.java public class Animationtools {pub Lic static void scale (View v) {scaleanimation anim = new Scaleanimation (1.0f, 1.5f, 1.0f, 1.5f, Animation.rel
Ative_to_self, 0.5f, Animation.relative_to_self, 0.5f);
Anim.setduration (300);
V.startanimation (ANIM); }
}
The code is very simple, a little understanding or very easy to understand, we listview update the data in the item, we must understand a truth, not in the item view directly change the data (just start to do when I was directly holder.zan_ Img.setimageresource (resource picture), found that when sliding down, the last touch record was the following item to reuse, resulting in confusion of data, otherwise it will cause confusion of data, if you understand the working principle of ListView, may be more clearly understand, ListView Each load of data is the current screen of a screen of data (in fact, I do not know much, but in the print log, the log is found to initialize the data is a screen of data), when you directly to change the view style, The current item you touch will be reused by the item below that has not yet appeared, I understand =-=.