FragmentPagerAdapter Exists Only In Android.Support.V4.App (and not Android.App)

samis picture samis · Nov 21, 2012 · Viewed 48.3k times · Source

I cannot find find FragmentPagerAdapter within Android.App.

I do not want to use the Fragment's from Android.Support.V4.App, as my target API is 14 and higher (Android 4.0 and up). So, I want to just use plain of Android.App.Fragments, and there associated classes.

I only have found it within Android.Support.V4.App, but this doesn't suffice for me b/c I'm trying to use Android.App.Fragment's (not Android.Support.V4.App.Fragment's) and there related classes within Android.App (not Android.Support.V4.App), and my code wont compile if I derive my pager from FragmentPagerAdapter if its from the Support library, because of the resulting type mismatch's between Android.App and Android.Support.V4.App.

Just as with the case here Cannot be cast to android.app.Fragment, is there a "normal" pager (PagerAdapter) class I should be using in place of FragmentPagerAdapter or something (just like you derive from normal Activity, and not FragmentActivity, when targeting API 11 or higher).

Here is the sample code I'm working with (its the FragmentPagerSupport.cs file within the Support4.sln solution from the MonoDroid examples found at https://github.com/xamarin/monodroid-samples/tree/master/Support4).

I've commented out the lines that referenced Android.Support.V4.App and replaced them with code that references Android.App. There is no FramePagerAdapter outside of Android.Support.V4.App that I could find, and I really need it).

Thanks.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
//using Android.Support.V4.App;
//using Android.Support.V4.View;

namespace Support4
{
    [Activity (Label = "@string/fragment_pager_support")]
    [IntentFilter (new[]{Intent.ActionMain}, Categories = new[]{ "mono.support4demo.sample" })]
    //public class FragmentPagerSupport : FragmentActivity
        public class FragmentPagerSupport : Activity
    {
        const int NUM_ITEMS = 10;
        MyAdapter adapter;
        ViewPager pager;

        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);

            SetContentView(Resource.Layout.fragment_pager);

            //adapter = new MyAdapter(SupportFragmentManager);
                        adapter = new MyAdapter(FragmentManager);

            pager = FindViewById<ViewPager>(Resource.Id.pager);
            pager.Adapter = adapter;

            var button = FindViewById<Button>(Resource.Id.goto_first);
            button.Click += (sender, e) => {
                pager.CurrentItem = 0;  
            };
            button = FindViewById<Button>(Resource.Id.goto_last);
            button.Click += (sender, e) => {
                pager.CurrentItem = NUM_ITEMS - 1;
            };
        }

                // ?????????????????????????????????????????????????
                // - where is FragmentPagerAdapter 
                // ?????????????????????????????????????????????????

        protected class MyAdapter : FragmentPagerAdapter 
        {
            public MyAdapter(FragmentManager fm) : base(fm)
            {
            }

            public override int Count {
                get {
                    return NUM_ITEMS;
                }
            }

            public override Fragment GetItem (int position)
            {
                return new ArrayListFragment(position);
            }


        }

        protected class ArrayListFragment : ListFragment
        {
            int num;

            public ArrayListFragment()
            {
            }

            public ArrayListFragment(int num)
            {
                var args = new Bundle();
                args.PutInt("num", num);
                Arguments = args;
            }

            public override void OnCreate (Bundle p0)
            {
                base.OnCreate (p0);

                num = Arguments != null ? Arguments.GetInt("num") : 1;
            }

            public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
            {
                var v = inflater.Inflate(Resource.Layout.fragment_pager_list, container, false);
                var tv = v.FindViewById<TextView>(Resource.Id.text);
                tv.Text = "Fragment #" + num;
                return v;
            }

            public override void OnActivityCreated (Bundle p0)
            {
                base.OnActivityCreated (p0);

                ListAdapter = new ArrayAdapter<string>(Activity, Android.Resource.Layout.SimpleListItem1, Cheeses.cheeseStrings);
            }

            public override void OnListItemClick(ListView l, View v, int position, long id) {
                Console.WriteLine ( "Item clicked: " + id);
            }
        }
    }
}

Answer

PearsonArtPhoto picture PearsonArtPhoto · Nov 21, 2012

There is one that is in android.support.v13.app.FragmentPagerAdapter, which should do what you want it to do. It's a FragmentPagerAdapter for non-support fragments.

Android Studio Installation

Please add follow Gradle dependencies

dependencies {
    compile 'com.android.support:support-v13:+'
}