我正在尝试为社交应用开发一个注册菜单,我正在努力。我希望注册菜单包含一个PageViewer,它包含五个片段。最后三个片段包含一个ListView,用户可以在其中“检查”有关它们的信息。 XML在这里:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minWidth="25px"
android:minHeight="25px"
android:id="@+id/layoutSignupLists">
<TextView
android:text="Add something here"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:id="@+id/signupListDescription" />
<ListView
android:id="@+id/interestListView"
android:layout_below="@id/signupListDescription"
android:layout_height="match_parent"
android:layout_width="match_parent" />
</RelativeLayout>
当正确显示最后三个片段时,此布局会膨胀。我已经为ListView中的itemSelected事件订阅了一个委托,如下所示:
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
//Inflate view and find content
View view = inflater.Inflate(Resource.Layout.signupFragLayout4, container, false);
interestListView = view.FindViewById <ListView>(Resource.Id.interestListView);
var desc = view.FindViewById<TextView>(Resource.Id.signupListDescription);
//Adds the description
desc.Text = GetString(Resource.String.profile_menu_edit_interests);
//Populate ListView
interestListView.Adapter = new ArrayAdapter<string>(Activity,
Resource.Layout.CheckedListViewItem, MainActivity.InfoNames[(int)InfoType.Interest]);
interestListView.ChoiceMode = ChoiceMode.Multiple;
interestListView.ItemSelected += (object sender, AdapterView.ItemSelectedEventArgs e) =>
{
if(!Interests.Contains(e.Position))
Interests.Add(e.Position);
else
Interests.Remove(e.Position);
};
return view;
}
在委托中放置断点时,我发现它从未被调用过,因此在向右或向左滑动时ListView会重置。
如何让片段“保持”信息,以便每次显示片段时都显示它?
答案 0 :(得分:0)
将信息放在MainActivity中。您可以通过将此变量放入片段类中来对其进行简单引用:
MainActivity mainActivity;
然后在您的OnCreateView()
中将其初始化:
mainActivity = (MainActivity)Activity;
现在,如果mainActivity中有任何公共变量,则可以在片段中引用它们,例如:
textView.Text = mainActivity.VariableName;
另外,为了将信息放回片段中,请像下面这样覆盖“ OnResume”方法:
public override void OnResume()
{
base.OnResume();
//Code to fill in all the information in your fragment again. I.E:
textView.Text = mainActivity.VariableName;
}