我正在尝试使用Xamarin.android做一个应用程序。它是“ ToDoList”,我有两个选项卡,其中之一是创建和编辑琐事(称为“撤消”)。另一个是标记为完成的杂务(称为完成)。我已使用“片段”作为选项卡,而我要完成的工作是当我将一项任务标记为完成时,要从“撤消”选项卡的列表中将其删除,然后在“完成”选项卡的列表中添加一项。
MainActivity.cs
public class MainActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
this.ActionBar.NavigationMode = ActionBarNavigationMode.Tabs;
var tab1 = ActionBar.NewTab();
tab1.SetText("Undone");
var tabFirst = new UndoneListFragment();
tab1.TabSelected += (sender, e) =>
{
var fragment = this.FragmentManager.FindFragmentById(Resource.Id.tabsContainer);
if (fragment != null)
e.FragmentTransaction.Remove(fragment);
e.FragmentTransaction.Add(Resource.Id.tabsContainer, tabFirst);
};
tab1.TabUnselected += (sender, e) =>
{
e.FragmentTransaction.Remove(tabFirst);
};
var tab2 = ActionBar.NewTab();
tab2.SetText("Done");
var tabSecond = new DoneListFragment();
tab2.TabSelected += (sender, e) =>
{
var fragment = this.FragmentManager.FindFragmentById(Resource.Id.tabsContainer);
if (fragment != null)
e.FragmentTransaction.Remove(fragment);
e.FragmentTransaction.Add(Resource.Id.tabsContainer, tabSecond);
};
tab2.TabUnselected += (sender, e) =>
{
e.FragmentTransaction.Remove(tabSecond);
};
ActionBar.AddTab(tab1);
ActionBar.AddTab(tab2);
}
}
UndoneListFragment.cs
public class UndoneListFragment : Fragment
{
List<Task> tasks = new List<Task>();
ListView lView;
TasksViewAdapter adapter;
View view;
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Use this to return your custom view for this Fragment
// return inflater.Inflate(Resource.Layout.YourFragment, container, false);
base.OnCreateView(inflater, container, savedInstanceState);
view = inflater.Inflate(Resource.Layout.undoneListView, container, false);
Button addBtn = view.FindViewById<Button>(Resource.Id.AddBtn);
lView = view.FindViewById<ListView>(Resource.Id.TasksViewList);
adapter = new TasksViewAdapter(view.Context, tasks);
lView.Adapter = adapter;
lView.ItemClick += Edit;
addBtn.Click += AddTask;
return view;
}
}
public override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
switch (requestCode)
{
case 10:
if (resultCode == Result.Ok) //Edit an already existing task
{
var replacedTaskName = data.GetStringExtra("new_name" ?? "Name not found");
var position = data.GetIntExtra("listPos", 0);
bool done = data.GetBooleanExtra("done", false);
Task t = new Task(replacedTaskName);
if (done == true)
{
t.TaskStatus = Status.Done;
//here I want to implement the code to somehow
//send the replacesTaskName to the second fragment
//Your suggestion is here
DoneListFragment frag = new DoneListFragment();
Bundle b = new Bundle();
b.PutString("MyKey",replacedTaskName);
frag.Arguments = b;
}
tasks[position] = t;
adapter.NotifyDataSetChanged();
string newName = tasks[position].TaskName;
break;
}
}
}
DoneListFragment.cs
public class DoneListFragment : Fragment
{
List<Task> doneTasks = new List<Task>();
ListView lView;
TasksViewAdapter adapter;
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
base.OnCreateView(inflater, container, savedInstanceState);
var view = inflater.Inflate(Resource.Layout.doneListView, container, false);
Button deleteBtn = view.FindViewById<Button>(Resource.Id.DeleteAllBtn);
lView = view.FindViewById<ListView>(Resource.Id.doneList);
adapter = new TasksViewAdapter(view.Context, doneTasks);
lView.Adapter = adapter;
if(Arguments != null)
{
string value= Arguments.GetString("MyKey");
}
//click to delete done tasks
deleteBtn.Click += DeleteAll;
return view;
}
}
答案 0 :(得分:1)
要从一个片段发送数据,另一个简单的步骤是在替换时创建构造函数 片段或调用片段。
例如:
假设我有两个片段,
ActionFragment
ActionDetailsFragment
现在我想将数据从“ ActionFragment”发送到“ ActionDetailsFragment”:
在ActionFragment中:
fragmentManager = getChildFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frameLayoutActionDetails, new ActionDetailsFragment(Data x, Data y)).commitAllowingStateLoss( );
现在下面的代码将在“ ActionDetailsFragment”中
public class ActionDetailsFragment extends AppFragment {
Data x;
Data y;
public ActionDetailsFragment(Data x, Data y) {
super();
this.x = x;
this.y = y;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
—————————————
////———Here is your code———————
—————————————————
}
现在您可以在ActionDetailsFragment中使用“ Data x,Data y”… 就这样...
注意:数据x和数据y都是虚变量。
活动不能在TabLayout或ViewPager中使用,必须使用Fragments。 您可以改用“捆绑包”
Fragment fragment = new Fragment();
Bundle bundle = new Bundle();
bundle.putInt(key, value);
fragment.setArguments(bundle);
然后在您的Fragment中,使用以下方法检索数据(例如,在onCreate()方法中):
Bundle bundle = this.getArguments();
if (bundle != null) {
int myInt = bundle.getInt(key, defaultValue);
}
答案 1 :(得分:0)
好吧,如果您正在寻找“片段到片段”数据传输的明智之举,那就是使用片段参数,就像这样:
根据情况初始化片段对象时:
var frag= new DoneListFragment();
Bundle bundle= new Bundle();
bundle.PutString("YourKey", "YourValue");
frag.Arguments=bundle;
然后要获取此数据,您可以在OnCreateView方法的DoneListFragment
中执行以下操作:
if(Arguments!=null)
{
String value = Arguments.GetString("YourKey");
}