我在ViewPager中的片段中有一个RecyclerView。问题是,当视图恢复时,RecyclerView为null,我需要更新内容的适配器也为null。
如何在恢复Fragment时保存适配器状态以使其不为空?
代码
//Inside fragment:
public override void OnResume()
{
base.OnResume();
(Activity as MainActivity).mLocation.ChangeFragments(this);
}
//Change Fragments calls this method:
private async void HomeLocation()
{
JsonValue json;
json = await GetZmanimAsync();
(mFragment as HomeFragment).ParseAndDisplay(json);
foreach (var time in (mFragment as HomeFragment).times)
{
time.GotTime = true;
}
}
public void ParseAndDisplay(JsonValue json)
{
if (times == null)
{
times = new List<TimeItem>();
times.Add(new TimeItem() { TimeType = "עלות השחר", ImageResourceId = Resource.Drawable.alotIcon });
times.Add(new TimeItem() { TimeType = "זמן טו\"ת", ImageResourceId = Resource.Drawable.tutIcon });
times.Add(new TimeItem() { TimeType = "נץ החמה", ImageResourceId = Resource.Drawable.netzIcon });
times.Add(new TimeItem() { TimeType = "סו\"ז ק\"ש", ImageResourceId = Resource.Drawable.shmaIcon });
times.Add(new TimeItem() { TimeType = "סו\"ז תפילה", ImageResourceId = Resource.Drawable.DavenAnimation, isAnimation = true });
times.Add(new TimeItem() { TimeType = "זמן מנחה", ImageResourceId = Resource.Drawable.DavenAnimation, isAnimation = true });
times.Add(new TimeItem() { TimeType = "שקיעה", ImageResourceId = Resource.Drawable.shkiaaIcon });
times.Add(new TimeItem() { TimeType = "צאת הכוכבים", ImageResourceId = Resource.Drawable.TzetIcon });
}
zman = DateTime.Parse((json[0] as JsonObject).Values.ToList()[1]);//"עלות השחר 72 ד"]);
times[0].Time = zman.ToString("H:mm");
zman = DateTime.Parse((json[2] as JsonObject).Values.ToList()[1]);//DateTime.Parse(json["זמן טלית ותפילין"]);
times[1].Time = zman.ToString("H:mm");
zman = DateTime.Parse((json[3] as JsonObject).Values.ToList()[1]);//DateTime.Parse(json["זריחה"]);
times[2].Time = zman.ToString("H:mm");
zman = DateTime.Parse((json[5] as JsonObject).Values.ToList()[1]);//DateTime.Parse(json["סוף זמן ק\"ש (גר\"א)"]);
times[3].Time = zman.ToString("H:mm");
zman = DateTime.Parse((json[7] as JsonObject).Values.ToList()[1]);//DateTime.Parse(json["סוף זמן תפילה (גר\"א"]);
times[4].Time = zman.ToString("H:mm");
zman = DateTime.Parse((json[9] as JsonObject).Values.ToList()[1]);//DateTime.Parse(json["מנחה גדולה"]);
times[5].Time = zman.ToString("H:mm");
zman = DateTime.Parse((json[12] as JsonObject).Values.ToList()[1]);//DateTime.Parse(json["שקיעה"]);
times[6].Time = zman.ToString("H:mm");
zman = DateTime.Parse((json[13] as JsonObject).Values.ToList()[1]);//DateTime.Parse(json["צאת הכוכבים"]);
times[7].Time = zman.ToString("H:mm");
mAdapter.NotifyDataSetChanged();
}