如何使用 Fragments 在 TabLayout 中的每个选项卡中显示不同的布局

2023-12-31

我一直在尝试使用 PagerTabStrip 在可滑动的 TabLayout 中的不同选项卡中显示不同的布局。有人可以帮忙吗?

我想在第一个选项卡中显示一个布局,在第二个选项卡中显示第二个不同的布局等。

 public class MainActivity extends FragmentActivity {


// create object of FragmentPagerAdapter
SectionsPagerAdapter mSectionsPagerAdapter;

// viewpager to display pages
ViewPager mViewPager;

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

    // Create the adapter that will return a fragment for each of the five
    // primary sections of the app.
    mSectionsPagerAdapter = new SectionsPagerAdapter(
            getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);

}

/**
 * A FragmentPagerAdapter that returns a fragment corresponding to one of
 * the sections/tabs/pages.
 */
public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

    @SuppressLint("NewApi")
    @Override
    public Fragment getItem(int position) {

        switch (position) {
        case 0: {
            //Show 1st Layout(Here I need HELP)

             //HELP HELP HELP

        }case 1:
        {
            //Show 2nd Layout(Here I need HELP)

             //HELP HELP HELP
        } 
        default:
        }
        Fragment fragment = new DummySectionFragment();
        Bundle args = new Bundle();
        args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
        fragment.setArguments(args);
        return fragment;
    }

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

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
        case 0:
            return "Section 1";
        case 1:
            return "Section 2";
        case 2:
            return "Section 3";
        case 3:
            return "Section 4";
        case 4:
            return "Section 5";
        case 5:
            return "Section 6";
        }
        return null;
    }
}

/**
 * A dummy fragment representing a section of the app, but that simply
 * displays dummy text.
 */
public static class DummySectionFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    public static final String ARG_SECTION_NUMBER = "section_number";

    public DummySectionFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // Create a new TextView and set its text to the fragment's section
        // number argument value.
        TextView textView = new TextView(getActivity());
        textView.setGravity(Gravity.CENTER);
        textView.setTextSize(25);
        textView.setText(Integer.toString(getArguments().getInt(
                ARG_SECTION_NUMBER)));
        return textView;
    }
   }

 }

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

        switch (getArguments().getInt(ARG_SECTION_NUMBER))
        {
            case 1: {
                rootView = inflater.inflate(R.layout.fragment_bba, container, false);
                break;
            }
            case 2: {
                rootView = inflater.inflate(R.layout.fragment_bcom, container, false);
                break;
            }

            case 3: {
                rootView = inflater.inflate(R.layout.fragment_bca, container, false);
                break;
            }

        }
        return rootView;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 Fragments 在 TabLayout 中的每个选项卡中显示不同的布局 的相关文章

随机推荐