2014-01-28 97 views
0

我正在使用Android Studio的预先创建的示例。通过转到一个新项目找到它,通过滑动选择具有操作栏选项卡导航样式的空白项目。从片段寻呼机适配器更改片段的显示

它显然工作,它会更改editText的文本,以表示您正在查看的选项卡号。

令人困惑的是我无法找到如何为每个片段指定不同的布局。最终,我想为我的3个选项卡分别设置一个完全不同的屏幕。我以为我会创建三个独立的片段,每个片段都有自己的布局,但在这里似乎这是通过示例在飞行中完成的。

有没有人玩过这个例子,可以让我知道如何使用XML查看器/设计器来定制每个片段?

回答

2

下面是您需要在示例中进行的更改或者可以创建新项目。

1.Write你要在你的导航选项卡显示像

Fragment1, Fragment2,... 

片段将是所有的碎片的超类的所有意见的片段,而不是活动。

为您的每个片段的创建XML布局,并覆盖您的片段类像onCreateView方法:

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

    // Get the view from respectivelayout.xml 

    View view = inflater.inflate(R.layout.respectivelayoput, container, false); 

    // Do whatever you want to do like an activity here for all the tabs 

    return rootView; 
    } 

2.创建它覆盖FragmentPagerAdapter的选项卡之间滑动,这样

东西的新类
public class ViewPagerAdapter extends FragmentPagerAdapter { 

    // Declare the number of ViewPager pages 
    final int PAGE_COUNT = 2; 
    Context context; 

    public ViewPagerAdapter(FragmentManager fm, Context context) { 
     super(fm); 
     this.context = context; 
    } 

    @Override 
    public Fragment getItem(int position) { 
     switch (position) { 
      // Open FragmentTab1.java 
      case 0: 
       FragmentTab1 fragmenttab1 = new FragmentTab1(context); 
       return fragmenttab1; 
      // Open FragmentTab2.java 
      case 1: 
       FragmentTab2 fragmenttab2 = new FragmentTab2(context); 
       return fragmenttab2; 
      //And so on.., make sure cases must be equal to page count you specified 
     } 
     return null; 
    } 

    @Override 
    public int getCount() { 
     return PAGE_COUNT; 
    } 

} 

3.Now为您的活动只拿着viewpager里面像(我使用的片段支持V4库)创建一个XML布局

<android.support.v4.view.ViewPager 
     android:id="@+id/pager" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"></android.support.v4.view.ViewPager> 

4.Now这里你MainActivity.java的代码段(在exapmle没有同改变)

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

     // Set up the action bar. 
     final ActionBar actionBar = getSupportActionBar(); 
     actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

     // Create the adapter that will return a fragment for each of the three 
     // primary sections of the activity. 
     yourAdaperInstance= new YourFragmentPagerAdapter(getSupportFragmentManager()); 

     // Set up the ViewPager with the sections adapter. 
     mViewPager = (ViewPager) findViewById(R.id.pager); //created in xml 

     mViewPager.setAdapter(mSectionsPagerAdapter); 

     // When swiping between different sections, select the corresponding 
     // tab. We can also use ActionBar.Tab#select() to do this if we have 
     // a reference to the Tab. 
     mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { 
      @Override 
      public void onPageSelected(int position) { 
       actionBar.setSelectedNavigationItem(position); 
      } 
     }); 

     // For each of the sections in the app, add a tab to the action bar. 
     for (int i = 0; i < yourAdaperInstance.getCount(); i++) { 
      // Create a tab with text corresponding to the page title defined by 
      // the adapter. Also specify this Activity object, which implements 
      // the TabListener interface, as the callback (listener) for when 
      // this tab is selected. 
      actionBar.addTab(
        actionBar.newTab() 
          .setText(mSectionsPagerAdapter.getPageTitle(i)) 
          .setTabListener(this)); 
     } 

     // getCount and getPageTitle are defined in your FragmentPagerAdapter, This is the best practice or else you can add tabs and define names everything in Activity also 
    } 

您需要设置TabListener很好,但它自带的例子没有什么需要改变的。

对于Studio示例,所有这些类(MainActivity,YourFragmentPagerAdapter,Fragment1,Fragment2 ..)都在相同的Activity类中,但您可以将它们全部分开,我更喜欢这样。

相关问题