Search code examples
androidandroid-fragmentsandroid-viewpagerfragmentpageradapter

Passing a List From the MainActivity to a Fragment


I have a viewPager activity that makes multiple fragments with custom FragmentPagerAdapter.

I need to get the List<Audio> audioList and use it in my fragment.

Here is a small part of my MainActivity where I make the fragments. I do not think the rest of the MainActivity code is required in order to answer this question.

    ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
    PagerAdapter pagerAdapter =
            new PagerAdapter(getSupportFragmentManager(), MainActivity.this, audioList);
    viewPager.setAdapter(pagerAdapter);

    // Give the TabLayout the ViewPager
    TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
    tabLayout.setupWithViewPager(viewPager);

    // Iterate over all tabs and set the custom view
    for (int i = 0; i < tabLayout.getTabCount(); i++) {
        TabLayout.Tab tab = tabLayout.getTabAt(i);
        tab.setCustomView(pagerAdapter.getTabView(i));
    }

Here is my custom FragmentPagerAdapter:

public class PagerAdapter extends FragmentPagerAdapter {

String tabTitles[] = new String[] { "Recommended", "Popular", "Rock", "Pop", "Blues", "Chill" };
Context context;
private List<Audio> audioList;

public PagerAdapter(FragmentManager fm, Context context, List<Audio> audioList) {
    super(fm);
    this.context = context;
    this.audioList = audioList;
}

@Override
public int getCount() {
    return tabTitles.length;
}

@Override
public Fragment getItem(int position) {

    switch (position) {
        case 0:
            return new BlankFragment();
        case 1:
            return new BlankFragment();
        case 2:
            return new BlankFragment();
        case 3:
            return new BlankFragment();
        case 4:
            return new BlankFragment();
        case 5:
            return new BlankFragment();
    }

    return null;
}

@Override
public CharSequence getPageTitle(int position) {
    // Generate title based on item position
    return tabTitles[position];
}


public View getTabView(int position) {
    View tab = LayoutInflater.from(context).inflate(R.layout.custom_tab, null);
    TextView tv = (TextView) tab.findViewById(R.id.custom_text);
    tv.setText(tabTitles[position]);
    return tab;
}

}

And I need to get the List<Audio> audioList into my Fragment here:

public class BlankFragment extends Fragment {

public BlankFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.content_view, container, false);


   //Get auidoList here 

    return rootView;
}

I also need to get the title of the tab in the future but my guess is that I would get it similarly to the audioList. If it is not then I could also use help on getting the tab title.

Additionally if the code style is wrong then I am always happy to have some feedback on it aswell.


Solution

  • The traditional way of doing this is by having an instance method that moves it all via a bundle.

    Like this:

    public class BlankFragment extends Fragment {
    
        public static BlankFragment newInstance(String title, ArrayList<Audio> audioList) {
    
            BlankFragment blankFragment = new BlankFragment();
    
            Bundle bundle = new Bundle();
            bundle.putString("Title", title);
            bundle.putParcelableArrayList("AudioList", audioList);
    
            blankFragment.setArguments(bundle);
    
            return blankFragment;
        }
    
        public BlankFragment() {
            // Required empty public constructor
        }
    
        private String title;
        private ArrayList<Audio> audioList;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            Bundle bundle = getArguments();
            if (bundle != null) {
                title = bundle.getString("Title");
                audioList = getArguments().getParcelableArrayList("AudioList");
            }
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.content_view, container, false);
    
    
            //Get auidoList here
    
            return rootView;
        }
    }
    

    Obviously your Audio class will need to implement parcelable for this to work. I found a great plugin (Android Parcelable Code Generator) in Android Studio for generating parcelable code that makes that quick and easy.