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
Asked
Active
Viewed 434 times
2

Sandeep Sharma
- 43
- 1
- 6
-
kindly share your code – MurugananthamS Oct 15 '19 at 05:16
-
can you use model class for items ? if yes then show it – niceumang Oct 15 '19 at 05:20
-
No i am not using the model class @Niceumang – Sandeep Sharma Oct 15 '19 at 05:28
-
then you can handle this using boolean flag i means handle it with true and false value – niceumang Oct 15 '19 at 05:31
-
If possible then using model class its too easy for handling ! – niceumang Oct 15 '19 at 05:31
-
It would be appreciated if you share a sample code. – Rohit Rawat Oct 15 '19 at 05:33
-
hi @sandeep Sharma, I suggest you to use a flag as a boolean, return true if the item is clicked else false.. – Peer Mohamed Oct 15 '19 at 05:54
3 Answers
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
}
}

Tobibur Rahman
- 161
- 8
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