gpt4 book ai didi

android - 如何在按下后退按钮时恢复整个 fragment View ?

转载 作者:行者123 更新时间:2023-11-29 02:38:46 24 4
gpt4 key购买 nike

我第一次需要从服务器拉取数据。当从另一个 fragment 按下后退 button 时,应该恢复以前的 View ,但不会再次重新创建 View 并再次从服务器拉取数据。我的部分 fragment 如下:

AttendanceFragment.cs

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
.......
if (savedInstanceState == null)
{
rootView = inflater.Inflate(Resource.Layout.AttendanceListview, container, false);
}
.........
// this is where I get data from WebServer
GetClassSection(); // I am confused here where to put this method and pull data from server



listView.ItemClick += OnListItemClick;




return rootView;
}

public override void OnSaveInstanceState(Bundle outState)
{
base.OnSaveInstanceState(outState);
outState.PutString("_dataGotFromServer", dataGotFromServer);
}

当从 button 登陆这个 fragment 时,点击我用这种方式替换了 fragment

 public void ReplaceFragment(Context context, Fragment newFragment, string TAG)
{
Android.Support.V4.App.FragmentManager fragmentManager = ((FragmentActivity)context).SupportFragmentManager;
Android.Support.V4.App.FragmentTransaction ft = fragmentManager.BeginTransaction();
ft.Replace(Resource.Id.HomeFrameLayout, newFragment);
ft.AddToBackStack(TAG);
ft.Commit();

}

BaseAdapter 在这里初始化

 public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
dataList = new List<DA_ClassSectionAttendance>();
attendanceAdapter = new AttendanceListAdapter(this.Activity, dataList);


// Create your fragment here
//set whether MenuOption show/hide from toolbar
HasOptionsMenu = true;
}

并且我的适配器设置在这里

  public override void OnActivityCreated(Bundle savedInstanceState)
{
base.OnActivityCreated(savedInstanceState);
dataGotFromServer = savedInstanceState.GetString("_dataGotFromServer"); // savedInstanceState always gets null here

listView.Adapter = attendanceAdapter;
}

我的数据是从服务器中提取并在 ui 中更新的

 private async void GetClassSection()
{
try
{ ...........

Activity.RunOnUiThread(async () => {
await Task.Delay(1 * 100);

attendanceAdapter.NotifyDataSetChanged();
progress.Visibility = ViewStates.Gone;
});

}
catch (Java.Lang.Exception e)
{

}
}

谢谢。

最佳答案

When back button pressed from another fragment, previous view should be restored but not recreate the view again and pull the data from server again.

当按下后退按钮时,如果您不希望 fragment 恢复数据并重新创建 View ,请使用 FragmentTransaction.Add() 方法而不是 FragmentTransaction.Replace() 会是更好的选择。

当您使用 Replace 方法时,它将替换添加到容器中的现有 fragment 。这基本上与调用 Remove(Fragment) 为所有当前添加的 fragment 调用相同的 containerViewId 然后调用 Add(int, Fragment, String) 与此处给出的参数相同。所以当你想重用一些 fragment 时,你必须考虑恢复数据,这会有点复杂。

但是当您使用Add 方法时,它会向 Activity 状态添加一个 fragment 。此 fragment 也可以选择将其 view(如果 Fragment.onCreateView 返回非 null)放入 activity 的容器 View 中。这意味着之前的Fragment只是隐藏,当您想显示之前的fragment时,您无需担心如何恢复数据。

这是我的代码:

public void ReplaceFragment(Context context, Fragment newFragment, string TAG)
{
Android.Support.V4.App.FragmentManager fragmentManager = ((FragmentActivity)context).SupportFragmentManager;
Android.Support.V4.App.FragmentTransaction ft = fragmentManager.BeginTransaction();

//When you want to show your AttendanceFragment
if (newFragment == null)
{
newFragment = new AttendanceFragment();
}
if (newFragment.IsAdded)
{
ft.Show(mg);
}
else
{
newFragment.Add(Resource.Id.HomeFrameLayout, newFragment);
}
if (anotherFragmentInstance != null && anotherFragmentInstance.IsAdded)
{
ft.Hide(anotherFragmentInstance);
}
ft.AddToBackStack(TAG);
ft.Commit();
}

关于android - 如何在按下后退按钮时恢复整个 fragment View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45544628/

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