2013-11-27 136 views
3

這是此問題的後續處理ViewPager and fragments — what's the right way to store fragment's state?。我需要有人在代碼中解釋這部分答案。在viewpager中保存狀態

Another approach is to override FragmentPageAdapter.instantiateItem(View, int) and save a reference to the fragment returned from the super call before returning it (it has the logic to find the fragment, if already present).

我使用使用PagerAdapter從光標數據加載到viewpager。我已經覆蓋了PagerAdapter.intantiateItem。我需要的是能夠保存被點擊的按鈕的狀態。這是我的代碼

public class CustomQuestionPagerAdapter extends PagerAdapter { 

private Cursor cursor; 
private Cursor cursorCategory; 
private LayoutInflater inflater; 
private Context context; 
private ArrayList<String> optionsArray; 
String rightAnswer; 
String wrongAnswer1; 
String wrongAnswer2; 
Button option1; 
Button option2; 
Button option3; 

public CustomQuestionPagerAdapter(Context context, Cursor cursor1, Cursor cursor2) { 
    this.context = context; 
    this.cursor = cursor1; 
    this.cursorCategory = cursor2; 
    this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

}   

public void swapCursor(Cursor cursor) { 
    this.cursor = cursor; 
} 

public void swapAnotherCursor(Cursor cursor) { 
    this.cursorCategory = cursor; 
} 


public void destroyItem(View view, int position, Object object) { 
    ((ViewPager) view).removeView((ScrollView) object); 
} 

@Override 
public int getCount() { 
    if (cursor == null) { 
     return 0; 
    }else { 
     return cursor.getCount(); 
    } 
} 



/* (non-Javadoc) 
* @see android.support.v4.view.PagerAdapter#instantiateItem(android.view.View, int) 
*/ 


@Override 
public Object instantiateItem(View view, int position) { 

    cursor.moveToPosition(position); 
    final ViewPager pager = (ViewPager) view.findViewById(R.id.pager_nav); 
    ScrollView layout = (ScrollView) inflater.inflate(R.layout.question_activity, null); 
    if (cursorCategory != null && cursorCategory.moveToFirst()){ 
     String s = cursorCategory.getString(cursorCategory.getColumnIndex("name")); 
     ((TextView)layout.findViewById(R.id.text_category)).setText(s); 
    } 

    Typeface tpf = Typeface.createFromAsset(context.getAssets(), "Roboto-Light.ttf"); 
    String text = cursor.getString(cursor.getColumnIndex("question_text")); 
    TextView question_text = (TextView)layout.findViewById(R.id.text_question); 
    question_text.setText(text); 
    question_text.setTypeface(tpf); 

    String qPos = Integer.toString(position + 1) + "."; 
    TextView question_position = (TextView) layout.findViewById(R.id.text_position); 
    question_position.setText(qPos); 
    question_position.setTypeface(tpf); 

    this.rightAnswer = cursor.getString(cursor.getColumnIndex(DBHelper.RIGHT_ANSWER)); 
    this.wrongAnswer1 = cursor.getString(cursor.getColumnIndex(DBHelper.WRONG_ANSWER1)); 
    this.wrongAnswer2 = cursor.getString(cursor.getColumnIndex(DBHelper.WRONG_ANSWER2)); 

    optionsArray = new ArrayList<String>(); 
    optionsArray.clear(); 
    optionsArray.add(this.rightAnswer); 
    optionsArray.add(this.wrongAnswer1); 
    optionsArray.add(this.wrongAnswer2); 
    Collections.shuffle(optionsArray); 

    option1 = (Button) layout.findViewById(R.id.button_option1); 
    option1.setText((CharSequence) this.optionsArray.get(0)); 

    option2 = (Button) layout.findViewById(R.id.button_option2); 
    option2.setText((CharSequence) this.optionsArray.get(1)); 

    option3 = (Button) layout.findViewById(R.id.button_option3); 
    option3.setText((CharSequence) this.optionsArray.get(2)); 


    final Button next = (Button) layout.findViewById(R.id.next_button); 
    next.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      int j = getItem(+1) + 1; 
      Toast.makeText(context, context.getResources().getString(R.string.question_no) + " " + j, Toast.LENGTH_SHORT).show(); 
      pager.setCurrentItem(getItem(+1), true); 
      next.setBackgroundColor(0xffeeeeee); // i need to save this new bacground color 
     } 

     private int getItem(int i) { 
      return i += pager.getCurrentItem(); 
     } 
    }); 

我想這個答案太多,但它並沒有提前上班Saving Fragment state in ViewPager

感謝。

+1

什麼不使用偏好來保存你想要的數據? – Techfist

回答

1

動態保存在shared pref中點擊的button_id。您已經完成了所有工作,您需要從共享的pref中恢復按鈕狀態,並且當您單擊任何按鈕時,在共享前綴中添加按鈕。
更新:

public static final String PREFS_NAME = "MyPrefsFile"; 
------ 
final Button next = (Button) layout.findViewById(R.id.next_button); 
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); 
     boolean isClicked = settings.getBoolean(getCurrentItemPosition(), false); 
     if(isClicked) 
     { 
     next.setBackgroundColor(0xffeeeeee); 
     } 

    next.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      int j = getItem(+1) + 1; 
      Toast.makeText(context, context.getResources().getString(R.string.question_no) + " " + j, Toast.LENGTH_SHORT).show(); 
      pager.setCurrentItem(getItem(+1), true); 
      // i need to save this new bacground color 
     SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); 
     SharedPreferences.Editor editor = settings.edit(); 
     editor.putBoolean(getCurrentItemPosition(), true); 
     editor.commit(); 
     } 

     private int getItem(int i) { 
      return i += pager.getCurrentItem(); 
     } 
    }); 

getCurrentItemPosition()是當前itemposition。

+0

如果我能得到一些代碼,我將不勝感激。我理解sharedPreferences的概念,並且我在項目的其他部分使用了它,但我不知道如何獲取每個視圖的button_id。謝謝。 – Jimi

+0

你有尋呼機View_Id和相應的button_id->現在,當你將它保存到共享的pref保存爲你自定義的格式,比如'viewId_buttonId',並且在恢復時,如果button_id和view_id匹配,然後恢復。明白了我的觀點? – Yup

+0

不幸的是,我沒有。 – Jimi

1

PagerAdapter不會自動保存和恢復狀態恢復的意見。
您需要與FragmentStatePagerAdapter等效的視圖。 您可以選擇是自行實施還是擴展已有的組件。在GitHub上有一些:例如 https://github.com/NightlyNexus/ViewStatePagerAdapter
如果您需要回收視圖支架,甚至還有偉大的傑克沃頓https://github.com/JakeWharton/salvage視圖尋呼機適配器。 我已經使用前者作爲我的情況它適合更好,只需將您的PageAdapter替換爲新的ViewStatePagerAdapter並覆蓋createView(ViewGroup, int)destroyView(ViewGroup, int)
檢查示例很容易遵循。