2012-01-16 56 views
1

EDIT2:我會更清楚,所以每個人都明白我的問題。我希望更多的人能幫助我!Android滾動到Tabs

我做了什麼是一個應用程序,它包含片段和標籤之間切換片段。我也可以使用ViewPager在片段之間滾動/滑動。

現在唯一的問題是標籤不會隨着片段一起滾動。因此,當我滑動到最後一個片段時,我看不到選定的選項卡,因爲它已超出查看範圍。

如何在碎片之間滑動時滾動到Tabs?

我想我必須做一些與這部分代碼:

public void onPageSelected(int position) { 
    // TODO Auto-generated method stub 
    this.mTabHost.setCurrentTab(position); 
} 

但我沒有任何線索,我都試過了不起作用。爲了更容易,這裏是代碼的兩個重要部分。我希望有人能幫助我!

我的主要代碼:

public class MainActivity extends FragmentActivity implements TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener { 

    private TabHost mTabHost; 
    private ViewPager mViewPager; 
    private HashMap<String, TabInfo> mapTabInfo = new HashMap<String, MainActivity.TabInfo>(); 
    private PagerAdapter mPagerAdapter; 

    private class TabInfo { 
     private String tag; 
     TabInfo(String tag, Class<?> clazz, Bundle args) { 
      this.tag = tag; 
     } 
    } 

    class TabFactory implements TabContentFactory { 
     private final Context mContext; 

     public TabFactory(Context context) { 
      mContext = context; 
     } 

     public View createTabContent(String tag) { 
      View v = new View(mContext); 
      v.setMinimumWidth(0); 
      v.setMinimumHeight(0); 
      return v; 
     } 
    } 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.tabs_layout); 
     this.initialiseTabHost(savedInstanceState); 
     if (savedInstanceState != null) { 
      mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab")); 
     } 
     this.intialiseViewPager(); 
    } 

    protected void onSaveInstanceState(Bundle outState) { 
     outState.putString("tab", mTabHost.getCurrentTabTag()); 
     super.onSaveInstanceState(outState); 
    } 

    private void intialiseViewPager() { 
     List<Fragment> fragments = new Vector<Fragment>(); 
     fragments.add(Fragment.instantiate(this, TabFragment1.class.getName())); 
     fragments.add(Fragment.instantiate(this, TabFragment2.class.getName())); 
     fragments.add(Fragment.instantiate(this, TabFragment3.class.getName())); 
     fragments.add(Fragment.instantiate(this, TabFragment4.class.getName())); 
     fragments.add(Fragment.instantiate(this, TabFragment5.class.getName())); 
     this.mPagerAdapter = new PagerAdapter(super.getSupportFragmentManager(), fragments); 
     // 
     this.mViewPager = (ViewPager)super.findViewById(R.id.viewpager); 
     this.mViewPager.setAdapter(this.mPagerAdapter); 
     this.mViewPager.setOnPageChangeListener(this); 
    } 

    private void initialiseTabHost(Bundle args) { 
     mTabHost = (TabHost)findViewById(android.R.id.tabhost); 
     mTabHost.setup(); 
     TabInfo tabInfo = null; 
     MainActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab1").setIndicator("Home"), (tabInfo = new TabInfo("Tab1", TabFragment1.class, args))); 
     this.mapTabInfo.put(tabInfo.tag, tabInfo); 
     MainActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab2").setIndicator("Messages"), (tabInfo = new TabInfo("Tab2", TabFragment2.class, args))); 
     this.mapTabInfo.put(tabInfo.tag, tabInfo); 
     MainActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab3").setIndicator("Search"), (tabInfo = new TabInfo("Tab3", TabFragment3.class, args))); 
     this.mapTabInfo.put(tabInfo.tag, tabInfo); 
     MainActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab4").setIndicator("Profile"), (tabInfo = new TabInfo("Tab4", TabFragment4.class, args))); 
     this.mapTabInfo.put(tabInfo.tag, tabInfo); 
     MainActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab5").setIndicator("Settings"), (tabInfo = new TabInfo("Tab5", TabFragment5.class, args))); 
     this.mapTabInfo.put(tabInfo.tag, tabInfo); 
     mTabHost.setOnTabChangedListener(this); 
    } 

    private static void AddTab(MainActivity activity, TabHost tabHost, TabHost.TabSpec tabSpec, TabInfo tabInfo) { 
     tabSpec.setContent(activity.new TabFactory(activity)); 
     tabHost.addTab(tabSpec); 
    } 

    public void onTabChanged(String tag) { 
     int pos = this.mTabHost.getCurrentTab(); 
     this.mViewPager.setCurrentItem(pos); 
    } 

    public void onPageScrolled(int position, float positionOffset, 
      int positionOffsetPixels) { 
     // TODO Auto-generated method stub 
    } 

    public void onPageSelected(int position) { 
     // TODO Auto-generated method stub 
     this.mTabHost.setCurrentTab(position); 
    } 

    public void onPageScrollStateChanged(int state) { 
     // TODO Auto-generated method stub 
    } 
} 

我做了什麼至今導致選項卡DISAPEAR和REAPEAR當我點擊他們:

public void onPageSelected(int position) { 
    // TODO Auto-generated method stub 
    this.mTabHost.setCurrentTab(position); 

    this.hScrollView = (HorizontalScrollView)super.findViewById(R.id.scroller); 
    this.hScrollView.scrollTo(((int)(mTabHost.getWidth() * (position/((double)(mTabHost.getChildCount() - 1))))), 0); 
} 

回答

1

我不知道答案,但this guy爲你正在做的事情實現了一個開源庫。希望有所幫助!

+0

嗯難治,它並不能幫助我,現在可是我盯着長此屏幕。也許有人得到一個想法。 它做一些事情的代碼,我認爲這一部分: '公共無效使用onPageSelected(INT位置){// TODO自動生成方法存根 this.mTabHost.setCurrentTab(位置); }' – MartijnG 2012-01-16 17:08:29

+1

看起來您需要在onPageSelected中添加一些代碼,該代碼告訴您的Horizo​​ntalScrollView新選定的選項卡超出範圍,並滾動到它。 – Josh 2012-01-16 17:36:12

+0

是的,但是如何?這就是問題;) – MartijnG 2012-01-16 17:47:26

1

我修好了,感謝喬希(見他的回答評論!)。非常感謝Josh!

它現在的工作很好,這是我使用它的代碼:

public void onPageSelected(int position) { 
    // TODO Auto-generated method stub 
    this.mTabHost.setCurrentTab(position); 
    this.hScrollView = (HorizontalScrollView)super.findViewById(R.id.scroller); 
    this.hScrollView.scrollTo(((int)(mTabHost.getWidth() * (position/((double)(4 - 1))))), 0); 
} 
+0

你應該將findViewById調用移動到onCreate,所以你不會一直調用它。與mTabHost.getWidth()一樣。 – Josh 2012-01-18 13:32:37

+0

另外,您應該點擊旁邊的綠色箭頭將我的答案標記爲已接受。像你提供的更新應該是對原始問題的編輯,而不是一個新的答案。祝你好運! – Josh 2012-01-18 13:34:28