itemSelected 未在 Xamarin.Android 中触发

Tob*_*ell 6 android listview xamarin.android android-fragments

我正在尝试为我正在开发的社交应用程序开发一个注册菜单。我希望注册菜单包含一个包含五个片段的 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>
Run Code Online (Sandbox Code Playgroud)

当最后三个片段创建正确显示时,此布局会膨胀。我已经订阅了 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;
    }
Run Code Online (Sandbox Code Playgroud)

在委托中放置断点时,我发现它从未被调用,因此在向右或向左滑动时会重置 ListView。

如何让片段“保留”信息,以便每次显示片段时都显示它?

小智 0

将信息放置在 MainActivity 中。您可以通过将此变量放入片段类中来对其进行简单引用:

MainActivity mainActivity;
Run Code Online (Sandbox Code Playgroud)

并在你的OnCreateView()地方初始化它:

mainActivity = (MainActivity)Activity;
Run Code Online (Sandbox Code Playgroud)

现在,如果 mainActivity 中有任何公共变量,您可以在片段中引用它们,如下所示:

textView.Text = mainActivity.VariableName;
Run Code Online (Sandbox Code Playgroud)

此外,为了将信息放回片段中,请重写“OnResume”方法,如下所示:

   public override void OnResume()
   {
       base.OnResume();
       //Code to fill in all the information in your fragment again. I.E:
       textView.Text = mainActivity.VariableName;
   }
Run Code Online (Sandbox Code Playgroud)