나는 많은 TextViews가있는 조각이 있습니다. ViewPager Tab에 4 개의 Fragment를 넣었습니다. Fragment 상태를 저장하는 방법에 대해 많이 검색하지만 모든 요소는 개별 요소 데이터를 저장 한 다음 데이터를 복원하고 TextViews 값을 다시 할당한다고 말합니다. 그러나 TextViews 값을 재 할당하는 작업은 시간이 많이 걸리기 때문에 View 요소 valus에 전체 Fragment 상태를 저장하고 완전히 복원해야합니다. 수있는 사람의 전화 나 어떻게 완벽한 조각의 상태를 저장하고 값을 할당 할 필요없이 복원하는 방법하시기 바랍니다 감사의전체 조각 저장 및 상태보기
이 내가 완전히 저장하지 않으려는 나의 조각 클래스입니다 :
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);
}
}
코드를 게시하십시오. –
@ Y.S 그는 지금 당장이나 자신의 목표를 달성하기 위해 의견을 원할만한 코드를 작성하지 않습니다 ..... – duggu
기본적으로 Fragment/FragmentManager 자체에서 수행해야합니다. Viewpages의 경우 FragmentStatePageAdapter를 Adapter로 사용해야하며 Fragment State를 저장합니다. (http://developer.android.com/reference/android/support/v4/app/FragmentStatePagerAdapter.html) – Christopher