Short answer:
Context context;
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
context = recyclerView.getContext();
}
Explanation why other answers are not great:
Context
to the adapter is completely unnecessary, since RecyclerView
you can access it from inside the classContext
at ViewHolder
level means that you do it every time you bind or create a ViewHolder
. You duplicate operations.Activity
lifespan (which would be weird) then you already have a leak.