我在Android.App中找不到FragmentPagerAdapter。
我不想使用Android.Support.V4.App的Fragment,因为我的目标API是14和更高版本(Android 4.0和更高版本)。因此,我只想使用简单的Android.App.Fragments以及相关的类。
我只在Android.Support.V4.App中找到了它,但这对我来说还是不够的。我想使用Android.App.Fragment的(不是Android.Support.V4.App.Fragment的) Android.App(不是Android.Support.V4.App)中的相关类,并且如果我从FragmentPagerAdapter派生我的寻呼机(如果它是从Support库派生的),我的代码将无法编译,因为在Android.App和Android.Support之间会导致类型不匹配.V4.App。
就像这里无法转换为android.app.Fragment的情况一样,是否应该使用“普通”传呼器(PagerAdapter)类代替FragmentPagerAdapter之类的东西(就像您是从常规Activity而非FragmentActivity派生的一样, (定位到API 11或更高版本时)。
这是我正在使用的示例代码(来自https://github.com/xamarin/monodroid-samples/tree/master/Support4上的MonoDroid示例的Support4.sln解决方案中的FragmentPagerSupport.cs文件)。
我已注释掉引用Android.Support.V4.App的行,并用引用Android.App的代码替换了它们。我找不到Android.Support.V4.App之外的FramePagerAdapter,但我确实需要它)。
谢谢。
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);
}
}
}
}
android.support.v13.app.FragmentPagerAdapter
。不确定是否会对您有帮助,但是...