My simple solution
Make a position holder:
public class PositionHolder {
private int position;
public PositionHolder(int position) {
this.position = position;
}
public int getPosition() {
return position;
}
public void setPosition(int position) {
this.position = position;
}
}
Just position or put data you need to get from activity.
Adapter constructor:
public ItemsAdapter(Context context, List<Item> items, PositionHolder positionHolder){
this.context = context;
this.items = items;
this.positionHolder = positionHolder;
}
In Activity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
selectedPosition = 0;
positionHolder = new PositionHolder(selectedPosition);
initView();
}
In Adapter onClickLictener in the item
in onBindViewHolder
holder.holderButton.setOnClickListener(v -> {
positionHolder.setPosition(position);
notifyDataSetChanged();
});
Now whenever you change position in RecyclerView it is hold in the Holder (or maybe it should be called Listener)
I hope it will be usefull
My first post ;P