FragmentPagerAdapter getItem位置错误 [英] FragmentPagerAdapter getItem wrong position

查看:99
本文介绍了FragmentPagerAdapter getItem位置错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在FramentPageAdapter中遇到了奇怪的问题

I've got strange problem with FramentPageAdapter

MainActivity.java

MainActivity.java

@SuppressLint("ValidFragment")
public class MainActivity<DashboardActivity> extends FragmentActivity implements     ActionBar.TabListener {
...
...
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
    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)).setTabListener(this).setIcon(R.drawable.rating_good));
    actionBar.addTab(actionBar.newTab().setText(mSectionsPagerAdapter.getPageTitle(1)).setTabListener(this).setIcon(R.drawable.action_search));
    actionBar.addTab(actionBar.newTab().setText(mSectionsPagerAdapter.getPageTitle(2)).setTabListener(this).setIcon(R.drawable.action_search));
}
...
public class SectionsPagerAdapter extends FragmentPagerAdapter {
    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }
    public Fragment getItem(int position) {
        Fragment fragment = null; 
        switch(position) {
            case 0:
                fragment = new Fragment0();  
            break;
            case 1:
                fragment = new Fragment1();  
            break;
            case 2:
                fragment = new Fragment2();  
            break;
        }
        return fragment;
    }
    @Override
    public int getCount() {
        return 3;
    }
    /*
     * Title
     */
    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
        switch (position) {
        case 0:
            return getString(R.string.title_section0).toUpperCase(l);
        case 1:
            return getString(R.string.title_section1).toUpperCase(l);
        case 2:
            return getString(R.string.title_section2).toUpperCase(l);
        }
        return null;
    }
}

当我尝试在3个选项卡之间切换时,

public Fragment getItem(int position)返回错误的位置.当我仅使用2个选项卡创建应用程序时,一切正常.然后再添加2个,这会带来一个奇怪的问题.从0切换到1位置-工作正常,从1切换到0-工作正常,从1到2位置切换-工作正常,但是,当我尝试从2转到1位置时,public Fragment getItem(int position)-int位置返回"0"而不是"1".有人帮我吗?

public Fragment getItem(int position) return wrong position when i try to switch between 3 tabs. When i create app with 2 tabs only, everything work just fine. Adding more then 2 of them, creating strange problem. Switch from 0 to 1 position - works fine, switch from 1 to 0 - works fine, switch from 1 to 2 posiition - works fine, but, when i try to go back from 2 to 1 position, public Fragment getItem(int position) - int position return "0" instead of "1". Does anyone help me with this ?

推荐答案

好,我找到了解决方案.首先,getItem"int位置"不指示当前显示片段.要显示3个或更多选项卡,而没有卸载firts片段,您必须添加以下行:

Ok, I've found the solution. First of all, the getItem "int position", doesn't indicate current display fragment. To display 3 or more tabs, without unload firts fragment You must added this line:

mViewPager.setOffscreenPageLimit(3);

故事的结尾...

这篇关于FragmentPagerAdapter getItem位置错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆