碎片选项卡打开默认第3个选项卡

时间:2014-12-29 17:17:38

标签: android

我有创建片段选项卡,当我们调用此选项卡时可以使用6个选项卡,然后默认打开第一个选项卡,但我们想要在我们调用此页面时实现,然后默认情况下打开第3个选项卡如何解析它请建议。< / p>

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        tabHost = FindViewById<TabHost>(Android.Resource.Id.TabHost);
        tabHost.Setup();

        viewPager = FindViewById<ViewPager>(Resource.Id.pager);
        tabsAdapter = new TabsAdapter(this, tabHost, viewPager);

        tabsAdapter.AddTab(tabHost.NewTabSpec("Sale").SetIndicator("Sale"), Java.Lang.Class.FromType(typeof(retailerStockSaleList.PageFragment)), null);
        tabsAdapter.AddTab(tabHost.NewTabSpec("L Y S M Sale").SetIndicator("L Y S M Sale"), Java.Lang.Class.FromType(typeof(retailerStockSale1List.PageFragment)), null);
        tabsAdapter.AddTab(tabHost.NewTabSpec("L M Sale").SetIndicator("L M Sale"), Java.Lang.Class.FromType(typeof(retailerStockSaleLMList.PageFragment)), null);
        tabsAdapter.AddTab(tabHost.NewTabSpec("Stock").SetIndicator("Stock"), Java.Lang.Class.FromType(typeof(retailerPhysicalStockList.PageFragment)), null);
        tabsAdapter.AddTab(tabHost.NewTabSpec("Order List").SetIndicator("Order List"), Java.Lang.Class.FromType(typeof(retailerOrderList.PageFragment)), null);
        tabsAdapter.AddTab(tabHost.NewTabSpec("Retailer Details").SetIndicator("Retailer Details"), Java.Lang.Class.FromType(typeof(retailerDetail.PageFragment)), null);

        if (bundle != null)
        {
            tabHost.SetCurrentTabByTag(bundle.GetString("tab"));
        }
    }


    protected override void OnSaveInstanceState(Bundle outState)
    {
        base.OnSaveInstanceState(outState);

        outState.PutString("tab", tabHost.CurrentTabTag);

    }

    protected class TabsAdapter : FragmentPagerAdapter, TabHost.IOnTabChangeListener, ViewPager.IOnPageChangeListener
    {
        private Context _context;
        private TabHost _tabHost;
        private ViewPager _viewPager;
        private List<TabInfo> _tabs = new List<TabInfo>();

        public class TabInfo
        {

            public string tag;
            public Class clss;
            public Bundle args;
            public Android.Support.V4.App.Fragment fragment { get; set; }

            public TabInfo(string _tag, Class _class, Bundle _args)
            {
                tag = _tag;
                clss = _class;
                args = _args;
            }
        }

        public class DummyTabFactory : Java.Lang.Object, TabHost.ITabContentFactory
        {
            private Context _context;

            public DummyTabFactory(Context context)
            {
                _context = context;
            }

            public View CreateTabContent(string tag)
            {
                var v = new View(_context);
                v.SetMinimumHeight(0);
                v.SetMinimumWidth(0);
                return v;
            }
        }

        public TabsAdapter(FragmentActivity activity, TabHost tabHost, ViewPager pager)
            : base(activity.SupportFragmentManager)
        {
            _context = activity;
            _tabHost = tabHost;
            _viewPager = pager;
            _tabHost.SetOnTabChangedListener(this);
            _viewPager.Adapter = this;
            _viewPager.SetOnPageChangeListener(this);

        }


        public void AddTab(TabHost.TabSpec tabSpec, Class clss, Bundle args)
        {
            tabSpec.SetContent(new DummyTabFactory(_context));
            var tag = tabSpec.Tag;

            var info = new TabInfo(tag, clss, args);

            _tabs.Add(info);
            _tabHost.AddTab(tabSpec);
            NotifyDataSetChanged();
        }

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

        public override Android.Support.V4.App.Fragment GetItem(int position)
        {
            var info = _tabs[position];
            return Android.Support.V4.App.Fragment.Instantiate(_context, info.clss.Name, info.args);
        }

        public void OnTabChanged(string tabId)
        {
            int position ;
            position = _tabHost.CurrentTab;    
            _viewPager.CurrentItem = position;
        }

        public void OnPageScrolled(int position, float positionOffset, int positionOffsetPixels)
        {

        }

        public void OnPageSelected(int position)
        {
            var widget = _tabHost.TabWidget;
            var oldFocusability = widget.DescendantFocusability;
            widget.DescendantFocusability = DescendantFocusability.BlockDescendants;
            _tabHost.CurrentTab = position;
            widget.DescendantFocusability = oldFocusability;
        }



    }
}

}

0 个答案:

没有答案