FragmentPagerAdapter只存在于Android.Support.V4.App(而不是Android.App)
我在Android.App中找不到FragmentPagerAdapter。
我不想使用Android.Support.V4.App中的Fragment,因为我的目标API是14以上(Android 4.0以上)。 所以,我只想使用Android.App.Fragments,并有关联的类。
我只发现它在Android.Support.V4.App,但是这不足以让我b / c我试图使用Android.App.Fragment的(不是Android.Support.V4.App.Fragment的)和那里Android.App(不是Android.Support.V4.App)中的相关类,如果我从FragmentPagerAdapter派生我的分页器(如果它来自支持库),我的代码就不会编译,因为Android.App和Android.Support之间的结果types不匹配.V4.App。
就像在这里的情况不能转换为android.app.Fragment ,是否有一个“普通”的pager(PagerAdapter)类,我应该使用的地方FragmentPagerAdapter或东西(就像你从正常的活动,而不是FragmentActivity派生,当瞄准API 11或更高)。
以下是我正在使用的示例代码(它来自https://github.com/xamarin/monodroid-samples/tree/master/Support4中的MonoDroid示例的Support4.sln解决scheme中的FragmentPagerSupport.cs文件)。
我已经注释了引用Android.Support.V4.App的行,并将其replace为引用Android.App的代码。 Android.Support.V4.App之外没有FramePagerAdapter,我可以find,我真的需要它)。
谢谢。
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
,它应该做你想做的事情。 这是一个不支持片段的FragmentPagerAdapter。
Android Studio安装
请添加遵循Gradle依赖关系
dependencies { compile 'com.android.support:support-v13:+' }
呃,你只需要使用V13支持库中的FragmentPagerAdapter
Android.Support.V13.App.FragmentPagerAdapter
然后,所有其他片段相关的类可以从“普通”库/名称空间使用,除了ViewPager,但这没什么大不了的。
下面是一个完整性示例(从https://github.com/xamarin/monodroid-samples/修改“Support4”示例):;
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.Util; using Android.Views; using Android.Widget; using Java.Lang; using Android.Support.V4.View; using Fragment = Android.App.Fragment; namespace Support4 { [Activity (Label = "@string/fragment_pager_support")] [IntentFilter (new[]{Intent.ActionMain}, Categories = new[]{ "mono.support4demo.sample" })] public class FragmentPagerSupport : Activity //public class FragmentPagerSupport : FragmentActivity { const int NUM_ITEMS = 4; protected MyAdapter _pagerAdapter; protected ViewPager _viewPager; protected override void OnCreate (Bundle bundle) { base.OnCreate (bundle); SetContentView(Resource.Layout.fragment_pager); List<Fragment> fragments = new List<Fragment>(); // *** MonoDroid 4.2.7 letter case bug *** make's first letter lower. //string typeName = typeof(Fragment1).FullName; string typeName = "support4." + typeof(Fragment1).Name; fragments.Add(Fragment.Instantiate(this, typeName)); fragments.Add(Fragment.Instantiate(this, typeName)); fragments.Add(Fragment.Instantiate(this, typeName)); fragments.Add(Fragment.Instantiate(this, typeName)); //adapter = new MyAdapter(SupportFragmentManager); _pagerAdapter = new MyAdapter(FragmentManager, fragments); _viewPager = FindViewById<ViewPager>(Resource.Id.view_pager); _viewPager.Adapter = _pagerAdapter; } public override bool OnTouchEvent(MotionEvent e) { return base.OnTouchEvent(e); } protected class MyAdapter : Android.Support.V13.App.FragmentPagerAdapter { private List<Fragment> _fragments; public override Java.Lang.Object InstantiateItem(View p0, int p1) { return base.InstantiateItem(p0, p1); } public MyAdapter(Android.App.FragmentManager fm) : base(fm) { } //public MyAdapter(Android.Support.V4.App.FragmentManager fm, List<Android.Support.V4.App.Fragment> fragments) // : base(fm) public MyAdapter(FragmentManager fm, List<Fragment> fragments) : base(fm) { _fragments = fragments; } public override int Count { get { return NUM_ITEMS; } } //public override Android.Support.V4.App.Fragment GetItem(int p0) public override Fragment GetItem(int p0) { return _fragments[p0]; } public override float GetPageWidth(int p0) { //return base.GetPageWidth(p0); //base.GetPageWidth(p0); return (float)(0.5f); } } } //public class Fragment1 : Android.Support.V4.App.Fragment public class Fragment1 : Fragment { int num; private static int _colorIndex = 0; private static Android.Graphics.Color[] _colors = new[] { Android.Graphics.Color.Aqua, Android.Graphics.Color.DarkViolet, Android.Graphics.Color.Coral, Android.Graphics.Color.Bisque}; public Fragment1() { } public Fragment1(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) { View v = inflater.Inflate(Resource.Layout.aaaaa, container, false); TextView tv = v.FindViewById<TextView>(Resource.Id.text); tv.Text = "# " + _colorIndex; tv.SetBackgroundColor(_colors[_colorIndex++]); return v; } public override void OnActivityCreated(Bundle p0) { base.OnActivityCreated(p0); } } }
<?xml version="1.0" encoding="utf-8"?> <!-- Copyright (C) 2010 The Android Open Source Project Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <!-- Top-level content view for the simple fragment sample. --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:padding="4dip" android:layout_width="match_parent" android:layout_height="match_parent"> <!--android:gravity="center_horizontal"--> <android.support.v4.view.ViewPager android:id="@+id/view_pager" android:layout_width="700dip" android:layout_height="match_parent" android:layout_weight="1" android:background="#FFCCFFFF"> <!--android:layout_width="match_parent"--> </android.support.v4.view.ViewPager> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/screen_container" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/text" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_vertical|center_horizontal" android:textAppearance="?android:attr/textAppearanceMedium" android:text="@string/hello_world" android:background="#FF335555"/> </LinearLayout>
将此依赖添加到gradle依赖关系中:
compile 'com.android.support:support-v13:+'
并像这样使用android.support.v13.app.FragmentPagerAdapter
(我只是修改了android studio中的官方演示项目:file→new→new project→next→next→tabbed activity→next→finish):
import android.app.Fragment; import android.app.FragmentManager; import android.support.v13.app.FragmentPagerAdapter; import com.google.android.gms.maps.MapFragment; /** A simple FragmentPagerAdapter that returns a MapFragment and a PreferenceFragment. */ public class MainActivityAdapter extends FragmentPagerAdapter { private MapFragment mapFragment; private PreferencesFragment preferencesFragment; public MainActivityAdapter(FragmentManager fm) { super(fm); mapFragment = MapFragment.newInstance(); preferencesFragment = new PreferencesFragment(); } @Override public int getCount() { return 2; } @Override public Fragment getItem(int position) { switch (position) { case 0: return mapFragment; case 1: return preferencesFragment; default: return null; } } }
- 什么时候SQLiteOpenHelper的onCreate()/ onUpgrade()运行?
- 当我加载ADT时,为什么我收到错误“Android SDK需要Android Developer Toolkit版本XX.XX或以上?
- 如何使DialogFragment宽度为Fill_Parent
- 为什么发生ArrayIndexOutOfBoundsException,以及如何避免在Android?
- 在Mac OS X上设置ANDROID_HOME环境variables
- React-Native,Android,Genymotion:ADB服务器没有确认
- API自动将apk上传到Google Play?
- android查看不附加到窗口pipe理器
- 在Android中使用Retrofit