Balaji Balaji - 3 months ago 37
Android Question

Android Recycler View select only one image and show tick mark on selected image

I need a solution of how to do the following thing using Android Recycler View.


I have Multiple Images and i need to select only one image like how the radio button works. Now i can select all the images that i have, but i need to restrict the current working behavior to work like the radio button (E.g.) If one is selected the other images that i have should not be selected.


I have tried with the below code but no luck for me. Can anyone rectify the mistake and make the code workable as per my need.

public class StarCountAdapter extends RecyclerView.Adapter<StarCountAdapter.StarCountHolder> {
Context context;
LayoutInflater inflater;
List<StarCount> starCounts = new ArrayList<>();

public StarCountAdapter(Context context, List<StarCount> starCounts) {
this.context = context;
this.inflater = LayoutInflater.from(context);
this.starCounts = starCounts;
}

@Override
public StarCountHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.star_count_row,parent,false);
return new StarCountHolder(view);
}

@Override
public void onBindViewHolder(StarCountHolder holder, int position) {
StarCount model = starCounts.get(position);
Picasso.with(context)
.load("http://"+model.getImagePath())
.into(holder.starImage);
holder.actorName.setText(model.getName());
holder.counts.setText(""+model.getCount());
}

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

public class StarCountHolder extends RecyclerView.ViewHolder {
ImageView starImage;
TextView actorName,counts;
StarCount modelCount;
public StarCountHolder(View itemView) {
super(itemView);
starImage = (ImageView) itemView.findViewById(R.id.starCountIv);
actorName = (TextView) itemView.findViewById(R.id.acterName);
counts = (TextView) itemView.findViewById(R.id.counts);
}
}
}


help needed to solve this issue since i am struck up for more than a Day. Share thoughts to rectify my Error in the code and rectify my error.

Answer
public int selectedPosition  = -1   
public class StarCountHolder extends RecyclerView.ViewHolder {
    ImageView starImage;
    TextView actorName,counts;
    StarCount modelCount;
    public StarCountHolder(View itemView) {
        super(itemView);
        starImage = (ImageView) itemView.findViewById(R.id.starCountIv);
        actorName = (TextView) itemView.findViewById(R.id.acterName);
        counts = (TextView) itemView.findViewById(R.id.counts);
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectedPosition = getLayoutPosition());
                notifyDatasetChanged();
            }
        });
    }
}

Now you are done with the selected item position, change it in your binder

@Override
public void onBindViewHolder(StarCountHolder holder, int position) {
    StarCount model = starCounts.get(position);
    Picasso.with(context)
            .load("http://"+model.getImagePath())
            .into(holder.starImage);
    holder.actorName.setText(model.getName());
    holder.counts.setText(""+model.getCount());
    if(selectedPosition == position){
       // do whatever you want to do to make it selected.
    }
}

Now to get the selected item in your activity, you can do something like this...

inside activity

StartCount startC = starCounts.get(adapter.selectedPosition);

hope it helps