Aan
Aan

Reputation: 308

Recyclerview single item selection not working correctly

I've implemented a recyclerview with staggered gridLayout containing about 31 items in the arrayList, recyclerview is working correctly, but I faced issue relating to single item selection.

When I select the value till "26" as shown in figure, its working fineenter image description here

But, when I select the value after "26", the values from top most item are also selected, as shown in this next figure.enter image description here

I require to only select one item at a time.

I've implemented the following code in my adapter class

public class DialogAdapter extends 
  RecyclerView.Adapter<DialogAdapter.DialogHolder>
 {
   // components
     public Context context;
     public ArrayList<AlertDialogModel> dialogArrayList = new 
          ArrayList<AlertDialogModel>();
     private final ArrayList<Integer> selected = new ArrayList<>();
     private int lastCheckedPosition = -1;
    public Interface interface;

    // parameterized constructor
    public DialogAdapter(Context context, ArrayList<AlertDialogModel> 
    dialogArrayList,Interface interface)
   {
       this.context = context;
       this.dialogArrayList = dialogArrayList;
       this.interface = interface;
   }
    @NonNull
   @Override
   public DialogHolder onCreateViewHolder(@NonNull ViewGroup parent, int 
   viewType)
   {
      View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_cardview,parent,false);
    DialogHolder dialogHolder = new DialogHolder(view);
    return dialogHolder;
}

@Override
public void onBindViewHolder(@NonNull final DialogHolder holder, final int position)
{
    final AlertDialogModel alertDialogModel = dialogArrayList.get(position);

    holder.textView.setText(alertDialogModel.getDisplayValue());
    if(lastCheckedPosition == position)
    {
        holder.textView.setTextColor(context.getResources().getColor(R.color.white));
        holder.textView.setBackground(context.getResources().getDrawable(R.drawable.circular_shape_selection));
    }
    else
    {

    }
    holder.textView.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {

            lastCheckedPosition = position;
            notifyDataSetChanged();
            holder.textView.setTextColor(context.getResources().getColor(R.color.white));
            holder.textView.setBackground(context.getResources().getDrawable(R.drawable.circular_shape_selection));

            interface.getSelectedValue(alertDialogModel.getDisplayValue());
        }
    });
}
@Override
public int getItemCount()
{
    return dialogArrayList.size();
}
public static class DialogHolder extends RecyclerView.ViewHolder
{
    public TextView textView;
    public DialogHolder(View itemView)
    {
        super(itemView);
        textView = (TextView)itemView.findViewById(R.id.textView);
     }
  }
}

Can anyone relate my code and identify the issue ?

Upvotes: 0

Views: 3260

Answers (2)

Alessandro Verona
Alessandro Verona

Reputation: 1257

you should put the text view to the original state:

if(lastCheckedPosition == position)
{
    holder.textView.setTextColor(context.getResources().getColor(R.color.white));
    holder.textView.setBackground(context.getResources().getDrawable(R.drawable.circular_shape_selection));
}
else
{ 
    holder.textView.setTextColor(context.getResources().getColor(R.color.transparent));
    holder.textView.setBackground(null));
}

Upvotes: 1

Bapusaheb Shinde
Bapusaheb Shinde

Reputation: 849

      holder.textView.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {

            lastCheckedPosition = position;
            notifyDataSetChanged();
            holder.textView.setTextColor(context.getResources().getColor(R.color.white));
            holder.textView.setBackground(context.getResources().getDrawable(R.drawable.circular_shape_selection));

            interface.getSelectedValue(alertDialogModel.getDisplayValue());


//below line is important to remove previous selected position from the variable
 lastCheckedPosition = -1;
        }
    });

Upvotes: 1

Related Questions