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);
}
}
}
}
Add this dependecy to the gradle dependencies:
And use
android.support.v13.app.FragmentPagerAdapter
like this (I simply modified the official demo project in android studio: file → new → new project → next → next → tabbed activity → next → finish):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
Had the same issue. My solution was to copy the code from android.support.v4.app.FragmentPagerAdapter, then change the imported Fragment class to android.app.Fragment. Afterwards do other minor adaptions to remove all errors. To my surprise it works perfectly. IMO this is simpler than adding a support library that you do not really need.
Ugh, you just need to use the FragmentPagerAdapter from the V13 support library
Then all other Fragment related classes can be used from the "normal" libraries/namespaces, with the exception of ViewPager, but that's no big deal.
Here's a sample for completeness (modified "Support4" example from https://github.com/xamarin/monodroid-samples/):