EDIT2 :我会更清楚,所以每个人都理解我的问题。我希望更多的人可以帮助我!
我所做的是一个包含片段和标签的应用程序,用于在片段之间切换。我还可以使用ViewPager在片段之间滚动/滑动。
现在唯一的问题是标签不会与片段一起滚动。因此,当我滑动到最后一个片段时,我看不到所选的标签,因为它超出了观察范围。
当我在片段之间滑动时,如何才能滚动到标签?
我想我必须对这段代码做点什么:
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
}
}
当我点击它们时,我所做的事情就会导致消失并重新燃烧:
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);
}
答案 0 :(得分:1)
我不知道答案,但是this guy为你正在做的事情实现了一个开源库。希望有所帮助!
答案 1 :(得分:1)
我感谢Josh(见他对答案的评论!)。非常感谢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);
}