so I created a listview that has 5 textviews in each row. the last textview is supposed to get me information about certain items upon being clicked. my listview's adapter has a context that corresponds to a fragment.
List<itemproperties> items;
itemdisplay context;
public homeadapter(itemdisplay context, List<itemproperties> items)
: base()
{
this.context = context;
this.items = items;
}
where itemdisplay is a fragment. and this is the code where I try to navigate to a new fragment when clicking on the 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();
};
}
all of the above codes are in my adapter. but I get an error because SupportFragmentManager doesn't seem to exist in adapters I guess. so my question is, how am I supposed to do this? thanks a lot in advance.
Do you want to achieve the result like following GIF?
If so, you Adatper need a mainActivity
attribute in constructor, then use following way in GetView
method.
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;
}
}
}
If you transfer to fragment layout, You found the previous listview stil existed. Please add android:background="?android:windowBackground"
in your activity_main.xml
and Fragment layout like following code.
<?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>