Rafael
Rafael

Reputation: 1487

Use ArrayList in Activity as content for RecyclerView Adapter in frament

I have this Activity inside of which there are two fragments in a TabLayout.

In my Activity there is an static ArrayList. I'm my fist Fragment, let's call it FragmentOne I have one RecyclerView and the second Fragment, FragmentTwo i where I need the magic to happen.

In FragmentTwo, I'm calling that same ArrayList from my Activity and adding one or more items to it. In FragmentOne I'm also calling the same ArrayList and passing it's value to a non static ArrayList, the one that is used in my RecyclerView's adapter.

How I can call the method notifyDataSetChanged() of my FragmentOne's RecyclerView in my second Fragment, so the RecyclerView would update it's content?

Or there is a more efficient way of sending the item to my fist Fragment ArrayList that I use in my RecyclerView Adapter?

My Activity:

public class MainActivity {

    TabLayout tabLayout;
    ViewPager viewPager;
    ViewPagerAdapter viewPagerAdapter;
    FragmentOne fragmentone;

    // The ArrayList used as FragmentOne RecyclerView's Adapter
    public static ArrayList<Contacts> contactsArrayList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        fragmentOne = new FragmentOne();

        contactsArrayList = new ArrayList<>();

    }

}

FragmentOne RecyclerView:

public class FragmentOne extends Fragment implements View.OnClickListener {

    // RecyclerView's adapter
    ContactsAdapter ctcAdapter;

    // RecyclerView
    RecyclerView rvContacts;

    public FragmentOne() {
        // Required empty public constructor

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_one, container, false);

        // RecyclerView
        rvContacts = (RecyclerView) view.findViewById(R.id.rvContacts);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rvContacts.setLayoutManager(linearLayoutManager);

        // Here I'm adding the static list from the Main Activity in the adapter
        ctcAdapter = new ContactsAdapter(getContext(), MainActivity.contactsArrayList);

        rvContacts.setAdapter(ctcAdapter);

        return view;
    }
}

The only thing I need is how to call the ctcAdapter.notifyDataSetChanged() in my second fragment, after I add a new Item to the contactsArrayList to make the list in the firs fragment show the added item.

Upvotes: 2

Views: 625

Answers (1)

Michael Hays
Michael Hays

Reputation: 6908

There are lots of ways to do this. One way would be to make FragmentOne implement a listener. Something like:

public interface ContactsChangedListener {
   void onContactsChanged();
}

public class FragmentOne implements ContactsChangedListener ... {
   ...
   @Override
   void onContactsChanged() {
     ctcAdapter.NotifyDataSetChanged();
   }
}

In FragmentTwo, provide a way to pass the listener:

public class FragmentTwo ... {
   ...
   private ContactsChangedListener mContactsChangedListener;
   ...
   public void setContactsChangedListener(
     ContactsChangedListener listener) {
     mContactsChangedListener = listener;
   }
}

And then, in onCreate of MainActivity, you can just pass the one to the other as an interface:

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    fragmentOne = new FragmentOne();
    ...
    fragmentTwo = new FragmentTwo();
    fragmentTwo.setContactsChangedListener(fragmentOne);
    contactsArrayList = new ArrayList<>();
    ...
}

It's nothing fancy. But it does the trick, and it maintains encapsulation.

Upvotes: 2

Related Questions