android - Imageview getting automatically changed on scroll listview -
i new android , facing 1 comman problem of listview googled alot did'nt found usefull case.hope people show me right direction.whenever changing image "plus" "tick" on click last item image got automatically changed on scroll.
my list_comapare_product xml :-
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/white_color" android:orientation="vertical" > <relativelayout android:layout_width="match_parent" android:layout_height="100dp" android:layout_marginleft="20dp" android:layout_marginright="20dp" android:background="@color/white_color" android:gravity="center_vertical" > <imageview android:id="@+id/img_product" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_centerhorizontal="true" android:layout_centervertical="true" android:src="@drawable/crane" /> <com.mss.skyjack.custom.views.skyjackcustomtextview android:id="@+id/tv_product_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerhorizontal="true" android:layout_centervertical="true" android:gravity="center" android:text="test" android:textappearance="?android:attr/textappearancemedium" /> <imageview android:id="@+id/img_plus" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentright="true" android:layout_centerhorizontal="true" android:layout_centervertical="true" android:src="@drawable/plus" /> </relativelayout> <view android:layout_width="match_parent" android:layout_height="1dp" android:layout_marginleft="20dp" android:layout_marginright="20dp" android:background="@color/black_color" /> </linearlayout>
here adapter :-
import java.util.list; import android.app.activity; import android.content.context; import android.view.layoutinflater; import android.view.view; import android.view.view.onclicklistener; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.imageview; import android.widget.textview; import android.widget.toast; public class comapreproductselectionadapter extends baseadapter { activity activity; list<selectortest> listselector; private layoutinflater mlayoutinflater; public comapreproductselectionadapter(activity activity, list<selectortest> listselector) { this.activity = activity; this.listselector = listselector; mlayoutinflater = (layoutinflater) activity .getsystemservice(context.layout_inflater_service); } @override public int getcount() { return listselector.size(); } @override public selectortest getitem(int position) { return listselector.get(position); } @override public long getitemid(int position) { return position; } @override public view getview(int position, view convertview, viewgroup parent) { final viewholder viewholder; final int = position; final selectortest listitem = listselector.get(position); if (convertview == null) { convertview = mlayoutinflater.inflate( r.layout.list_comapare_product, parent, false); viewholder = new viewholder(); viewholder.tvproductname = (textview) convertview .findviewbyid(r.id.tv_product_name); viewholder.imgproduct = (imageview) convertview .findviewbyid(r.id.img_product); viewholder.imgadd = (imageview) convertview .findviewbyid(r.id.img_plus); convertview.settag(viewholder); } else { viewholder = (viewholder) convertview.gettag(); } viewholder.imgadd.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { if (viewholder.imgadd.getdrawable().getconstantstate() == activity .getresources().getdrawable(r.drawable.plus) .getconstantstate()) { viewholder.imgadd.setimageresource(r.drawable.tick); } else { viewholder.imgadd.setimageresource(r.drawable.plus); } } }); return convertview; } static class viewholder { textview tvproductname; imageview imgproduct, imgadd; view viewrightline; } }
thanks.
you need keep track of items have been "ticked". copied adapter , made necessary changes. can copy-paste code , should work.
import java.util.list; import android.app.activity; import android.content.context; import android.view.layoutinflater; import android.view.view; import android.view.view.onclicklistener; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.imageview; import android.widget.textview; import android.widget.toast; public class comapreproductselectionadapter extends baseadapter { activity activity; list<selectortest> listselector; private layoutinflater mlayoutinflater; private arraylist<integer> tickeditems = new arraylist<integer>(); public comapreproductselectionadapter(activity activity, list<selectortest> listselector) { this.activity = activity; this.listselector = listselector; mlayoutinflater = (layoutinflater) activity .getsystemservice(context.layout_inflater_service); } @override public int getcount() { return listselector.size(); } @override public selectortest getitem(int position) { return listselector.get(position); } @override public long getitemid(int position) { return position; } @override public view getview(int position, view convertview, viewgroup parent) { final viewholder viewholder; final int = position; final selectortest listitem = listselector.get(position); if (convertview == null) { convertview = mlayoutinflater.inflate( r.layout.list_comapare_product, parent, false); viewholder = new viewholder(); viewholder.tvproductname = (textview) convertview .findviewbyid(r.id.tv_product_name); viewholder.imgproduct = (imageview) convertview .findviewbyid(r.id.img_product); viewholder.imgadd = (imageview) convertview .findviewbyid(r.id.img_plus); convertview.settag(viewholder); } else { viewholder = (viewholder) convertview.gettag(); } viewholder.imgadd.settag(position); viewholder.imgadd.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { if(tickeditems.contains((integer)v.gettag()) { //already ticked, set plus viewholder.imgadd.setimagedrawable(activity.getresources().getdrawable(r.drawable.plus)); tickeditems.remove((integer)v.gettag()); } else { tickeditems.add((integer)v.gettag()); viewholder.imgadd.setimagedrawable(activity.getresources().getdrawable(r.drawable.tick)); } } }); if(tickeditems.contains(position)) viewholder.imgadd.setimagedrawable(activity.getresources().getdrawable(r.drawable.tick)); else viewholder.imgadd.setimagedrawable(activity.getresources().getdrawable(r.drawable.plus)); return convertview; } static class viewholder { textview tvproductname; imageview imgproduct, imgadd; view viewrightline; } }
as extra, don't use imageview.setimageresource()
inefficient.
quote android docs imageview.setimagereource()
this bitmap reading , decoding on ui thread, can cause latency hiccup. if that's concern, consider using setimagedrawable(android.graphics.drawable.drawable) or setimagebitmap(android.graphics.bitmap) , bitmapfactory instead.
Comments
Post a Comment