2013-10-31 44 views
4

在我的android應用程序中有一個swipeable tab佈局。它有兩個選項卡:新投訴和先前的投訴。當點擊抱怨標籤時,我需要啓動NewComplain.java。這裏是我NewComplain類代碼段:Android:在swipeable tab佈局中切換活動

NewComplain

public class NewComplain extends SherlockFragmentActivity implements View.OnClickListener { 
    private int Year; 
    private int Month; 
    private int Day; 
    private static final int DATE_DIALOG_ID = 1; 
    private View viewLoad; 
    private TextView heading, date; 
    private Spinner spiComplain, spiProduct; 
    private EditText description, contInfo; 
    private Button btnDate; 
    private ArrayAdapter<String> complainAdapter, prodAdapter; 
    private ArrayList<String> lstComplain = new ArrayList<String>(); 
    private ArrayList<String> lstProdNames = new ArrayList<String>(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     viewLoad = LayoutInflater.from(NewComplain.this).inflate(
       R.layout.new_complain, null); 
     setContentView(viewLoad);      
    } 
} 

這個id我TabsPagerAdapter類。

package xont.virtusel.v4.controller.sale; 

    import android.content.Intent; 
    import android.support.v4.app.Fragment; 
    import android.support.v4.app.FragmentManager; 
    import android.support.v4.app.FragmentPagerAdapter; 

    public class TabsPagerAdapter extends FragmentPagerAdapter { 

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

    @Override 
    public Fragment getItem(int index) { 

     switch (index) { 
     case 0: 

     case 1: 

     } 

     return null; 
    } 

    @Override 
    public int getCount() { 
     // get item count - equal to number of tabs 
     return 2; 
    } 

} 

這是我主要活動類。

package xont.virtusel.v4.controller.sale; 

    import xont.virtusel.v4.controller.R; 
    import android.app.ActionBar; 
    import android.app.ActionBar.Tab; 
    import android.app.FragmentTransaction; 
    import android.os.Bundle; 
    import android.support.v4.app.FragmentActivity; 
    import android.support.v4.view.ViewPager; 

    public class CustomerComplainActivity extends FragmentActivity implements ActionBar.TabListener { 

    private ViewPager viewPager; 
    private TabsPagerAdapter mAdapter; 
    private ActionBar actionBar; 
    private String[] tabs = { "New Complain", "Previous Complains" }; 

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

     // Initialization 
     viewPager = (ViewPager) findViewById(R.id.pager); 
     actionBar = getActionBar(); 
     mAdapter = new TabsPagerAdapter(getSupportFragmentManager()); 

     viewPager.setAdapter(mAdapter); 
     actionBar.setDisplayHomeAsUpEnabled(true); 
     //actionBar.setHomeButtonEnabled(true); 
     actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 
     //actionBar.setDisplayShowHomeEnabled(false); 
     //actionBar.setDisplayShowTitleEnabled(false); 

     // Adding Tabs 
     for (String tab_name : tabs) { 
      actionBar.addTab(actionBar.newTab().setText(tab_name) 
        .setTabListener(this)); 
     } 

     /** 
     * on swiping the viewpager make respective tab selected 
     * */ 
     viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { 

      @Override 
      public void onPageSelected(int position) { 
       // on changing the page 
       // make respected tab selected 
       actionBar.setSelectedNavigationItem(position); 
      } 

      @Override 
      public void onPageScrolled(int arg0, float arg1, int arg2) { 
      } 

      @Override 
      public void onPageScrollStateChanged(int arg0) { 
      } 
     }); 
    } 

    @Override 
    public void onTabReselected(Tab tab, FragmentTransaction ft) { 
    } 

    @Override 
    public void onTabSelected(Tab tab, FragmentTransaction ft) { 
     // on tab selected 
     // show respected fragment view 
     viewPager.setCurrentItem(tab.getPosition()); 
    } 

    @Override 
    public void onTabUnselected(Tab tab, FragmentTransaction ft) { 
    } 

} 

如何將切換活動語句添加到此TabsPagerAdapter類中。我用意圖。但它沒有奏效。 任何人都可以這麼友好地澄清這個問題。 Thanx提前

回答

1

究竟是什麼問題?在public Fragment getItem()方法

@Override 
    public Fragment getItem(int index) { 

     switch (index) { 
     case 0: 
      return new NewComplain(); 
     case 1: 
      //return some other class 
     } 

     return null; 
    } 

TabsPagerAdapterNewComplain

0

您要添加的片段,而不是活動創建一個構造函數:

如果您無法啓動新Fragment,試試這個。在您的Tab監聽器中,您將得到一個FragmentTransaction,您應該使用該FragmentTransaction來更新哪些片段相對於按下的選項卡顯示。

此外,不要在創建片段時膨脹,在創建視圖時這樣做。