Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling Fragment method from attached RecyclerView.Adapter

I recently started coding my first Android project using Android Studio 3.1.2 and SDK 19.

One of my fragments contains a RecyclerView with a custom RecyclerView.Adapter attached. On the CardView the Adapter gets by its ViewHolder, there can be a button. The target is, if the button is pressed, a method of my fragment should be called, though it's an instance of a custom subclass of Fragment:

From RequestingFragment:

public abstract class RequestingFragment extends Fragment implements RequestCallbacks {

    public final static void startRequest(final RequestOperation, String param) {
        //this is the guy i want to call
    }

    //these are the RequestCallbacks, they're all getting called in startRequest()
    public void onSuccess(JSONObject json, String parsingkey) { }

    public void onError() { }

    public void onFinished() { }

Now one of my RequestingFragments contains a RecyclerView, on which a custom ErrorCompactAdapter is attached. Inside the Adapters ViewHolder, where I load the layout for the single CardViews, there's a button, which should call startRequest() onClick from my RequestingFragment

From ErrorCompactAdapter:

public class ErrorCompactAdapter extends RecyclerView.Adapter<ErrorCompactAdapter.ErrorCompactViewHolder> {

    private Context context;
    private ArrayList<Error> errors;

    public ErrorCompactAdapter(Context context, ArrayList<Error> errors) {
        this.context = context;
        this.errors = errors;
    }
    
    public void onBindViewHolder(ErrorCompactViewHolder, int position) {
        //...
        holder.errorTakeOverButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //here's were i'm stuck
            }
        });
        //...
    }
}

My first approach was to change the context attribute of ErrorCompactAdapter to a RequestingFragment, so that I can call startRequest() on this.

private Context context; // private RequestingFragment attacher;

public void onClick(View v) {
    attacher.startRequest(/*params*/);
}

But i'm very unsure, if the fragment that contains the RecyclerView will be the one which receives the response of the request, or if a somehow "pseudo-anonymous" Fragment will receive the response and simply does nothing with it then. Can someone enlight me, if this is the correct path? Thanks in advance.

like image 379
procra Avatar asked Oct 09 '18 11:10

procra


People also ask

How can we call a method in fragment from adapter class?

Solution 1 : Make the adapter an inner class of your fragment, so that you can call the method directly. Solution 2 : Update your adapter constructor to accept the Fragment as a parameter. then you call methods using the fragment variable.

Can we use RecyclerView in fragment?

Set the Adapter to RecyclerView on Fragment class This class will call the layout that consists of the RecyclerView and connect the adapter with the RecyclerView. onViewCreated() will set the RecyclerAdapter to the RecyclerView layout.


1 Answers

Pass the Fragment in you ErrorCompactAdapter class's constructor. This works for me the way I want. I had the same issue.

RequestingFragment mFragment;

public ErrorCompactAdapter(Context context, ArrayList<Error> errors, 
                           RequestingFragment fragment) 
{
    this.context = context;
    this.errors = errors;
    this.mFragment = fragment;
}

// While passing the fragment into your adapter, do it this way.

ErrorCompactAdapter errorCompactAdapter = new ErrorCompactAdapter(
          context, errors, RequestingFragment.this);

holder.errorTakeOverButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // use you method of fragment here
        mFragment.startRequest();
    }
});
like image 172
Amit Jangid Avatar answered Oct 16 '22 16:10

Amit Jangid