第一个和第二个片段显示相同的数据

First and second fragment showing same data

我有 6 个片段,都具有相同的布局和功能,每个片段的数据 [A,B,C,D,...] 不同。当创建前两个片段时,我查看 B、B,然后是 C,然后是 D...等等。从 C 向后滑动,我得到正确的数据查看 C、B、A。 我看不出我做错了什么。这是一些代码:

private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {

    private final Bundle fragmentBundle;

    public ScreenSlidePagerAdapter(FragmentManager fm, Bundle data) {
        super(fm);
        fragmentBundle = data;
    }

    @Override
    public Fragment getItem(int position) {
        // creating fragment and passing int position for array and array
        return SoundFrag.newInstance(position, fragmentBundle);
    }

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

和片段 class :

public class SoundFrag extends Fragment{
    // fragment Id Postion
    private int fragPosition;
    private static final String KEY_POSITION="fragIntIdP";

    static SoundFrag newInstance(int position, Bundle arrayIntS) {
        SoundFrag frag=new SoundFrag();
        arrayIntS.putInt(KEY_POSITION, position);
        frag.setArguments(arrayIntS);

        return(frag);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        // retrive data from bundle passed by activity
        Bundle argsIn = getArguments();
        SoundArrayParcel arrayToReceive = argsIn.getParcelable("dataResId");
        final int[][] arrayResId = arrayToReceive.getInt();
        fragPosition = argsIn.getInt("fragIntIdP");

        // inflate the fragment
        final ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.sounds_frag, container, false);

....... 我相信问题就在这里。 fragPosition 的值对于前两个片段始终为 1,而不是 0 然后 1。为什么?

感谢您的帮助!

物有所值...

我更改了代码,使数组不再通过 Bundle 传递给片段。我现在只传递片段的位置值(来自 getItem(int position) 方法),一旦它膨胀,数组就会在片段中 'constructed' 。 我之前注意到的情况(fragposition 对于第一个和第二个片段都是 1)不再发生并且应用程序正确显示信息。 为什么 ?不确定,但它有效。但是,我会继续调查,因为我不喜欢我找到的解决方案。