I am new to Android development. I have a an Activity that utilizes FragmentPagerAdapter to create tabs. Everything works fine except I want each tab to have its own unique fragment.xml file and right now all tabs display the same fragment (fragment_home.xml).

I have a suspicion the problem lies in this code:

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

        TextView textView = (TextView) rootView
            .findViewById(R.id.section_label);
        textView.setText(Integer.toString(getArguments()
            .getInt(ARG_SECTION_NUMBER)));

        return rootView; 
}

EDIT 1

I have added another Fragment class to represent another Fragment. It has its own OnCreateView() method which inflates an XML file called fragment_overview.xml and that works fine.

Now I am still stuck with my problem of wanting both fragments to be present in different tabs.

The code below creates an instance of OverViewFragment in its getItem() method. There is only one fragment returned so how do I make it multiple fragments?

public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class
        // below).
        return OverviewFragment.newInstance(position + 1);
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 5;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
        switch (position) {
        case 0:
            return getString(R.string.tab1).toUpperCase(l);
        case 1:
            return getString(R.string.tab2).toUpperCase(l);
        }
        return null;
    }
}
有帮助吗?

解决方案

Hi you have to create seprate fragments for each tab and link with your tab position. You can refer following repository for learn how to do it. FragmentPageAdapter With Tabs

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top