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);
}
嗯難治,它並不能幫助我,現在可是我盯着長此屏幕。也許有人得到一個想法。 它做一些事情的代碼,我認爲這一部分: '公共無效使用onPageSelected(INT位置){// TODO自動生成方法存根 this.mTabHost.setCurrentTab(位置); }' – MartijnG 2012-01-16 17:08:29
看起來您需要在onPageSelected中添加一些代碼,該代碼告訴您的HorizontalScrollView新選定的選項卡超出範圍,並滾動到它。 – Josh 2012-01-16 17:36:12
是的,但是如何?這就是問題;) – MartijnG 2012-01-16 17:47:26