<legend id='NjB6h'><style id='NjB6h'><dir id='NjB6h'><q id='NjB6h'></q></dir></style></legend>

      <bdo id='NjB6h'></bdo><ul id='NjB6h'></ul>

    <small id='NjB6h'></small><noframes id='NjB6h'>

  1. <tfoot id='NjB6h'></tfoot>

      <i id='NjB6h'><tr id='NjB6h'><dt id='NjB6h'><q id='NjB6h'><span id='NjB6h'><b id='NjB6h'><form id='NjB6h'><ins id='NjB6h'></ins><ul id='NjB6h'></ul><sub id='NjB6h'></sub></form><legend id='NjB6h'></legend><bdo id='NjB6h'><pre id='NjB6h'><center id='NjB6h'></center></pre></bdo></b><th id='NjB6h'></th></span></q></dt></tr></i><div id='NjB6h'><tfoot id='NjB6h'></tfoot><dl id='NjB6h'><fieldset id='NjB6h'></fieldset></dl></div>
    1. Android 选项卡式活动:带有 ViewPager 的操作栏选项卡:每个选项卡的布局不同

      时间:2023-10-04

    2. <legend id='yFs99'><style id='yFs99'><dir id='yFs99'><q id='yFs99'></q></dir></style></legend>
      <tfoot id='yFs99'></tfoot>

      <small id='yFs99'></small><noframes id='yFs99'>

          <bdo id='yFs99'></bdo><ul id='yFs99'></ul>

        • <i id='yFs99'><tr id='yFs99'><dt id='yFs99'><q id='yFs99'><span id='yFs99'><b id='yFs99'><form id='yFs99'><ins id='yFs99'></ins><ul id='yFs99'></ul><sub id='yFs99'></sub></form><legend id='yFs99'></legend><bdo id='yFs99'><pre id='yFs99'><center id='yFs99'></center></pre></bdo></b><th id='yFs99'></th></span></q></dt></tr></i><div id='yFs99'><tfoot id='yFs99'></tfoot><dl id='yFs99'><fieldset id='yFs99'></fieldset></dl></div>

                <tbody id='yFs99'></tbody>
              1. 本文介绍了Android 选项卡式活动:带有 ViewPager 的操作栏选项卡:每个选项卡的布局不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我完全停电了,我不熟悉使用片段.

                I have a total blackout, I am not familiar to work with fragments.

                我用 eclipse 助手创建了一个新的 Android 项目:

                I created with the eclipse assistant a new Android project:

                这里是创建的默认类的片段:

                here a snippet of the created default class:

                    /**
                 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
                 * one of the sections/tabs/pages.
                 */
                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 PlaceholderFragment.newInstance(position + 1);
                    }
                
                    @Override
                    public int getCount() {
                        // Show 3 total pages.
                        return 3;
                    }
                
                    @Override
                    public CharSequence getPageTitle(int position) {
                        Locale l = Locale.getDefault();
                        switch (position) {
                        case 0:
                            return getString(R.string.title_section1).toUpperCase(l);
                        case 1:
                            return getString(R.string.title_section2).toUpperCase(l);
                        case 2:
                            return getString(R.string.title_section3).toUpperCase(l);
                        }
                        return null;
                    }
                }
                
                /**
                 * A placeholder fragment containing a simple view.
                 */
                public static class PlaceholderFragment extends Fragment {
                    /**
                     * The fragment argument representing the section number for this
                     * fragment.
                     */
                    static int sec;
                    private static final String ARG_SECTION_NUMBER = "section_number";
                
                    /**
                     * Returns a new instance of this fragment for the given section number.
                     */
                    public static PlaceholderFragment newInstance(int sectionNumber) {
                        sec = sectionNumber;
                        Log.i("ARG_SECTION_NUMBER"," "+sec);
                        PlaceholderFragment fragment = new PlaceholderFragment();
                        Bundle args = new Bundle();
                        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
                        fragment.setArguments(args);
                        return fragment;
                    }
                
                    public PlaceholderFragment() {
                    }
                
                    @Override
                    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                            Bundle savedInstanceState) {
                        int jjj = sec;
                
                        Log.i("onCreateView"," "+sec);
                        View rootView = inflater.inflate(R.layout.fragment_main, container,
                                false);
                        return rootView;
                    }
                }
                

                SectionsPagerAdapter 用作适配器并返回 Fragments3 个片段的布局完全相同

                the SectionsPagerAdapter is used as an adapter and is returning Fragments the 3 fragments are having all the same layout

                fragment_main.xml
                

                如何将不同的布局分配给 3 个片段?

                how can different layouts assigned to the 3 fragments?

                PlaceholderFragment 包含一个带有 section_number 的 Bundle:

                the PlaceholderFragment contains a Bundle with the section_number:

                args.putInt(ARG_SECTION_NUMBER, sectionNumber);
                

                我可以使用此信息来确定要在每个片段中显示的布局吗?

                can I use this Info for determing wich layout to show in each fragment?

                    // Create the adapter that will return a fragment for each of the three
                    // primary sections of the activity.
                    mSectionsPagerAdapter = new SectionsPagerAdapter(
                            getSupportFragmentManager());
                
                    // Set up the ViewPager with the sections adapter.
                    mViewPager = (ViewPager) findViewById(R.id.pager);
                    mViewPager.setAdapter(mSectionsPagerAdapter);
                

                推荐答案

                你需要为每个页面制作不同的类.页面从片段延伸.您可以为每个片段加载不同的 layout-xml.

                You need to make different classes for each page. A page extends from a Fragment. You can just load a different layout-xml for every fragment.

                public class FirstFragment extends Fragment{
                
                @Override
                public View onCreateView(LayoutInflater inflater, ViewGroup container,
                        Bundle savedInstanceState) {
                
                
                    View rootView = inflater.inflate(R.layout.<yourxmlhere>, container,
                            false);
                    return rootView;
                }
                
                }
                

                您知道有片段,但您的适配器仍然需要知道哪个页面(位置)是哪个片段.这在以下函数中决定:

                You know have the fragment, but your adapter still needs to know which page (position) is which fragment. This is decided in the following function:

                @Override
                public Fragment getItem(int position) {
                    switch (position){
                    case 0:
                    //page 1
                    return new FirstFragment();
                    break;
                
                    case 1:
                    //page 2
                    return new SecondFragment();
                    break;
                    default:
                    //this page does not exists
                    return null;
                }
                

                确保您设置了正确的页数!

                Make sure you have set the correct amount of pages!

                @Override
                public int getCount() {
                    //the amount of pages your adapter knows
                    return <youramountofpages>;
                }
                

                这应该让你启动并运行.

                This should get you up and running.

                您可以删除整个 placeholderfragment 类.它不再需要了.

                You can just delete the whole placeholderfragment class. It is not needed anymore.

                这篇关于Android 选项卡式活动:带有 ViewPager 的操作栏选项卡:每个选项卡的布局不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:Android 2 ViewPagers 同时滚动 下一篇:Viewpager、光标和片段

                相关文章

                    <bdo id='I24Yx'></bdo><ul id='I24Yx'></ul>
                1. <i id='I24Yx'><tr id='I24Yx'><dt id='I24Yx'><q id='I24Yx'><span id='I24Yx'><b id='I24Yx'><form id='I24Yx'><ins id='I24Yx'></ins><ul id='I24Yx'></ul><sub id='I24Yx'></sub></form><legend id='I24Yx'></legend><bdo id='I24Yx'><pre id='I24Yx'><center id='I24Yx'></center></pre></bdo></b><th id='I24Yx'></th></span></q></dt></tr></i><div id='I24Yx'><tfoot id='I24Yx'></tfoot><dl id='I24Yx'><fieldset id='I24Yx'></fieldset></dl></div>

                  <small id='I24Yx'></small><noframes id='I24Yx'>

                2. <tfoot id='I24Yx'></tfoot>

                    <legend id='I24Yx'><style id='I24Yx'><dir id='I24Yx'><q id='I24Yx'></q></dir></style></legend>