XamarinAndroid Development -如何通过单击列表视图项来更改Fragments

本文关键字:Fragments 视图 列表 Development 何通过 单击 XamarinAndroid | 更新日期: 2023-09-27 18:15:55

这是我到目前为止的代码:

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);
        }
    }
}

正如你从上面看到的,我试图添加代码到onlisttitemclick事件,但是,我没有成功地改变片段。

所以,我需要代码做以下事情:

  • 向左或向右滑动时不改变页面
  • 但只能通过点击列表项来更改页面

我走的方向对吗?

XamarinAndroid Development -如何通过单击列表视图项来更改Fragments

我能够解决我的问题通过做以下:

  • 将Fragment类更改为ListFragment类
  • 将我的ViewPager设置为属性
  • 使用ViewPager属性将currentitem设置为下一个ListFragment类
代码:

public class MyFragment : Android.Support.V4.App.ListFragment 
{
    string[] animals = new string[5] { "Tiger", "Lion", "Zebra", "Cheetah", "Giraffe" };
    CustomViewPager vpager;
    Context thiscontext;
    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        thiscontext = container.Context;
        var view = inflater.Inflate(Resource.Layout.Main, container, true);
        var vp = view.FindViewById<CustomViewPager>(Resource.Id.ViewPager);
        vpager = vp;
        return base.OnCreateView(inflater, container, savedInstanceState);
    }
    public override void OnActivityCreated(Bundle savedInstanceState)
    {
        base.OnActivityCreated(savedInstanceState);
        this.ListAdapter = new ArrayAdapter<string>(Activity, Android.Resource.Layout.SimpleExpandableListItem1, animals);
    }
    public override void OnListItemClick(ListView l, View v, int position, long id)
    {
        // We can display everything in place with fragments.
        // Have the list highlight this item and show the data.
        ListView.SetItemChecked(position, true);
       // vpager.SetOnPageChangeListener(new PagerListener());
        vpager.SetCurrentItem(1, true);
    }

}

这花了几个小时才解决,可能会对其他人有所帮助。