Theodorus Agum Gumilang
Theodorus Agum Gumilang

Reputation: 1486

How to add page in sliding tab layout in Xamarin Android

I need to add some page in each tab from Sliding Tab Page Layout Xamarin Android, I Already make the sliding tab layout it works but i cant change the page for each tab here is my code Fragments Code :

public class SlidingTabsFragment : Fragment
    {
        private SlidingTabScrollView mSlidingTabScrollView;
        private ViewPager mViewPager;

        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            return inflater.Inflate(Resource.Layout.fragment_sample, container, false);
        }

        public override void OnViewCreated(View view, Bundle savedInstanceState)
        {
            mSlidingTabScrollView = view.FindViewById<SlidingTabScrollView>(Resource.Id.sliding_tabs);
            mViewPager = view.FindViewById<ViewPager>(Resource.Id.viewpager);
            mViewPager.Adapter = new SamplePagerAdapter();

            mSlidingTabScrollView.ViewPager = mViewPager;
        }

        public class SamplePagerAdapter : PagerAdapter
        {
            List<string> items = new List<string>();

            public SamplePagerAdapter() : base()
            {
                items.Add("Nasional");
                items.Add("Olahraga");
                items.Add("Internasional");
                items.Add("Hiburan");
                items.Add("Sains");
                items.Add("Ekonomi");
            }

            public override int Count
            {
                get { return items.Count; }
            }

            public override bool IsViewFromObject(View view, Java.Lang.Object obj)
            {
                return view == obj;
            }

            public override Java.Lang.Object InstantiateItem(ViewGroup container, int position)
            {
                View view = LayoutInflater.From(container.Context).Inflate(Resource.Layout.pager_item, container, false);
                container.AddView(view);

                TextView txtTitle = view.FindViewById<TextView>(Resource.Id.item_title);
                int pos = position + 1;
                txtTitle.Text = pos.ToString();

                return view;
            }

            public string GetHeaderTitle(int position)
            {
                return items[position];
            }

            public override void DestroyItem(ViewGroup container, int position, Java.Lang.Object obj)
            {
                container.RemoveView((View)obj);
            }
        }
    }

and what i want in here is to make each tab is redirect to another page that i make, and here is the example of my page

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:titleTextColor="#333639" />
    <android.support.v7.widget.RecyclerView
        android:padding="8dp"
        android:id="@+id/recyclerView"
        android:layout_below="@id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>

Upvotes: 0

Views: 1319

Answers (1)

Abhinash
Abhinash

Reputation: 368

Include the PagerSlidingTabStrip widget in your layout. This should usually be placed above the ViewPager it represents.

<com.refractored.PagerSlidingTabStrip android:id="@+id/tabs" 
android:layout_width="match_parent" android:layout_height="?
attr/actionBarSize" android:background="?attr/colorPrimary" />

In your OnCreate method (or OnCreateView for a fragment), bind the widget to the ViewPager.

// Initialize the ViewPager and set an adapter
var pager =  FindViewById<ViewPager>(Resource.Id.pager);
pager.Adapter = new TestAdapter(SupportFragmentManager);

// Bind the tabs to the ViewPager
var tabs = FindViewById<PagerSlidingTabStrip>(Resource.Id.tabs);
tabs.SetViewPager(pager);

If your adapter implements the interface CustomTabProvider you can past you custom tab view/s. In case the the view returned contains the id Resource.Id.psts_tab_title, this view should be a TextView and will be used to placed the title. If you don't want the library to manage your TextView title for the tab, use a different id than Resource.Id.psts_tab_title in your tab layout.

If your adapter doesn't impelement the interface CustomTabProvider the default tab will be used (That's a TextView with id Resource.Id.psts_tab_title).

(Optional) If you use implement IOnPageChangeListener with your view pager you should set it in the widget rather than on the pager directly.

// continued from above

tabs.SetOnPageChangeListener(pageChangeListener);

For Sample Code, Refer this link:

https://github.com/jamesmontemagno/PagerSlidingTabStrip-for-Xamarin.Android

Sample http://cforbeginners.com/XamarinProjects.html

Youtube Video https://www.youtube.com/watch?v=0Rr0ZCvX_Zs

Upvotes: 1

Related Questions