Mark F
Mark F

Reputation: 1543

Design Issue - Communicating Between RecyclerView and Fragments

I have one Activity and two Fragments, Fragment A and Fragment B. Fragment A contains a RecyclerView. Within it's adapter(MovieRecyclerAdapter) I have an Interface that relays the the ID of the column clicked back to Fragment A.

Here is the interface in the Adapter:

private MovieClickedListener movieClickedListener;

public interface MovieClickedListener{
    void onCLicked(int Id);
}

public MovieRecyclerAdapter(MovieClickedListener listener){
    this.movieClickedListener = listener;
}

I implement this in my Fragment Class in order to retrieve the data.

implements MovieRecyclerAdapter.MovieClickedListener

I now have the data. Here's my issue, I need to pass it along to Fragment B. I know that I can implement ANOTHER interface that communicates between Fragment A and my Activity, pass it there, and then in my Activity retrieve it and pass the id to Fragment B.

I'm wondering if that's the correct strategy or if there is a more efficient way of getting the clicked id from the RecyclerView in FragmentA to Fragment B?

I tried implementing the interface from my RecyclerView directly in my Activity instead of FragmentA but nothing occurs when I click an item now.

Here is the clickedListener in the Activity with a simple Log statement.

public class MainActivity extends AppCompatActivity implements MovieRecyclerAdapter.MovieClickedListener {   @Override
public void onCLicked(int Id) {
    Log.v("TAG","In MainActivity")
}}

The issue if I do it this, implement the clickedListener in the Activity and not the Fragment, when I instantiated my RecyclerView in my Fragment, I can no longer pass "this" as the constructor parameter to the adapter.

movieRecyclerAdapter = new MovieRecyclerAdapter(this);

I've tried all types of Contexts and it wont work

Upvotes: 2

Views: 1503

Answers (4)

Nika Kurdadze
Nika Kurdadze

Reputation: 2512

You can pass the activity callback as a parameter this way :

movieRecyclerAdapter = new MovieRecyclerAdapter((MovieRecyclerAdapter.MovieClickedListener) getActivity());

Upvotes: 1

beeb
beeb

Reputation: 1615

To the last section of your question:

If you want to implement the interface in your activity you can add this code in your fragment:

@Override
public void onAttach(Context context) {
  super.onAttach(context);

  if (context instanceof MovieRecyclerAdapter.MovieClickedListener){
    // save the interface in a field 
    callback = (MovieRecyclerAdapter.MovieClickedListener) context;
  }
}

Then you can init your adapter like this:

movieRecyclerAdapter = new MovieRecyclerAdapter(callback);

Now what i recommend:

If I unterstand you right you want to click on an item of the recyclerview and open/switch to another fragment. In my app I have a similar problem and solved it like this:

I have a recyclerview where the items represent a movie object. I always work with movie ids. The real movie objects were managed by a singleton manager class. So if i click on an item I create a new Intent and pass the movie id in a bundle to the new activity. If i want to access the movie object I have to call something like manager.getInstance().getMovie(movieid);. So i use the passed movieid to access the real object.

Upvotes: 2

Sanjay Kumar
Sanjay Kumar

Reputation: 1185

Let FragmentB has implemented MovieClickedListener

Your Activity has two instance for both fragments say fragA and fragB in Activity

In activity call as

fragA.setMovieClickedListener(fragB);

Add method in FragmentA

public void setMovieClickedListener(MovieClickedListener listener){
 adapter.setMovieClickedListener(listner);
}

Now your adapter has listener for FragmentB

Note : adapter should be created first in fragmentA

Upvotes: 0

Kirankumar Zinzuvadia
Kirankumar Zinzuvadia

Reputation: 1249

Have you passed listener between fragments or activity in bundle ? If yes, it will create another instance of listener on receiver side.

Now in your case, you need to check about observable and observer. https://stackoverflow.com/a/40627973/3728591

Upvotes: 0

Related Questions