2012-01-16 69 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