gpt4 book ai didi

android - 在 viewpager 中保存 View 状态

转载 作者:太空狗 更新时间:2023-10-29 12:44:42 25 4
gpt4 key购买 nike

这是这个问题的跟进 ViewPager and fragments — what's the right way to store fragment's state? .我需要有人用代码来解释这部分答案。

另一种方法是覆盖 FragmentPageAdapter.instantiateItem(View, int) 并在返回之前保存对从 super 调用返回的 fragment 的引用(它具有查找 fragment 的逻辑,如果已经存在的话)。

我正在使用 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

提前致谢。

最佳答案

PagerAdapter 不会在状态恢复时自动保存和恢复 View 。
您需要等效于 FragmentStatePagerAdapter 但对于 View 。您可以选择是自己实现还是扩展已有的组件。 GitHub 上有一些:例如 https://github.com/NightlyNexus/ViewStatePagerAdapter .
如果您需要回收 View 支架,甚至还有来自伟大的 Jake Wharton 的 View 寻呼机适配器 https://github.com/JakeWharton/salvage .我使用前者,因为它更适合我的情况,只需将 PageAdapter 替换为新的 ViewStatePagerAdapter 并覆盖 createView(ViewGroup, int)destroyView(ViewGroup, int)
检查这个例子真的很容易理解。

关于android - 在 viewpager 中保存 View 状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20239273/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com