2

I have an issue, i am selecting a single item at a time with Recycler Gridview, when I click on another item last item should be unselected

3 Answers3

1

Here is the solution for your requirement

public class AdapterClass extends RecyclerView.Adapter<AdapterClass.ViewHolder> {
        private int selected_position = -1;

        @Override
        public void onBindViewHolder(AdapterClass.ViewHolder holder, final int position) {
            if (selected_position == position) {
                // do your stuff here like
                //Change selected item background color and Show sub item views

            } else {
                  // do your stuff here like
                  //Change  unselected item background color and Hide sub item views
            }
  // rest of the code here

    holder.linelayout.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              if(selected_position==position){
                        selected_position=-1;
                        notifyDataSetChanged();
                        return;
                    }
                    selected_position = position;
                    notifyDataSetChanged();

            }
        });

    //rest of the code here

     }


}
Quick learner
  • 10,632
  • 4
  • 45
  • 55
0

create a variable:

private var lastSub: View? = null

and on your onClickListener of the recyclerview item use like this:

override fun onClick(view: View) {
   //val subjects = view.tag as Subject
   //subId = subjects.id
   if (lastSub != view && lastSub != null) {
     lastSub!!.background = ResourcesCompat.getDrawable(resources, R.color.white, null)
     view.background = ResourcesCompat.getDrawable(resources, R.drawable.card_border, null)
     lastSub = view
   } else {
     view.background = ResourcesCompat.getDrawable(resources, R.drawable.card_border, null)
     lastSub = view
   }


}
0
            class AdapterClass extends RecyclerView.Adapter<AdapterClass.ViewHolder> {
             private int currentPosition = -1;
                private int previousPosition = -1;
             @Override
                public void onBindViewHolder(AdapterClass.ViewHolder holder, final int position) {
    holder.currentPos = position;


            if(currentPosition == -1) {
                    previousPosition = position;
                } else {

                }
            } else {
                if(previousPosition == position) {

                }
                if(currentPosition == position) {
                    previousPosition = currentPosition;
                } else {

                }
            }
        }
            }

// in view holder onClick you can notify previous and current
itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    notifyItemChanged(previousPosition);
                    currentPosition = currentPos;
                    notifyItemChanged(currentPosition);
                }
lucifer
  • 11
  • 1