2013-03-23 44 views
3

我試圖使用選項卡+刷卡在應用程序和要使用的導航類型「選項卡固定+掃」創建活動時的ADT提供了我。安卓:「導航類型:固定標籤+掃」

的sooo現在ADT吐出漂亮的代碼,我稍微修改...

我完全理解的代碼,這是怎麼回事......但我怎麼能教應用使用我的三個片段,而不是愚蠢的虛擬碎片? :(

我找不到它與ADT的「導航類型」涉及任何教程...

感謝您的幫助!

public class MainActivity extends FragmentActivity implements ActionBar.TabListener { 
SectionsPagerAdapter mSectionsPagerAdapter; 
ViewPager mViewPager; 

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

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

    // Create the adapter that will return a fragment for each of the three 
    // 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); 

    // 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); 
       } 
      }); 
    //Adding Tabs 
     actionBar.addTab(actionBar.newTab().setText("Tab 1").setTabListener(this)); 
     actionBar.addTab(actionBar.newTab().setText("Tab 2").setTabListener(this)); 
     actionBar.addTab(actionBar.newTab().setText("Tab 3").setTabListener(this)); 
} 

@Override 
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { 
    // When the given tab is selected, switch to the corresponding page in 
    // the ViewPager. 
    mViewPager.setCurrentItem(tab.getPosition()); 
} 

@Override 
public void onTabUnselected(ActionBar.Tab tab,FragmentTransaction fragmentTransaction) { 
} 

@Override 
public void onTabReselected(ActionBar.Tab tab,FragmentTransaction fragmentTransaction) { 
} 

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 DummySectionFragment (defined as a static inner class 
     // below) with the page number as its lone argument. 
     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 3 total pages. 
     return 3; 
    } 
} 

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) { 
     View rootView = inflater.inflate(R.layout.fragment_main_dummy,container, false); 
     TextView dummyTextView = (TextView) rootView.findViewById(R.id.section_label); 
     dummyTextView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER))); 
     return rootView; 
    } 
} 
} 
+0

我碰到一個稍微修改的解決方案,解決了代碼的ObjectFragment段來了。這對我來說很有用。 :) http://stackoverflow.com/questions/17380391/tabs-and-swipe-editing-the-getitem-to-display-3-different-fragments – 2013-10-26 16:40:23

回答

3

但我怎麼能教應用使用我的三個片段,而不是愚蠢的假破片?

你會發現,DummySectionFragment在的getItem()引用:

@Override 
public Fragment getItem(int position) { 
    // getItem is called to instantiate the fragment for the given page. 
    // Return a DummySectionFragment (defined as a static inner class 
    // below) with the page number as its lone argument. 
    Fragment fragment = new DummySectionFragment(); 
    Bundle args = new Bundle(); 
    args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1); 
    fragment.setArguments(args); 
    return fragment; 
} 

如果要使用不同的片段,修改getItem()回到你想要的,給予所提供的position(從0頁碼)的片段。

+0

謝謝,現在我改變了我的getItem()的開關情況取決於「位置」,並在我的MainActivity.java的末尾添加了三個類(每個片段一個)...有什麼方法可以解決我的問題更好或更好? :) – Jonas 2013-03-23 19:55:56

+0

@Jonas:因爲我不知道你認爲「更好或更好」,我不能真正回答這個問題。我通常將我的片段作爲單獨的公共Java類來使用,因爲它們可能會有點冗長。否則,你聽起來合理。 – CommonsWare 2013-03-23 19:59:16

+0

好的,然後:) Muchas gracias @CommonsWare! – Jonas 2013-03-23 20:00:52

5

用於設置碎片的開關箱很容易,並且非常清楚。讓您的每一個片段的膨脹在XML根視圖

@Override 
public Fragment getItem(int index) { 

    Fragment fragment = null; 
    switch(index){ 
    case 0: 
     fragment = new Fragment1(); 
     break; 
    case 1: 
     fragment = new Fragment2(); 
     break; 
    case 2: 
     fragment = new Fragment3(); 
     break; 
    default: 
     break; 
    } 

    //set args if necessary (which it isn't?) 
    Bundle args = new Bundle(); 
    args.putInt(ObjectFragment.ARG_OBJECT, index + 1); 
    fragment.setArguments(args); 

    //return fragment 
    return fragment; 
} 
+1

ObjectFragment是什麼意思? – 2013-08-07 10:11:37