You need just add field in your adapter where you will save currently active item. Then call notifyDataSetChanged
.
Also you should update your onBindViewHolder
like this:
private int currentPosition = -1;
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
holder.textView.setText(items.get(position));
if (currentPosition == position) {
holder.img1.setVisibility(View.INVISIBLE);
holder.img2.setVisibility(View.VISIBLE);
} else {
holder.img1.setVisibility(View.VISIBLE);
holder.img2.setVisibility(View.INVISIBLE);
}
}
public void setCurrentPosition(int currentPosition) {
this.currentPosition = currentPosition;
}
I’ve created a test project to show how it can be implemented. Here you can see how it works. Here is my github repository.
0
solved how to show and hide imageview inside recyclerView