将列表从 MainActivity 传递到片段

Passing a List From the MainActivity to a Fragment

我有一个 viewPager activity 可以使用自定义 FragmentPagerAdapter.

制作多个片段

我需要获取 List<Audio> audioList 并在我的 fragment 中使用它。

这是我 MainActivity 制作 fragments 的一小部分。我认为不需要 MainActivity 代码的其余部分来回答这个问题。

    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));
    }

这是我的习惯 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;
}

}

我需要在此处将 List<Audio> audioList 放入我的片段中:

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;
}

我还需要在将来获得 tabtitle,但我猜我会像 audioList 一样获得它。如果不是,那么我也可以使用帮助来获得 tab 标题。

此外,如果代码风格有误,我也很乐意收到一些反馈。

这样做的传统方法是使用一个实例方法,通过一个包移动它。

像这样:

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;
    }
}

显然,您的 Audio class 需要实施 parcelable 才能正常工作。我在 Android Studio 中找到了一个很棒的插件(Android Parcelable Code Generator),用于生成可打包代码,使之变得简单快捷。