保存完整片段并查看状态
Save Complete Fragment and View State
我有包含很多 TextView 的 Fragment。我把这个片段的 4 个放在 ViewPager 选项卡中。我搜索了很多关于保存片段状态的信息,但他们都说要保存单个元素数据,然后恢复它们的数据并重新分配 TextViews 值。但是我需要保存完整的 Fragment 状态及其 View 元素值并完全恢复它,因为重新分配 TextViews 值非常耗时。
谁能告诉我如何保存完整的片段状态并在不需要重新分配值的情况下恢复它?请
谢谢
这是我要完整存储的片段 Class:
public class SampleFragment extends Fragment{
private TextView[] month_days;
private TextView[] weeks;
private TextView[] week_days;
private TableRow[] tb_row;
private TextView month_name;
protected boolean MONTH_NAME_VISIBILITY = false;
protected boolean WEEK_NUMBER_VISIBILITY = false;
private Configurations conf = new Configurations();
private View rootView;
@Override
public void onCreate(Bundle savedInstanceState) {
setRetainInstance(true);
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.month_view, container, false);
initialize(this.getActivity());
setDate();
return rootView;
}
private void initialize(Context context) {
month_days = new TextView[42];
weeks = new TextView[6];
week_days = new TextView[7];
tb_row = new TableRow[7];
month_name = (TextView) rootView.findViewById(R.id.month_name);
Integer i, j, id;
for (i = 0; i < 42; i++) {
id = rootView.getResources().getIdentifier("tv" + (i + 1) + "", "id",conf.getPACKAGE_NAME() );
month_days[i] = (TextView) rootView.findViewById(id);
}
for (i = 0; i < 6; i++) {
id = rootView.getResources().getIdentifier("tvw" + (i + 1) + "", "id", conf.getPACKAGE_NAME());
weeks[i] = (TextView) rootView.findViewById(id);
weeks[i].setTextAppearance(activity, R.style.SmallFontBold);
}
for (i = 0; i < 7; i++) {
id = rootView.getResources().getIdentifier("tvwd" + (i + 1) + "", "id", conf.getPACKAGE_NAME());
week_days[i] = (TextView) rootView.findViewById(id);
week_days[i].setText(conf.getDayName()[i]);
week_days[i].setTextAppearance(activity, R.style.SmallFontBold);
if (Arrays.asList(conf.getWEEK_FREE_DAYS_INDEX()).contains(String.valueOf(i)))
week_days[i].setTextColor(Color.RED);
id = getResources().getIdentifier("row" + (i + 1) + "", "id", conf.getPACKAGE_NAME());
tb_row[i] = (TableRow) rootView.findViewById(id);
}
}
public void setDate() {
Calendar cal = Calendar.getInstance();
cal.setFirstDayOfWeek(conf.getCurrentCal().getFirstDayOfWeek());
Integer i, j = 0, wn, backday, z;
cal.set(conf.getCurrentCal().get(YEAR), conf.getCurrentCal().get(MONTH), 1);
backday = ((cal.get(DAY_OF_WEEK)) - cal.getFirstDayOfWeek() + 7) % 7;
wn = (int) (Math.ceil((backday + cal.getActualMaximum(DAY_OF_MONTH)) / 7.0));
cal.add(DAY_OF_WEEK, -backday);
for (i = 0; i < wn * 7; i++) {
month_days[i].setText(String.valueOf(cal.get(DAY_OF_MONTH)));
if (Arrays.asList(conf.getWEEK_FREE_DAYS_INDEX()).contains(String.valueOf(cal.get(DAY_OF_WEEK))))
month_days[i].setTextColor(Color.RED);
else
month_days[i].setTextColor(Color.BLACK);
if (cal.get(MONTH) != conf.getCurrentCal().get(MONTH))
month_days[i].setTextAppearance(this.getActivity(), R.style.InActiveSmall);
else
month_days[i].setTextAppearance(this.getActivity(), R.style.SmallFont);
cal.add(DAY_OF_MONTH, 1);
}
if (WEEK_NUMBER_VISIBILITY) {
cal.set(conf.getCurrentCal().get(YEAR), conf.getCurrentCal().get(MONTH), 1);
i = cal.get(WEEK_OF_YEAR);
z = 0;
for (j = 0; j < wn; j++) {
weeks[j].setText(String.valueOf(cal.get(WEEK_OF_YEAR)));
if ((i == 52) && (z == Calendar.DECEMBER))
weeks[j].setText(String.valueOf(53));
weeks[j].setVisibility(VISIBLE);
i = cal.get(WEEK_OF_YEAR);
z = cal.get(MONTH);
cal.add(WEEK_OF_YEAR, 1);
}
} else {
cal.set(conf.getCurrentCal().get(YEAR), conf.getCurrentCal().get(MONTH), 1);
for (j = 0; j < 6; j++) {
weeks[j].setVisibility(INVISIBLE);
cal.add(WEEK_OF_YEAR, 1);
}
}
for (j = 1; j <= 6; j++)
if (j > wn)
tb_row[j].setVisibility(INVISIBLE);
else
tb_row[j].setVisibility(VISIBLE);
if (MONTH_NAME_VISIBILITY) {
month_name.setText(conf.getMonthName()[conf.getCurrentCal().get(MONTH)]);
month_name.setVisibility(VISIBLE);
}
else
month_name.setVisibility(INVISIBLE);
}
public void setConf(Configurations conf) {
this.conf.setCurrentCal(conf.getCurrentCal());
this.conf.setCAL_FIRST_WEEK_DAY(conf.getCAL_FIRST_WEEK_DAY());
this.conf.setDayName(conf.getDayName());
this.conf.setMonthName(conf.getMonthName());
this.conf.setPACKAGE_NAME(conf.getPACKAGE_NAME());
this.conf.setTodayCal(conf.getTodayCal());
this.conf.setWEEK_FREE_DAYS_INDEX(conf.getWEEK_FREE_DAYS_INDEX());
}
public void setmonth(Calendar cal){
Calendar tcal = getInstance();
tcal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
this.conf.setCurrentCal(tcal);
}
}
我有包含很多 TextView 的 Fragment。我把这个片段的 4 个放在 ViewPager 选项卡中。我搜索了很多关于保存片段状态的信息,但他们都说要保存单个元素数据,然后恢复它们的数据并重新分配 TextViews 值。但是我需要保存完整的 Fragment 状态及其 View 元素值并完全恢复它,因为重新分配 TextViews 值非常耗时。 谁能告诉我如何保存完整的片段状态并在不需要重新分配值的情况下恢复它?请 谢谢
这是我要完整存储的片段 Class:
public class SampleFragment extends Fragment{
private TextView[] month_days;
private TextView[] weeks;
private TextView[] week_days;
private TableRow[] tb_row;
private TextView month_name;
protected boolean MONTH_NAME_VISIBILITY = false;
protected boolean WEEK_NUMBER_VISIBILITY = false;
private Configurations conf = new Configurations();
private View rootView;
@Override
public void onCreate(Bundle savedInstanceState) {
setRetainInstance(true);
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.month_view, container, false);
initialize(this.getActivity());
setDate();
return rootView;
}
private void initialize(Context context) {
month_days = new TextView[42];
weeks = new TextView[6];
week_days = new TextView[7];
tb_row = new TableRow[7];
month_name = (TextView) rootView.findViewById(R.id.month_name);
Integer i, j, id;
for (i = 0; i < 42; i++) {
id = rootView.getResources().getIdentifier("tv" + (i + 1) + "", "id",conf.getPACKAGE_NAME() );
month_days[i] = (TextView) rootView.findViewById(id);
}
for (i = 0; i < 6; i++) {
id = rootView.getResources().getIdentifier("tvw" + (i + 1) + "", "id", conf.getPACKAGE_NAME());
weeks[i] = (TextView) rootView.findViewById(id);
weeks[i].setTextAppearance(activity, R.style.SmallFontBold);
}
for (i = 0; i < 7; i++) {
id = rootView.getResources().getIdentifier("tvwd" + (i + 1) + "", "id", conf.getPACKAGE_NAME());
week_days[i] = (TextView) rootView.findViewById(id);
week_days[i].setText(conf.getDayName()[i]);
week_days[i].setTextAppearance(activity, R.style.SmallFontBold);
if (Arrays.asList(conf.getWEEK_FREE_DAYS_INDEX()).contains(String.valueOf(i)))
week_days[i].setTextColor(Color.RED);
id = getResources().getIdentifier("row" + (i + 1) + "", "id", conf.getPACKAGE_NAME());
tb_row[i] = (TableRow) rootView.findViewById(id);
}
}
public void setDate() {
Calendar cal = Calendar.getInstance();
cal.setFirstDayOfWeek(conf.getCurrentCal().getFirstDayOfWeek());
Integer i, j = 0, wn, backday, z;
cal.set(conf.getCurrentCal().get(YEAR), conf.getCurrentCal().get(MONTH), 1);
backday = ((cal.get(DAY_OF_WEEK)) - cal.getFirstDayOfWeek() + 7) % 7;
wn = (int) (Math.ceil((backday + cal.getActualMaximum(DAY_OF_MONTH)) / 7.0));
cal.add(DAY_OF_WEEK, -backday);
for (i = 0; i < wn * 7; i++) {
month_days[i].setText(String.valueOf(cal.get(DAY_OF_MONTH)));
if (Arrays.asList(conf.getWEEK_FREE_DAYS_INDEX()).contains(String.valueOf(cal.get(DAY_OF_WEEK))))
month_days[i].setTextColor(Color.RED);
else
month_days[i].setTextColor(Color.BLACK);
if (cal.get(MONTH) != conf.getCurrentCal().get(MONTH))
month_days[i].setTextAppearance(this.getActivity(), R.style.InActiveSmall);
else
month_days[i].setTextAppearance(this.getActivity(), R.style.SmallFont);
cal.add(DAY_OF_MONTH, 1);
}
if (WEEK_NUMBER_VISIBILITY) {
cal.set(conf.getCurrentCal().get(YEAR), conf.getCurrentCal().get(MONTH), 1);
i = cal.get(WEEK_OF_YEAR);
z = 0;
for (j = 0; j < wn; j++) {
weeks[j].setText(String.valueOf(cal.get(WEEK_OF_YEAR)));
if ((i == 52) && (z == Calendar.DECEMBER))
weeks[j].setText(String.valueOf(53));
weeks[j].setVisibility(VISIBLE);
i = cal.get(WEEK_OF_YEAR);
z = cal.get(MONTH);
cal.add(WEEK_OF_YEAR, 1);
}
} else {
cal.set(conf.getCurrentCal().get(YEAR), conf.getCurrentCal().get(MONTH), 1);
for (j = 0; j < 6; j++) {
weeks[j].setVisibility(INVISIBLE);
cal.add(WEEK_OF_YEAR, 1);
}
}
for (j = 1; j <= 6; j++)
if (j > wn)
tb_row[j].setVisibility(INVISIBLE);
else
tb_row[j].setVisibility(VISIBLE);
if (MONTH_NAME_VISIBILITY) {
month_name.setText(conf.getMonthName()[conf.getCurrentCal().get(MONTH)]);
month_name.setVisibility(VISIBLE);
}
else
month_name.setVisibility(INVISIBLE);
}
public void setConf(Configurations conf) {
this.conf.setCurrentCal(conf.getCurrentCal());
this.conf.setCAL_FIRST_WEEK_DAY(conf.getCAL_FIRST_WEEK_DAY());
this.conf.setDayName(conf.getDayName());
this.conf.setMonthName(conf.getMonthName());
this.conf.setPACKAGE_NAME(conf.getPACKAGE_NAME());
this.conf.setTodayCal(conf.getTodayCal());
this.conf.setWEEK_FREE_DAYS_INDEX(conf.getWEEK_FREE_DAYS_INDEX());
}
public void setmonth(Calendar cal){
Calendar tcal = getInstance();
tcal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
this.conf.setCurrentCal(tcal);
}
}