sdk 22-> 23升级时,FragmentStatePagerAdapter第一次调用getItem错误 [英] FragmentStatePagerAdapter first call to getItem wrong with sdk 22-->23 upgrade

查看:169
本文介绍了sdk 22-> 23升级时,FragmentStatePagerAdapter第一次调用getItem错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新2:摆脱所有v4支持参考的问题已对其进行了修复. 更新:我从头开始,看看是什么触发了此行为.一旦我添加了位置权限检查,就会发生这种情况.我不能倒退-即使我删除了所有权限代码,它也会留在行为不当的FragmentStatePagerAdapger中.

UPDATE 2: Getting rid of all v4 support references fixed it. UPDATE: I started from scratch to see what triggers this behavior. It occurs once I add a check for location permissions. I can't go backwards -- even when I strip out all the permissions code it stays with the incorrectly-bahaving FragmentStatePagerAdapger.

我有一个FragementStatePagerAdapter,它对于动态创建的片段的ViewPager正常工作,直到使用appcompat-v7:23.2.1将compileSdkVersion和目标SdkVersion从22更改为23为止.现在,如果我尝试加载,例如A,B,C,它将加载B,B,C.但是如果我向后轻扫,则会得到C,B,A.因此,这只是最初尝试加载动态创建的片段A那是不成功的.

I have a FragementStatePagerAdapter that was working just fine for a ViewPager of dynamically created fragments until I changed my compileSdkVersion and target SdkVersion from 22 to 23, using appcompat-v7:23.2.1. Now if I attempt to load, say, A, B, C it loads B, B, C. But then if I swipe back I get C, B, A. So it is only the initial attempt to load dynamically-created fragment A that is unsuccessful.

这是我设置适配器和viewpager的方法:

Here is how I set my adapter and viewpager:

myAdapter = new MyAdapter(getSupportFragmentManager(), numItems);
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(myAdapter);
viewPager.setCurrentItem(position);

MyAdapter:

MyAdapter:

private class MyAdapter extends FragmentStatePagerAdapter {
    private final int size;

    public MyAdapter(FragmentManager fm, int _size)  {
        super(fm);
        size = _size;
    }

    @Override
    public int getCount() {
        return size;
    }

    @Override
    public Fragment getItem(int position) {
        String _id = myArray[position];
        return MyFragment.newInstance(_id);
    }
}

并实例化片段:

public static MyFragment newInstance(String _id)  {
        final MyFragment f = new MyFragment();
        final Bundle args = new Bundle();
        args.putString("_id", _id);
        f.setArguments(args);
        return f;
    }

...

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        _id = getArguments().getString("_id");            
    }

升级后,其他任何人都经历过吗?在这个轮子上转动了几个小时后,我全神贯注.

Has anyone else experienced this after upgrading? I am at a total loss after spinning my wheels on this for hours.

推荐答案

我作为评论发布的想法解决了这个问题,这里是相同的答案,但有更多详细信息...

The idea that I posted as a comment resolved the problem, here is the same answer with a few more details...

简短版本:在从FragmentStatePagerAdapter派生的适配器中,尝试使用FragmentManager而不是SupportFragmentManager. 除非您100%确定需要SupportFragmentManager .

Short version: in adapters derived from FragmentStatePagerAdapter, try to use FragmentManager instead of SupportFragmentManager. Unless you're 100% sure you need the SupportFragmentManager.

说明:

代码看起来不错. adapter可以混淆"片段的唯一地方是方法instantiateItem(ViewGroup container, int position).此方法使用作为 constructor 的参数传递的FragmentManager.因此,我们可以责怪那个可疑的SupportFragmentManager.

code in the question looks pretty good. The only place where adapter can 'confuse' fragments is the method instantiateItem(ViewGroup container, int position). This method uses a FragmentManager passed as an argument of constructor. So we can blame that suspicious SupportFragmentManager.

这篇关于sdk 22-> 23升级时,FragmentStatePagerAdapter第一次调用getItem错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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