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

时间:2014-11-25 19:37:15

标签: android android-fragments tabs android-viewpager android-tabs

我一直在尝试使用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;
    }
   }

 }

2 个答案:

答案 0 :(得分:8)

 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;

答案 1 :(得分:0)

对于想要使用设计模式解决此问题的人来说,这是好的  找到完整的工作解决方案Here

如果您根据if-else条件编写片段,则可以解决问题

    <body>
    <div id="wrapper">
        <nav id="navigation">

            <img id="logo" src="img/logo.png"/>

            <ul>
                <li><a class="current" href="index.html">Home</a></li>
                <li><a href="#">Text</a></li>
                <li><a href="#">Text</a></li>
                <li><a href="#">Text</a></li>
                <li><a href="#">Text</a></li>
            </ul>
        </nav>

        <div id="banner-text">
            <h1>h1 text</h1>
            <p>
                text text text text text text text text text text text text text text text text text text 
                text text text text text text text text text text text text text text text text text text 
            </p>
        </div>
    </div>

    <div id="main-content">
        <img src="https://livedemo00.template-help.com/wt_61406/images/post-32.jpg"/><img src="https://livedemo00.template-help.com/wt_61406/images/post-32.jpg"/>
   </div>
   
   <div id="main-content-2">
     <img src="https://livedemo00.template-help.com/wt_61406/images/post-34.jpg"/><img src="https://livedemo00.template-help.com/wt_61406/images/post-34.jpg"/>
   </div>

    <div id="footer">

    </div>

    </body>

但这种方法的问题在于将来,

  

1)您决定添加更多片段

  

2)您决定更改现有片段的某些功能

然后你必须修改现有的代码(if-else条件内)
不是首选的编程实践

相反,你可以遵循这种方法

switch(fragmentId)
    {
    case 1:
    {
      fragment 1 related stuff
    }
    case 2:
    {
    fragment 2 related stuff
    }
    .......
    .......
    and so on
  

你的片段实现

public abstract class BasicFragment extends Fragment {

public BasicFragment newInstance()
{
    Log.d("Rohit", "new Instance");
    Bundle args = new Bundle();
   // args.putInt(ARG_PAGE, page);
    BasicFragment fragment = provideYourFragment();
    fragment.setArguments(args);
    return fragment;

}

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
}

public View onCreateView(LayoutInflater inflater,ViewGroup parent, Bundle savedInstanseState)
{
    View view = provideYourFragmentView(inflater,parent,savedInstanseState);
    return view;
}

public abstract BasicFragment provideYourFragment();

public abstract View provideYourFragmentView(LayoutInflater inflater,ViewGroup parent, Bundle savedInstanceState);
}

快乐编码