如何将数据从FragmentStatePagerAdapter传递到片段

时间:2015-02-01 11:26:38

标签: android android-fragments android-viewpager bundle fragmentstatepageradapter

嘿我想将数据从FragmentStatePagerAdapter传递到fragment。 我该怎么办?

FragmentStatePagerAdapter

public class ScreenSlidePagerActivity extends Fragment {
    /**
     * The number of pages (wizard steps) to show in this demo.
     */

    /**
     * The pager widget, which handles animation and allows swiping horizontally to access previous
     * and next wizard steps.
     */
    private ViewPager mPager;

    /**
     * The pager adapter, which provides the pages to the view pager widget.
     */
    private PagerAdapter mPagerAdapter;
    private String[] Titel;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        final View layout = inflater.inflate(R.layout.viewpage, container, false);

        ArrayList<String> strtext=getArguments().getStringArrayList("key");
        String frnames[]=strtext.toArray(new String[strtext.size()]);
        Titel = frnames;

        // Instantiate a ViewPager and a PagerAdapter.
        mPager = (ViewPager) layout.findViewById(R.id.viewPager);
        mPagerAdapter = new ScreenSlidePagerAdapter(getFragmentManager());
        mPager.setAdapter(mPagerAdapter);
        return layout;
    }
    /**
     * A simple pager adapter that represents 5 ScreenSlidePageFragment objects, in
     * sequence.
     */
    private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
        public ScreenSlidePagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return new ScreenSlidePageFragment();
        }

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

碎片

public class ScreenSlidePageFragment extends Fragment {

    String[] Titel;

    private static final Map<String, Integer> TITLE_IMAGES =
            new HashMap<String, Integer>();
    static {
        TITLE_IMAGES.put("Arme", R.drawable.ic_launcher);
        TITLE_IMAGES.put("Bauch", R.drawable.ic_launcher2);
    }

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



        return rootView;
    }
}

我想将TitelFragmentStatePagerAdapter传递到fragment

0 个答案:

没有答案