Skip to content

Instantly share code, notes, and snippets.

@AlejandroRuiz
Created April 28, 2015 03:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save AlejandroRuiz/c1338bb0166754c6da3d to your computer and use it in GitHub Desktop.
Save AlejandroRuiz/c1338bb0166754c6da3d to your computer and use it in GitHub Desktop.
ViewPager Xamarin Android Example
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minWidth="25px"
android:minHeight="25px">
<ImageView
android:src="@android:drawable/ic_menu_gallery"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageview_card"
android:scaleType="centerCrop" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Support.V4.View;
using System.Collections.Generic;
using Android.Media;
using Android.Content.Res;
namespace NewApp
{
[Activity (Label = "TestApp",ScreenOrientation = Android.Content.PM.ScreenOrientation.Portrait, Icon = "@drawable/ic_launcher")]
public class MainActivity : Activity, ViewPager.IOnPageChangeListener
{
private ViewPager mCardsViewPager;
public void OnPageScrollStateChanged (int state)
{
}
public void OnPageScrolled (int position, float positionOffset, int positionOffsetPixels)
{
}
public void OnPageSelected (int position)
{
}
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
ActionBar.SetDisplayUseLogoEnabled (true);
SetContentView(Resource.Layout.Main);
mCardsViewPager = FindViewById<ViewPager>(Resource.Id.pager);
mCardsViewPager.Adapter = new CardsPagerAdapter(this.FragmentManager);
mCardsViewPager.SetPageTransformer (true, new FadeTransformer());
}
}
public class CardsPagerAdapter:Android.Support.V13.App.FragmentStatePagerAdapter {
private int[] mCards = {
Resource.Drawable.img1,
Resource.Drawable.img2,
Resource.Drawable.img3,
Resource.Drawable.img4,
Resource.Drawable.img5,
Resource.Drawable.img6,
Resource.Drawable.img7,
Resource.Drawable.img8,
Resource.Drawable.img9,
Resource.Drawable.img10
,Resource.Drawable.img11
};
private List<Fragment> mCardsFragments { get; set; }
public CardsPagerAdapter(FragmentManager fm) : base(fm)
{
mCardsFragments = new List<Fragment>{
new PagerFragment(mCards[0]),
new PagerFragment(mCards[1]),
new PagerFragment(mCards[2]),
new PagerFragment(mCards[3]),
new PagerFragment(mCards[4]),
new PagerFragment(mCards[5]),
new PagerFragment(mCards[6]),
new PagerFragment(mCards[7]),
new PagerFragment(mCards[8]),
new PagerFragment(mCards[9]),
new PagerFragment(mCards[10])
};
}
#region implemented abstract members of PagerAdapter
public override int Count {
get {
return mCardsFragments.Count;
}
}
#endregion
#region implemented abstract members of FragmentStatePagerAdapter
public override Fragment GetItem (int position)
{
return mCardsFragments[position];
}
#endregion
}
public class FadeTransformer : Java.Lang.Object, ViewPager.IPageTransformer
{
private const float MaxAngle = 30F;
public void TransformPage(View view, float position)
{
if (position < -1 || position > 1)
{
view.Alpha = 0; // The view is offscreen.
}
else
{
view.Alpha = 1;
view.PivotY = view.Height / 2; // The Y Pivot is halfway down the view.
// The X pivots need to be on adjacent sides.
if (position < 0)
{
view.PivotX = view.Width;
}
else
{
view.PivotX = 0;
}
view.RotationY = MaxAngle * position; // Rotate the view.
}
}
}
}
using System;
using Android.App;
using Android.Views;
using Android.OS;
using Android.Widget;
namespace NewApp
{
public class PagerFragment:Fragment
{
int IDIMG{ get; set; }
public PagerFragment (int id)
{
IDIMG = id;
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.Inflate(Resource.Layout.DemoFragment, container, false);
((ImageView)view.FindViewById (Resource.Id.imageview_card)).SetImageResource (IDIMG);
return view;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment