Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Ugh, you just need to use the FragmentPagerAdapter from the V13 support library</p> <pre><code>Android.Support.V13.App.FragmentPagerAdapter </code></pre> <p>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.</p> <hr> <p>Here's a sample for completeness (modified "Support4" example from <a href="https://github.com/xamarin/monodroid-samples/">https://github.com/xamarin/monodroid-samples/</a>):</p> <pre><code>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&lt;Fragment&gt; fragments = new List&lt;Fragment&gt;(); // *** 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&lt;ViewPager&gt;(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&lt;Fragment&gt; _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&lt;Android.Support.V4.App.Fragment&gt; fragments) // : base(fm) public MyAdapter(FragmentManager fm, List&lt;Fragment&gt; 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&lt;TextView&gt;(Resource.Id.text); tv.Text = "# " + _colorIndex; tv.SetBackgroundColor(_colors[_colorIndex++]); return v; } public override void OnActivityCreated(Bundle p0) { base.OnActivityCreated(p0); } } } </code></pre> <hr> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;!-- 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. --&gt; &lt;!-- Top-level content view for the simple fragment sample. --&gt; &lt;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"&gt; &lt;!--android:gravity="center_horizontal"--&gt; &lt;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"&gt; &lt;!--android:layout_width="match_parent"--&gt; &lt;/android.support.v4.view.ViewPager&gt; &lt;/LinearLayout&gt; </code></pre> <hr> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;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"&gt; &lt;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"/&gt; &lt;/LinearLayout&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload