[Xamarin] 製作可以滑動的Tab

2016-07-28

之前幾篇文章,是利用一個網路上的open source ( https://github.com/Cheesebaron/ViewPagerIndicator ) 來製作,但是後來發現原來Xamarin Component 裡面有一個  Material Pager Sliding Tab Strip 也有一個可以做出滑動的Slider Tab ,基本上就是中規中矩比較符合Material  Design 
material_tabs

1. 首先打開專案後 在 Components 下載  Material Pager Sliding Tab Strip
image
2. 引入後,在主layout 端加入 com.refractored.PagerSlidingTabStrip , 還有 android.support.v4.view.ViewPager
Code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:fitsSystemWindows="true">
    <com.refractored.PagerSlidingTabStrip
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:pstsPaddingMiddle="true"
        app:pstsDividerWidth="1dp"
        app:pstsDividerPadding="12dp"
        app:pstsDividerColor="#50FFFFFF"
        android:textColor="#50FFFFFF"
        app:pstsTextColorSelected="@android:color/white"
        app:pstsIndicatorColor="@android:color/white"
        app:pstsUnderlineColor="@android:color/white" />
<!--Change this to true if you want to center items-->
    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        tools:context=".MainActivity" />
</LinearLayout>

3.  main activity 的部分 我們要繼承  AppCompatActivity, IOnTabReselectedListener, ViewPager.IOnPageChangeListener ,並且初始化 tab and pager

// 初始化  ViewPager 和 adapter
var pager = FindViewById<ViewPager>(Resource.Id.pager);
pager.Adapter = new TestAdapter(SupportFragmentManager);
 
// 載入上放頁籤
var tabs = FindViewById<PagerSlidingTabStrip>(Resource.Id.tabs);
tabs.SetViewPager(pager);
 
//加入事件
tabs.OnTabReselectedListener = this;
tabs.OnPageChangeListener = this;

4. 其中 TestAdapter

public class TestAdapter : FragmentPagerAdapter
    {
        private readonly string[] Titles =
        {
            "Categories", "Home", "Top Paid", "Top Free", "Top Grossing", "Top New Paid",
            "Top New Free", "Trending"
        };
 
        public TestAdapter(FragmentManager fm) : base(fm)
        {
        }
 
        public override ICharSequence GetPageTitleFormatted(int position)
        {
            return new Java.Lang.String(Titles[position % Titles.Length]);
        }
 
        #region implemented abstract members of PagerAdapter
 
        public override int Count
        {
            get { return Titles.Length; }
        }
 
        #endregion
 
        #region implemented abstract members of FragmentPagerAdapter
 
        public override Fragment GetItem(int position)
        {
            var res = new TabContentFragment();
            res.Content = Titles[position % Titles.Length];
            return res;
        }
 
        #endregion
    }

執行起來的結果 :
Screenshot_20160728-173007

其中幾點必須注意 :
1. 我是改寫 https://github.com/jamesmontemagno/PagerSlidingTabStrip-for-Xamarin.Android 的範例,如果覺得我寫得不夠詳細,可以參考這裡

2.因為他其中範例用到lib 會比較多 包含 CardView 所以我把範例單純化,變成自訂的fragment 這樣比較貼切一般使用

Source: https://www.dropbox.com/s/nwzyl8ak8hzomuo/SliderTest2_fix_20140910.7z?dl=0


當麻許的超技八 2014 | Donma Hsu Design.