0

Here is the bit of code I have so far :

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

As you can see from the above, i have attempted to add code to the OnListItemClick event, however, ive not succeeded in changing the fragment.

So, I need the code to do the following :

  • Not to change pages when swiping left or right
  • but only change pages by clicking a list item

Am I going in the right direction ?

Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95

1 Answers1

0

I was able to resolve my issue by doing the following :

  • Changing my Fragment class to a ListFragment class
  • Setting my ViewPager as a property
  • Using the ViewPager property to setCurrentItem to the next ListFragment class

Code :

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


}

This took a few hours to resolve and may help others.