Android fragments 单击listview中的项目后导航到新片段

Android fragments 单击listview中的项目后导航到新片段,android-fragments,xamarin.android,listview-adapter,Android Fragments,Xamarin.android,Listview Adapter,所以我创建了一个列表视图,每行有5个文本视图。最后一个文本视图应该在单击时为我获取有关某些项目的信息。我的listview的适配器有一个与片段对应的上下文 List<itemproperties> items; itemdisplay context; public homeadapter(itemdisplay context, List<itemproperties> items) : base()

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

 List<itemproperties> items;
        itemdisplay context;
        public homeadapter(itemdisplay context, List<itemproperties> items)
            : base()
        {
            this.context = context;
            this.items = items;
        }
列表项;
项目显示上下文;
公共homeadapter(项目显示上下文,列表项目)
:base()
{
this.context=上下文;
这个项目=项目;
}
其中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();
                };
            } 
TextView TextView=view.FindViewById(Resource.Id.textView5);
Fragment1片段=新的Fragment1();
如果(!textView.HasOnClickListeners)
{
文本视图。单击+=(o,e)=>
{
SupportFragmentManager.BeginTransaction()
.Replace(Resource.Id.mainFrame,fragment)
.Commit();
};
} 
以上所有代码都在我的适配器中。但我得到了一个错误,因为我想SupportFragmentManager似乎不存在于适配器中。所以我的问题是,我该怎么做?
提前非常感谢。

您是否希望获得如下效果

如果是这样,您需要在构造函数中使用
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;
        }

      
    }

   
}

是否希望获得如下效果

如果是这样,您需要在构造函数中使用
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显示在一个片段中。因此,我的适配器的上下文是一个片段“itemdisplay”。我是否应该更改整个上下文并在活动而不是片段中显示listview。请注意,我在单击抽屉中的一个项目后访问了我的列表视图。哦,很抱歉,我发现即使是片段上下文也有FragmantManager。你的回答很有用,先生,谢谢。这是我的演示。你可以参考一下。非常感谢,先生。但问题是我在一个导航抽屉模板中工作。因此,我的listview显示在一个片段中。因此,我的适配器的上下文是一个片段“itemdisplay”。我是否应该更改整个上下文并在活动而不是片段中显示listview。请注意,我在单击抽屉中的一个项目后访问了我的列表视图。哦,很抱歉,我发现即使是片段上下文也有FragmantManager。先生,你的回答很有用,非常感谢
<?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>