Xamarin \ Android开发 - 如何通过单击列表视图项来更改碎片?

时间:2014-08-01 15:08:48

标签: c# android android-fragments xamarin fragmentpageradapter

以下是我到目前为止的一些代码:

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事件中,但是,我没有成功更改片段。

所以,我需要代码来执行以下操作:

  • 向左或向右滑动时不要更改页面
  • 但仅通过单击列表项来更改页面

我是朝着正确的方向前进的吗?

1 个答案:

答案 0 :(得分:0)

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

  • 将我的Fragment类更改为ListFragment类
  • 将我的ViewPager设置为属性
  • 使用ViewPager属性将setCurrentItem设置为下一个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);
    }


}

这需要几个小时才能解决,可能会帮助其他人。