【问题标题】:itemSelected not fired in Xamarin.AndroiditemSelected 未在 Xamarin.Android 中触发
【发布时间】:2016-03-03 19:33:09
【问题描述】:

我正在尝试为我正在开发的社交应用程序开发注册菜单。我希望注册菜单包含一个包含五个片段的 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 在向右或向左滑动时重置。

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

【问题讨论】:

标签: android listview android-fragments xamarin.android


【解决方案1】:

将信息放在 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;
   }

【讨论】:

    猜你喜欢
    • 2018-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-11
    • 1970-01-01
    相关资源
    最近更新 更多