我目前正在开发一个Android项目并创建了一个小测试项目来测试Tab的使用并通过使用水平滑动手势来更改选项卡。
除了一个问题,代码几乎可以工作。当我滑动内容以更改选项卡时,将显示第二个选项卡的活动,但未选中实际选项卡。即在Android 4.2上,当应用程序加载时,它启动主FragmentActivity,然后显示第一个片段(片段1),它位于选项卡1中。然后,Tab 1在其下方有一条蓝线,表示选中了该选项卡。
如果我然后滑动屏幕,则显示第二个活动,但蓝线位于标签1下方,不会移动到标签2。
但是,如果我选择实际的选项卡而不是滑动,则蓝线会按预期移动到第二个选项卡上。
我怀疑我从我正在看的教程中遗漏了一些非常简单的东西,但我无法找到我的生活可能是什么。
以下是我为FragmentActivity编写的代码。这是应用程序首次启动时加载的内容。
public class TabsViewPagerFragmentActivity extends FragmentActivity
implements OnTabChangeListener, OnPageChangeListener{
private TabHost mTabHost;
private ViewPager mViewPager;
private HashMap<String, TabInfo> mapTabInfo = new HashMap<String, TabInfo>();
private PagerAdapter mPagerAdapter;
private class TabInfo
{
private String tag;
private Class<?> clss;
private Bundle bundle;
private Fragment fragment;
public TabInfo(String tag, Class<?> clss, Bundle bundle) {
this.tag = tag;
this.clss = clss;
this.bundle = bundle;
}
}
class TabFactory implements TabContentFactory
{
private final Context mContext;
public TabFactory (Context context)
{
mContext = context;
}
public View createTabContent(String tag)
{
View view = new View(mContext);
view.setMinimumWidth(0);
view.setMinimumHeight(0);
return view;
}
}
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.initialiseTabHost(savedInstanceState);
if (savedInstanceState != null)
{
mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab"));
}
this.initialiseViewPager();
}
protected void onSaveInstancestate(Bundle outState)
{
outState.putString("tab", mTabHost.getCurrentTabTag());
super.onSaveInstanceState(outState);
}
private void initialiseViewPager()
{
List<Fragment> fragments = new Vector<Fragment>();
fragments.add(Fragment.instantiate(this, Tab1Fragment.class.getName()));
fragments.add(Fragment.instantiate(this, Tab2Fragment.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 bundle)
{
mTabHost = (TabHost)findViewById(android.R.id.tabhost);
mTabHost.setup();
TabInfo tabInfo = null;
TabsViewPagerFragmentActivity.AddTab(this, this.mTabHost,
this.mTabHost.newTabSpec("Tab 1").setIndicator("Tab 1"),
tabInfo = new TabInfo("Tab1", Tab1Fragment.class, bundle));
TabsViewPagerFragmentActivity.AddTab(this, this.mTabHost,
this.mTabHost.newTabSpec("Tab 2").setIndicator("Tab 2"),
tabInfo = new TabInfo("Tab2", Tab2Fragment.class, bundle));
this.mapTabInfo.put(tabInfo.tag, tabInfo);
mTabHost.setOnTabChangedListener(this);
}
private static void AddTab(TabsViewPagerFragmentActivity activity, TabHost tabHost,
TabHost.TabSpec tabSpec, TabInfo tabInfo)
{
tabSpec.setContent(activity.new TabFactory(activity));
tabHost.addTab(tabSpec);
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
@Override
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
this.mViewPager.setCurrentItem(position);
}
@Override
public void onTabChanged(String tag) {
int pos = this.mTabHost.getCurrentTab();
this.mViewPager.setCurrentItem(pos);
}
}
以下是PageAdapter的代码
public class PagerAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments;
public PagerAdapter(FragmentManager fm, List<Fragment> fragments)
{
super(fm);
this.fragments = fragments;
}
public int getCount()
{
return this.fragments.size();
}
@Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
}
感谢您提供的任何帮助。
答案 0 :(得分:1)
将mTabHost.setCurrentTab(position)
添加到onPageSelected()
。{/ p>中的FragmentActivity
方法