Android滑动视图选项卡中滑动视图期间显示错误的操作栏菜单和标题

时间:2014-06-17 19:17:50

标签: android android-fragments menu swipeview android-actionbaractivity

您好我调试了我的代码,并且知道当我切换标签时,actionbarActivity中的getItem()函数调用了两次。

假设我有三个选项卡(每个选项卡由一个片段组成),我正在从第一个选项卡移动到第二个选项卡,getItem()方法调用第二个和第三个选项卡。片段内容显示正确但标题和菜单显示第三个标签。

任何人都可以帮我解决这个问题。我在谷歌搜索了很多但没有得到解决方案。

提前致谢。

这是我的代码:

公共类ActionBarTabActivity扩展了ActionBarActivity实现         ActionBar.TabListener {

private String token = "";
private String loggedInEmail = "";
private String userId = "";

SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_action_bar_tab);

    Intent intent = getIntent();
    loggedInEmail = intent.getStringExtra(Constants.LOGGED_IN_MAIL);
    token = intent.getStringExtra(Constants.TOKEN);
    userId = intent.getStringExtra(Constants.USER_ID);
    Log.d("session ", token);

    // Set up the action bar.
    final ActionBar actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    actionBar.setBackgroundDrawable(getResources().getDrawable(R.color.light_orange));
    mSectionsPagerAdapter = new SectionsPagerAdapter(
            getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);


    mViewPager
            .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                @Override
                public void onPageSelected(int position) {
                    actionBar.setSelectedNavigationItem(position);
                }
            });

    actionBar.addTab(actionBar.newTab()
            .setText(mSectionsPagerAdapter.getPageTitle(0)).setIcon(getResources().getDrawable(R.drawable.adduser))
            .setTabListener(this));
    actionBar.addTab(actionBar.newTab()
            .setText(mSectionsPagerAdapter.getPageTitle(1)).setIcon(getResources().getDrawable(R.drawable.dashboard))
            .setTabListener(this));
    actionBar.addTab(actionBar.newTab()
            .setText(mSectionsPagerAdapter.getPageTitle(2)).setIcon(getResources().getDrawable(R.drawable.report))
            .setTabListener(this));
}


@Override
public void onTabSelected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
    // When the given tab is selected, switch to the corresponding page in
    // the ViewPager.
    mViewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
}

@Override
public void onTabReselected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
}

public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class
        // below).
        //return PlaceholderFragment.newInstance(position + 1);
        Bundle args = new Bundle();
        args.putString(Constants.TOKEN, token);
        args.putString(Constants.LOGGED_IN_MAIL, loggedInEmail);
        args.putString(Constants.USER_ID, userId);
        Fragment fr = null;
        switch (position) {
        case 0:
            fr = new Fragment1();
            fr.setArguments(args);
            break;
        case 1:
            fr = new Fragment2();
            break;
        case 2:
            fr = new Fragment3();
            fr.setArguments(args);
            break;
        default:
            fr = new BlankFragment();
            break;
        }

        return fr;
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
        switch (position) {
        case 0:
            return getString(R.string.title_section1).toUpperCase(l);
        case 1:
            return getString(R.string.title_section2).toUpperCase(l);
        case 2:
            return getString(R.string.title_section3).toUpperCase(l);
        }
        return null;
    }
}

}

0 个答案:

没有答案