在ViewPager中保存视图的状态

3
这是这个问题的跟进 ViewPager和Fragments - 存储Fragment状态的正确方式是什么?。我需要有人来解释答案中的代码部分。
另一种方法是重写FragmentPageAdapter.instantiateItem(View, int),并在返回前保存从super调用返回的fragment的引用(如果已经存在,则它具有找到fragment的逻辑)。
我正在使用PagerAdapter将数据从cursor加载到viewpager中。我已经重写了PagerAdapter.intantiateItem。我只需要能够保存被点击的Button的状态。这是我的代码。
    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();
        }
    });

我也尝试了这个答案,但它没有起作用。参考链接:在ViewPager中保存Fragment状态。非常感谢。

1
你为什么不使用偏好设置来保存你想要的数据呢? - Techfist
2个回答

2
PagerAdapter不会自动保存和恢复视图状态。您需要使用与FragmentStatePagerAdapter类似但适用于视图的相应解决方案。您可以选择自己实现或扩展已经可用的组件。在GitHub上有一些解决方案,例如https://github.com/NightlyNexus/ViewStatePagerAdapter。如果您需要回收视图持有者,甚至可以使用Jake Wharton大神的ViewPager适配器https://github.com/JakeWharton/salvage。我曾使用前者,因为它更加适合我的情况,只需将您的PageAdapter替换为新的ViewStatePagerAdapter并覆盖createView(ViewGroup, int)destroyView(ViewGroup, int)。查看示例非常容易。

1
动态保存所点击的按钮ID到共享首选项中。您已经完成了所有操作,只需要从共享首选项中恢复按钮状态,并在单击任何按钮时将该按钮添加到共享首选项中即可。
更新:
 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() 是当前项的位置。


我希望能够得到一些代码。我理解sharedPreferences的概念,并在项目的其他部分中使用它,但我不知道如何获取每个视图的button_id。谢谢。 - Jimi
你有pager的View_Id和对应的button_id->现在当你将它保存到shared pref中时,以自定义格式viewId_buttonId保存,恢复时如果button_id和view_id匹配则进行恢复。明白我的意思吗? - Pradip
很遗憾,我没有。 - Jimi
经过对您的代码进行一些修改,它现在可以正常工作。现在的问题是,当用户点击按钮时,它不会立即生效(直到用户在前后大约两个视图之间滑动),然后才返回该屏幕。如何解决这个问题? - Jimi
更新您的Pager适配器,以正确反映效果。 - Pradip

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接