1

我目前正在开发一个 android 项目,并创建了一个小测试项目来测试 Tab 的使用并通过使用水平滑动手势来更改选项卡。

除了一个问题外,该代码几乎可以正常工作。当我滑动内容以更改选项卡时,会显示第二个选项卡的活动,但未选择实际选项卡。即在 android 4.2 上,当应用程序加载时,它会启动主 FragmentActivity,然后显示位于选项卡 1 内的第一个片段(片段 1)。然后选项卡 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);
    }
}

感谢您的任何帮助,您可以提供。

4

1 回答 1

1

Add mTabHost.setCurrentTab(position) to the onPageSelected() method in your FragmentActivity.

于 2013-01-13T15:36:00.730 回答