0

我有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; 
    } 
} 

和片段类:

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.为什么?

感谢您的帮助!

回答

0

对于它的价值......

我改变了代码,以便在该阵列不再传递到通过捆绑的片段。我现在只传递片段的位置值(来自getItem(int position)方法),并且在片段膨胀后,数组在片段内“构建”。 我之前注意到的情况(fragposition对于第一个和第二个片段都是1)不再发生,应用程序正确显示信息。 为什么?不确定,但它有效。不过,我会继续调查,因为我不喜欢我找到的解决方案。

相关问题