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.爲什麼?
感謝您的幫助!