2013-01-02 14 views
2

我已經創建了一個viewpager佈局,一個擴展FragmentActivity的類和一個片段。我想要的是每個片段都被傳遞到viewpager中的什麼位置。所以第一個viewpager被創建得到參數0,第二個獲得1等等。然後,如果我滾動這種或那種方式,這些數字仍然是一個真正的計數。ViewPager fragmentstatepageadapter getItem沒有通過正確的位置

問題是第一次創建一個片段,它似乎被創建兩次,所以傳遞的位置是0然後1.但是我不能回滾,但我知道肯定該類被調用兩次。現在當我向前滾動時,位置遞增1。但是,如果我回滾動立即下降到三隻一頁回來,然後繼續下跌近1比0所以現在我終於可以看到我的佈局0

我有這樣的:

private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter { 

    public ScreenSlidePagerAdapter(FragmentManager fm) { 
     super(fm); 
    } 

    @Override 
    public Fragment getItem(int position) { 
     PracticeFragment fragment = new PracticeFragment(); 
     getAll.putInt("position", position); 
     fragment.setArguments(getAll); 
     return fragment; 
    } 

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

它首先運行getItem兩次,甚至去我的片段類,以便位置爲0然後1。然後,當它到達我的片段類它使佈局正常,我滾動幾個(3或4)新頁面並且每當我向後滾動表示它是零個或兩個時,它就會向該位置添加一個,然後這些數字仍然是零星的。最後,當我滾動回到開始位置時,突然再次顯示0,所以我的位置0的片段突然顯示。

我不明白髮生了什麼,所以我想知道錯誤是什麼?還是同樣的問題 -

公共類PracticeFragment如果我註釋掉的一切,但一個ViewGroup,返回rootview延伸片段{

TextView question, explain; 
private ScrollView sv; 
private boolean starActionBar; 
private final static int version = Consts.SDKversion; 

ArrayList<RadioButton> rbArray; 
ArrayList<LinearLayout> lArray; 
ArrayList<ImageView> ivArray; 


int iRow; 

SQLite info; 
private String correctAnswer; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    info = new SQLite(getActivity()); 
    starActionBar = PreferenceManager.getDefaultSharedPreferences(
      getActivity()).getBoolean("star", true); 
    setHasOptionsMenu(true); 

} 

@Override 
public void onViewStateRestored(Bundle savedInstanceState) { 
    super.onViewStateRestored(savedInstanceState); 
    for (RadioButton r : rbArray) { 
     if (r.isChecked()) { 
      r.performClick(); 
     } 
    } 
} 

@Override 
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 
    inflater.inflate(R.menu.activity_pm_fragment, menu); 
    super.onCreateOptionsMenu(menu, inflater); 
} 

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

    ViewGroup rootView = (ViewGroup) inflater.inflate(
      R.layout.activity_main, container, false); 

    lArray = new ArrayList<LinearLayout>(); 
    rbArray = new ArrayList<RadioButton>(); 
    ivArray = new ArrayList<ImageView>(); 

    lArray.add((LinearLayout) rootView.findViewById(R.id.PM_LinLay0)); 
    lArray.add((LinearLayout) rootView.findViewById(R.id.PM_LinLay1)); 
    lArray.add((LinearLayout) rootView.findViewById(R.id.PM_LinLay2)); 
    lArray.add((LinearLayout) rootView.findViewById(R.id.PM_LinLay3)); 
    lArray.add((LinearLayout) rootView.findViewById(R.id.PM_LinLay4)); 

    for (LinearLayout l : lArray) { 
     l.setOnTouchListener(PracticeFragment.this); 
     l.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       if (((ViewGroup) v).getChildAt(0).isEnabled()) { 
        ((ViewGroup) v).getChildAt(0).performClick(); 
       } 
      } 
     }); 
    } 

    rbArray.add((RadioButton) rootView.findViewById(R.id.radio0)); 
    rbArray.add((RadioButton) rootView.findViewById(R.id.radio1)); 
    rbArray.add((RadioButton) rootView.findViewById(R.id.radio2)); 
    rbArray.add((RadioButton) rootView.findViewById(R.id.radio3)); 
    rbArray.add((RadioButton) rootView.findViewById(R.id.radio4)); 

    ivArray.add((ImageView) rootView.findViewById(R.id.ivradio0)); 
    ivArray.add((ImageView) rootView.findViewById(R.id.ivradio1)); 
    ivArray.add((ImageView) rootView.findViewById(R.id.ivradio2)); 
    ivArray.add((ImageView) rootView.findViewById(R.id.ivradio3)); 
    ivArray.add((ImageView) rootView.findViewById(R.id.ivradio4)); 


    rootView.findViewById(R.id.bNext).setVisibility(View.GONE); 
    rootView.findViewById(R.id.bPrevious).setVisibility(View.GONE); 
    sv = (ScrollView) rootView.findViewById(R.id.svMain); 

    info.open(); 
    iRow = Integer.valueOf(info.getEverything(getArguments(), getArguments().getInt("position"), "next")); 
    Cursor c = info.getCursor(iRow); 
    ((TextView) rootView.findViewById(R.id.tvQuestion)) 
      .setText((getArguments().getInt("position") + 1) + ") " + c.getString(2)); 

    explain = (TextView) rootView.findViewById(R.id.tvExplain); 
    explain.setText(c.getString(9)); 
    explain.setVisibility(View.GONE); 
    correctAnswer = c.getString(8); 


    String[] aArray = { c.getString(3), c.getString(4), c.getString(5), 
      c.getString(6), c.getString(7) }; 

    c.close(); 
    info.close(); 

    int o = 0; 
    int pos = 0; 
    for (String s : aArray) { 
     LinearLayout l = lArray.get(pos); 
     if (s.contentEquals("BLANK")) { 
      l.setVisibility(View.GONE); 
     } else { 
      l.setVisibility(View.VISIBLE); 
      rbArray.get(pos).setText(s); 
      rbArray.get(pos).setOnClickListener(null); 
      if (o % 2 == 0) { 
       l.setBackgroundColor(Consts.colorAlt); 
      } 
      o++; 
     } 
     pos++; 
    } 

    return rootView; 
} 

} 

不過。

+0

添加您的片段代碼也 –

+0

@bhaveshN添加:)還你幫我 – AndroidPenguin

+0

增加了更多的信息 – AndroidPenguin

回答

0

子類化它解決了問題!

+0

I'米有這個相同的問題你是什麼意思的「繼承它」 – Petro

+0

@AndroidPenguin你可以具體說明什麼代碼更改確切地解決了問題。 –

1

每次初始化GETALL中的getItem()的新對象

讓你的片段類的靜態

,並創建一個在PracticeFragment

static PracticeFragment newInstance(int num) { 
     PracticeFragment f = new PracticeFragment(); 

     // Supply num input as an argument. 
     Bundle args = new Bundle(); 
     args.putInt("num", num); 
     f.setArguments(args); 

     return f; 
    } 

方法和改變適配器

 @Override 
public Fragment getItem(int position) { 
     return PracticeFragment.newInstance(position); 
     } 
+0

這將返回相同的錯誤真的很感激:/ 捆綁ARGS =新包(); args.putInt(「position」,postition) Fragment fragment = new PracticeFragment(); fragment.setArguments(args); return fragment; – AndroidPenguin

+0

在片段 –

+0

中的oncreate方法中獲取參數請參閱此鏈接http://developer.android.com/reference/android/support/v4/app/FragmentStatePagerAdapter.html –

相關問題