2013-09-26 76 views
0

我正在使用操作欄Sherlock的應用程序Android。我目前有三個選項卡,包含以下類別的家用計算器和驅動器。行動欄中的標籤Sherlock

我之前做過一個計算器應用程序,我希望能夠在計算器選項卡中使用該代碼,我如何使用操作欄Sherlock選項卡中的活動?

這是我的看法尋呼機代碼:

package com.d4a.stzh; 

import com.d4a.stzh.FragmentTab1; 
import com.d4a.stzh.FragmentTab2; 
import com.d4a.stzh.FragmentTab3; 
import com.d4a.stzh.CMainActivity; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentPagerAdapter; 

public class ViewPagerAdapter extends FragmentPagerAdapter { 


    // Declare the number of ViewPager pages 
    final int PAGE_COUNT = 4; 

    public ViewPagerAdapter(FragmentManager fm) { 
     super(fm); 
    } 

    @Override 
    public Fragment getItem(int arg0) { 
     switch (arg0) { 

      // Open FragmentTab1.java 
     case 0: 
      FragmentTab1 fragmenttab1 = new FragmentTab1(); 
      return fragmenttab1; 

      // Open FragmentTab2.java 
     case 1: 
      FragmentTab2 fragmenttab2 = new FragmentTab2(); 
      return fragmenttab2; 

      // Open FragmentTab3.java 
     case 2: 
      FragmentTab3 fragmenttab3 = new FragmentTab3(); 
      return fragmenttab3; 
     } 
     return null; 
    } 

    @Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     return PAGE_COUNT; 
    } 

} 

這是我的MainActivity代碼:

package com.d4a.stzh; 

import android.os.Bundle; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentTransaction; 
import android.support.v4.view.ViewPager; 
import android.view.Window; 

import com.actionbarsherlock.app.ActionBar; 
import com.actionbarsherlock.app.ActionBar.Tab; 
import com.actionbarsherlock.app.SherlockFragmentActivity; 
import com.d4a.stzh.ViewPagerAdapter; 
import com.d4a.stzh.R; 

public class MainActivity extends SherlockFragmentActivity { 

    // Declare Variables 
    ActionBar mActionBar; 
    ViewPager mPager; 
    Tab tab; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     // Get the view from activity_main.xml 
     setContentView(R.layout.activity_main); 

     // Activate Navigation Mode Tabs 
     mActionBar = getSupportActionBar(); 
     getSupportActionBar().setDisplayShowTitleEnabled(false); 
     getSupportActionBar().setDisplayShowHomeEnabled(false); 
     mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

     // Locate ViewPager in activity_main.xml 
     mPager = (ViewPager) findViewById(R.id.pager); 

     // Activate Fragment Manager 
     FragmentManager fm = getSupportFragmentManager(); 

     // Capture ViewPager page swipes 
     ViewPager.SimpleOnPageChangeListener ViewPagerListener = new ViewPager.SimpleOnPageChangeListener() { 
      @Override 
      public void onPageSelected(int position) { 
       super.onPageSelected(position); 
       // Find the ViewPager Position 
       mActionBar.setSelectedNavigationItem(position); 
      } 
     }; 

     mPager.setOnPageChangeListener(ViewPagerListener); 
     // Locate the adapter class called ViewPagerAdapter.java 
     ViewPagerAdapter viewpageradapter = new ViewPagerAdapter(fm); 
     // Set the View Pager Adapter into ViewPager 
     mPager.setAdapter(viewpageradapter); 

     // Capture tab button clicks 
     ActionBar.TabListener tabListener = new ActionBar.TabListener() { 

      @Override 
      public void onTabSelected(Tab tab, FragmentTransaction ft) { 
       // Pass the position on tab click to ViewPager 
       mPager.setCurrentItem(tab.getPosition()); 
      } 

      @Override 
      public void onTabUnselected(Tab tab, FragmentTransaction ft) { 
       // TODO Auto-generated method stub 
      } 

      @Override 
      public void onTabReselected(Tab tab, FragmentTransaction ft) { 
       // TODO Auto-generated method stub 
      } 
     }; 

     // Create first Tab 
     tab = mActionBar.newTab().setText("Home").setTabListener(tabListener); 
     mActionBar.addTab(tab); 

     // Create second Tab 
     tab = mActionBar.newTab().setText("Books").setTabListener(tabListener); 
     mActionBar.addTab(tab); 

     // Create third Tab 
     tab = mActionBar.newTab().setText("Drive").setTabListener(tabListener); 
     mActionBar.addTab(tab); 
     // Create fourth Tab 
       tab = mActionBar.newTab().setText("Calculator").setTabListener(tabListener); 
       mActionBar.addTab(tab); 
    } 

} 

有什麼辦法插入一個活動到夏洛特片段標籤?

回答

1

供您參考。

片段必須始終嵌入到活動中,片段的生命週期直接受到宿主活動生命週期的影響。例如,當活動暫停時,其中的所有片段也是暫停的,並且當活動被破壞時,所有片段也是如此。

來源:http://developer.android.com/guide/components/fragments.html

不能在片段使用的活動。