单击列表视图中的项目后导航到新片段

时间:2020-07-23 07:22:14

标签: android-fragments xamarin.android listview-adapter

所以我创建了一个列表视图,每行有5个文本视图。最后的textview应该会在单击时为我提供有关某些项目的信息。我的列表视图的适配器具有与片段相对应的上下文。

 List<itemproperties> items;
        itemdisplay context;
        public homeadapter(itemdisplay context, List<itemproperties> items)
            : base()
        {
            this.context = context;
            this.items = items;
        }

其中itemdisplay是片段。这是我在单击textview时尝试导航到新片段的代码

 TextView textView = view.FindViewById<TextView>(Resource.Id.textView5);
            
            Fragment1 fragment = new Fragment1();
            if (!textView.HasOnClickListeners)
            {
                textView.Click += (o, e) =>
                {
                    SupportFragmentManager.BeginTransaction()
                            .Replace(Resource.Id.mainFrame, fragment)
                            .Commit();
                };
            } 

以上所有代码都在我的适配器中。但我收到一个错误,因为我猜适配器中似乎不存在SupportFragmentManager。所以我的问题是,我应该怎么做? 提前非常感谢。

1 个答案:

答案 0 :(得分:0)

您是否想获得遵循GIF的结果?

enter image description here

如果是这样,则Adatper在构造函数中需要一个mainActivity属性,然后在GetView方法中使用以下方法。

   class MyAdapter : BaseAdapter<string>
    {
        private MainActivity mainActivity;
        private string[] items;

        public MyAdapter(MainActivity mainActivity, string[] items)
        {
            this.mainActivity = mainActivity;
            this.items = items;
        }

       
        public override string this[int position] => items[position];

        public override int Count => items.Length;

        public override long GetItemId(int position)
        {
            return position;
        }

        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            View view = convertView;
            if (view == null) // no view to re-use, create new
                view = mainActivity.LayoutInflater.Inflate(Resource.Layout.layout1, null);
            view.FindViewById<TextView>(Resource.Id.My_textView1).Text = items[position];
            TextView textView = view.FindViewById<TextView>(Resource.Id.textView1);
            textView.Text = items[position];
           

            if (!textView.HasOnClickListeners)
            {
                textView.Click += (o, e) =>
                {
                    Fragment1 fragment = new Fragment1();

                    FragmentTransaction transaction = mainActivity.FragmentManager.BeginTransaction();
                    transaction.Replace(Resource.Id.FramePage, fragment);
                    transaction.AddToBackStack("main");
                    transaction.CommitAllowingStateLoss();
                    //  Toast.MakeText(mainActivity, "click", ToastLength.Short).Show();
                };
            }


          
            return view;
        }

      
    }

   
}

如果您转移到片段布局,则发现先前的listview stil存在。请像下面的代码一样,在 android:background="?android:windowBackground"和Fragment布局中添加activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
                android:background="?android:windowBackground"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


  <FrameLayout
     android:id="@+id/FramePage"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
    
       >
    <ListView
    android:id="@+id/listview"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:choiceMode="singleChoice"/>

  </FrameLayout>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
     android:background="?android:windowBackground"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
     <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Hello World!" />
</LinearLayout>
相关问题