Jaydeep Dhamecha Jaydeep Dhamecha - 2 months ago 18
Android Question

How to change button Image like toggle button RecyclerView button click programmatically

I have a

RecyclerView
that is populated with
ListView
. On each of the
ListView
there is a
Button
, which up add the list.


Here is what the button looks when it is not pressed

enter image description here


Here is what the button looks when it is pressed,

enter image description here


How can I keep the buttons pressed respectfully for each
ListView
?

This is my Adapter

public class BuildCustomAdapter extends RecyclerView.Adapter<BuildCustomAdapter.MyViewHolder> implements Filterable {
private int previousTotal = 0;
private boolean loading = true;
private List<Build> buildList;
private List<Build> buildListCopy;
private ItemFilter mFilter = new ItemFilter();


@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.build_list_row, parent, false);

return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {

Build build = buildList.get(position);
holder.imageView.setImageResource(build.getImages());
holder.name.setText(build.getName());

}

@Override
public int getItemCount() {
return buildList.size();
}

@Override
public Filter getFilter() {
if (mFilter == null) {
mFilter = new ItemFilter();
}
return mFilter;
}

public class MyViewHolder extends RecyclerView.ViewHolder {
public ImageView imageView;
public TextView name;
public Button button;

public MyViewHolder(View itemView) {
super(itemView);
imageView = (ImageView) itemView.findViewById(R.id.ivPerson);
name = (TextView) itemView.findViewById(R.id.tvPersonName);
button = (Button) itemView.findViewById(R.id.addbn);

}
}

private class ItemFilter extends Filter {


protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();

if (constraint != null && constraint.length() > 0) {
List<Build> filterList = new ArrayList<Build>();

for (int i = 0; i < buildList.size(); i++) {
if ((buildList.get(i).getName().toUpperCase())
.contains(constraint.toString().toUpperCase())) {
Build builddata = new Build(buildList.get(i).getImages(), buildList.get(i).getName());

filterList.add(builddata);
}
}
results.count = filterList.size();
results.values = filterList;

} else {
results.count = buildList.size();
results.values = buildList;
}
return results;
}

@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {

buildList = (ArrayList<Build>) results.values;
notifyDataSetChanged();
}

}


public BuildCustomAdapter(List<Build> buildList) {
this.buildList = buildList;
}


}


Build.class

public class Build {
private String name;
private int images;

public Build(int images, String name) {
this.images = images;
this.name = name;
}

public int getImages() {
return images;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

Answer

It will be better if you use selected as boolean variable into your pojo class, it will help adapter to set the image according to state. Actually , whenever you scroll-up and down un-visible items remove from the memory, and we have updated our list according to the button event, so second time it will fetch the desired state and again show the previous state image.

BuildCustomAdapter pass context always whenever you create an Adapter, context will help you

 Context context;
    public BuildCustomAdapter(List<Build> buildList,Context context) {
            this.buildList = buildList;
            this.context = context;
        }

Build.java

public class Build {
        private String name;
        private int images;
        private boolean selected;

        public Build(boolean selected, String name,int images) {
            this.images = images;
           this.selected=selected; 
           this.name = name;
        }

        public int getImages() {
            return images;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public void setSeleted(boolean selected){
            this.selected=selected;
        }

        public boolean isSelected(){
            return selected;
        } 
    }

YourAdapter

@Override
        public void onBindViewHolder(MyViewHolder holder,  int position) {

            Build build = buildList.get(position);

            holder.imageView.setImageResources(build.getImages());

            holder.name.setText(build.getName());
            setImageIntoButton(holder.button,build.isSelected());

            holder.button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        build.setSelected(!build.isSelected());
                        setImageIntoButton(holder.button,build.isSelected());
                    }
                });
        }

      private void setImageIntoButton(Button buttonView,boolean isSelected){
         if(isSelected)
           buttonView.setBackgroundResource(R.drawable.clicked_image); 
            else 
          buttonView.setBackgroundResource(R.drawable.cross_image); 
 }

ItemFilter change this statement

Build builddata = new Build(buildList.get(i).isSelected(), buildList.get(i).getName(),buildList.get(i).getImages());