java - RecyclerView scrolling makes changes -
i have recyclerview has custom adapter inflates rowlayout. each row contains cardview , in text , images buttons. in particular have "like" button(imageview
) supposed change imageresource on click.
i able set onclicklistener
in onbindviewholder()
method , indeed register when click button, not changes buttons image changes of other buttons , when scroll down , again first button reset.
here adapter code.
public class myadapter extends recyclerview.adapter<myadapter.viewholder> implements view.onclicklistener { private arraylist<wallpost> wallposts; @override public void onclick(view v) { } public static class viewholder extends recyclerview.viewholder { public view view; public viewholder(view v) { super(v); view = v; } } public myadapter(arraylist<wallpost> wallposts) { this.wallposts = wallposts; } @override public myadapter.viewholder oncreateviewholder(viewgroup parent, int viewtype) { view v = layoutinflater.from(parent.getcontext()) .inflate(r.layout.item_layout, parent, false); viewholder vh = new viewholder(v); return vh; } @override public void onbindviewholder(viewholder holder, final int position) { textview name = (textview) holder.view.findviewbyid(r.id.person_name); textview age = (textview) holder.view.findviewbyid(r.id.person_age); imageview profile = (imageview) holder.view.findviewbyid(r.id.person_photo); textview description = (textview) holder.view.findviewbyid(r.id.description_text); appcompatimageview mainimage = (appcompatimageview) holder.view.findviewbyid(r.id.main_image); final imageview likebutton = (imageview) holder.view.findviewbyid(r.id.like_button); name.settext(wallposts.get(position).getname()); age.settext(wallposts.get(position).getage()); profile.setimageresource(wallposts.get(position).getphotoid()); description.settext(wallposts.get(position).getdescription()); mainimage.setimageresource(wallposts.get(position).getmainphotoid()); likebutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { likebutton.setimageresource(r.drawable.favourite_red); } }); } @override public int getitemcount() { return wallposts.size(); } }
here item layout
<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.cardview xmlns:card_view="http://schemas.android.com/apk/res-auto" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/cv" android:layout_marginleft="8dp" android:layout_marginright="8dp" android:layout_marginbottom="10dp" card_view:cardcornerradius="5dp"> <relativelayout android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="8dp" > <de.hdodenhof.circleimageview.circleimageview android:layout_width="48dp" android:layout_height="48dp" android:id="@+id/person_photo" android:layout_alignparentleft="true" android:layout_alignparenttop="true" android:layout_marginright="16dp" android:src="@drawable/placeholderprofilepic" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/person_name" android:text="name" android:layout_torightof="@+id/person_photo" android:layout_alignparenttop="true" android:textsize="30sp" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/person_age" android:text="19" android:layout_torightof="@+id/person_photo" android:layout_below="@+id/person_name" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/description_text" android:text="this description of image" android:layout_below="@id/person_age"/> <android.support.v7.widget.appcompatimageview android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/main_image" android:src="@drawable/placeholderfoodimage" android:layout_below="@+id/description_text" android:elevation="4dp"/> <imageview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/like_button" android:src="@drawable/favoruite_hollow" android:layout_margintop="10dp" android:layout_below="@+id/main_image" /> </relativelayout> </android.support.v7.widget.cardview>
this due recycling of views . need modify getview()
method , these 2 things -
modify
wallpost
class have flag set flag. set flag inonclicklistener
, updatewallposts
dataset .wallposts.get(position).setlike(true);
set like/not image resource everytime based on flag own in list objects -
if(wallposts.get(position).isliked()){ likebutton.setimageresource(r.drawable.favourite_red); } else{ likebutton.setimageresource(r.drawable.favoruite_hollow); }
Comments
Post a Comment