2015-02-11 157 views
0

我有一个完全停电,我不熟悉使用碎片。Android选项卡式活动:使用ViewPager的动作栏选项卡:每个选项卡的布局不同

我与日食助理创建了一个新的Android项目:

enter image description here

这里创建的默认类的一个片段:

/** 
* 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用作适配器和正在恢复片段 3个片段具有全部相同的布局

fragment_main.xml 

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

的PlaceholderFragment包含捆绑与SECTION_NUMBER:

args.putInt(ARG_SECTION_NUMBER, sectionNumber); 

我可以用这个信息对于determing至极布局在每个片段中显示?

// 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); 
+1

请注意,操作栏选项卡在Android 5.0中已被弃用。您可能希望考虑其他选项卡解决方案,例如带有选项卡式指示符的“ViewPager”(例如,['PagerSlidingTabStrip'](https://github.com/astuetz/PagerSlidingTabStrip))。 – CommonsWare 2015-02-11 23:57:54

+0

谢谢你的回复。 eclipse中安装的唯一api是API21,SDK是最新的。为什么它根本不提供弃用的ActionBarTab? – user1616685 2015-02-12 01:03:27

+0

这只是一些模板,而且相当简单。 – CommonsWare 2015-02-12 02:02:48

回答

2

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

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; 
} 

} 

您知道有片段,但您的适配器仍然需要知道哪个页面(位置)是哪个片段。这取决于以下功能:

@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; 
} 

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

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

这应该让你启动并运行。

编辑:你可以删除整个placeholderfragment类。它不再需要了。

+0

谢谢我会试一试 – user1616685 2015-02-12 01:03:47

相关问题