0
Вот немного кода, который я до сих пор:Xamarin Android Development - Как изменить фрагменты, нажав на элемент listview?
namespace MyTestApp2
{
[Activity(Label = "Simple ListView", MainLauncher = true, Icon = "@drawable/icon")]
public class Activity1 : FragmentActivity {
private CustomViewPager _viewPager;
string[] items;
protected override void OnCreate(Bundle bundle) {
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
//items = new string[] { "Jon Douglas", "Jax The Cat", "Moxxi The Cat", "Andie The Dog" };
//ListAdapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleListItem1, items);
_viewPager = FindViewById<CustomViewPager>(Resource.Id.ViewPager);
_viewPager.Adapter = new MyFragmentAdapter(SupportFragmentManager);
}
//protected override void OnListItemClick(ListView l, View v, int position, long id)
//{
// var t = items[position];
// Toast.MakeText(this, t, Android.Widget.ToastLength.Short).Show();
//}
}
public class MyFragmentAdapter : FragmentPagerAdapter
{
public MyFragmentAdapter(Android.Support.V4.App.FragmentManager fm) : base(fm)
{}
public override int Count
{
get {
return 5; }
}
public override Android.Support.V4.App.Fragment GetItem(int position)
{
//List<Android.Support.V4.App.Fragment> test = new List<Android.Support.V4.App.Fragment>();
//test.Add(new MyFragment());
//test.Add(new MyFragment2());
if (position == 0)
return new MyFragment();
else
return new MyFragment2();
//return test[position] == null ? new MyFragment() : test[position];
}
public override void SetPrimaryItem(View container, int position, Java.Lang.Object @object)
{
base.SetPrimaryItem(container, position, @object);
}
}
public class MyFragment : Android.Support.V4.App.Fragment
{
string[] animals = new string[5] { "Tiger", "Lion", "Zebra", "Cheetah", "Giraffe" };
Context thiscontext;
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var view = inflater.Inflate(Resource.Layout.MyLayout, container, false);
var test = view.FindViewById<ListView>(Resource.Id.listView1);
thiscontext = container.Context;
test.Adapter = new ArrayAdapter(Activity, Android.Resource.Layout.SimpleListItem1, animals);
test.ItemClick += OnListItemClick;
return view;
// return base.OnCreateView(inflater, container, savedInstanceState);
}
void OnListItemClick(object sender, AdapterView.ItemClickEventArgs e)
{
//var listView = sender as ListView;
//var t = animals[e.Position];
////Android.Widget.Toast.MakeText(MyFragment.this, "kjh", Android.Widget.ToastLength.Short).Show();
//Android.Widget.Toast.MakeText(thiscontext, t, ToastLength.Short).Show();
//var adapter = new MyFragmentAdapter(this.FragmentManager);
//adapter.SetPrimaryItem(this.View, 1, this);
var test = new CustomViewPager(thiscontext, null);
test.SetCurrentItem(1, true);
}
}
public class MyFragment2 : Android.Support.V4.App.Fragment
{
private string[] animals = new string[5] { "Mackeral", "Sea Lion", "Dolphins", "Sardine", "Whales" };
Context thiscontext;
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var view = inflater.Inflate(Resource.Layout.MyLayout, container, false);
var test = view.FindViewById<ListView>(Resource.Id.listView1);
thiscontext = container.Context;
test.Adapter = new ArrayAdapter(Activity, Android.Resource.Layout.SimpleListItem1, animals);
test.ItemClick += OnListItemClick;
return view;
// return base.OnCreateView(inflater, container, savedInstanceState);
}
void OnListItemClick(object sender, AdapterView.ItemClickEventArgs e)
{
var listView = sender as ListView;
var t = animals[e.Position];
Android.Widget.Toast.MakeText(thiscontext, t, Android.Widget.ToastLength.Short).Show();
}
}
public class CustomViewPager : ViewPager
{
private bool enabled;
public CustomViewPager(Context context, Android.Util.IAttributeSet attr) : base(context, attr) { this.enabled = true; }
public override bool OnTouchEvent(MotionEvent e)
{
if(this.enabled)
return base.OnTouchEvent(e);
return false;
}
public override bool OnInterceptHoverEvent(MotionEvent e)
{
if(this.enabled)
return base.OnInterceptHoverEvent(e);
return false;
}
public override void SetCurrentItem(int item, bool smoothScroll)
{
base.SetCurrentItem(item, smoothScroll);
}
}
}
Как видно из вышесказанного, я попытался добавить код в событие OnListItemClick, однако, ив не удалось изменить фрагмент.
Итак, мне нужен код, чтобы сделать следующее:
- Не менять страницы, когда листать влево или вправо
- , но только изменить страницы, щелкнув пункт списка
Am I идти в правильном направлении?