Is there a better way of getting a reference to the parent RecyclerView from the adapter?
Asked Answered
S

2

102

I have a use case where I need a reference to the parent RecyclerView from inside the adapter, specifically inside the onBindViewHolder method. So far what I am doing is assigning it to a private class member in the onCreateViewHolder method passing along the viewGroup parent arg like so:

private ViewGroup mParent;

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    // inflater logic.
    mParent = parent;
}

And referencing the parent RecyclerView in onBindViewHolder like this:

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    // binder logic.
    ((RecyclerView)mParent).blahBlahBlah();
}

Is there a better way of doing this? Maybe RecyclerView.Adapter has a way that I may have missed?

Spheroid answered 6/7, 2015 at 14:30 Comment(0)
L
266

There's actually a specific method that callsback with the RecyclerView that attaches to the adapter. Just override the onAttachedToRecylerView(RecyclerView recyclerView) method.

public class Adapter_RV extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

    RecyclerView mRecyclerView; 


    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);

        mRecyclerView = recyclerView;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        mRecyclerView....
    }
Lavonlavona answered 6/7, 2015 at 15:50 Comment(5)
How did I miss this in the documentation! Thank you, this is exactly what I was looking for!Spheroid
The documentation says: "Keep in mind that same adapter may be observed by multiple RecyclerViews." developer.android.com/reference/android/support/v7/widget/… Correct me if I'm wrong but won't it create problem if you assign the adapter to more than one RecyclerView?Goal
@Goal yes it would. The code above would overwrite the previous RecyclerView instance. A solution would be to maintain a list of attached RecyclerView instances and remove instances in onDetachedFromRecyclerView(..). If possible, it would be best to avoid this and just create an instance of the adapter for each RecyclerView instance.Indraft
worth 200th like!Breach
I am not sure that an adapter should be aware of the consumer i.e recyclerView. The responsibility of an adapter is to maintain a list of items (ViewHolders in this case) regardless of who consumes it. My suggestion is to use a plain old callback to register on the recyclerview itselfLangelo
E
2

Another way is passing a reference in the constructor, e.g.

public final class MyAdapter extends RecyclerView.Adapper {
    private final recyclerView;

    public MyAdapter(@NonNull RecyclerView recyclerView) {
        this.recyclerView = recyclerView;
    }

    ...

    @Override
     public void onBindViewHolder(ViewHolder holder, int position) {
        ...
    }
}
Economically answered 6/7, 2015 at 14:51 Comment(1)
I don't recommect, you usually pass context,list of items, things get buzzy passing the recycler view tooUnwelcome

© 2022 - 2024 — McMap. All rights reserved.