在mvvmcross中使用带有新ToolBar(AppCompat v7-21)的Tab

时间:2015-06-10 18:05:29

标签: android tabs xamarin android-toolbar

我使用support_v7工具栏作为ActionBar,但是在我的活动中添加标签时出现问题。是否有任何示例添加工具栏选项卡。   我看了http://blog.ostebaronen.dk/2013/11/getting-support-v7-working-with.html

1 个答案:

答案 0 :(得分:2)

我的工作方式与普通的Android应用程序相同。使用Gabriele Mariotti链接的布局。要使viewpager在MvvmCross中使用绑定,您需要使用以下内容:https://github.com/MvvmCross/MvvmCross-AndroidSupport/blob/master/Cirrious.MvvmCross.Droid.Support.V4/MvxFragmentStatePagerAdapter.cs

为了更好的可读性:https://gist.github.com/martijn00/23b00172af9e2c798f3d

以下是一些代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:layout_scrollFlags="scroll|enterAlways" />
        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="16dp"
            app:tabGravity="center"
            app:tabMode="scrollable" />
    </android.support.design.widget.AppBarLayout>
    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>


    public class SomeFragment : MvxFragment<SomeViewModel>
    {
    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        var view = base.OnCreateView(inflater, container, savedInstanceState);

        var viewPager = view.FindViewById<Android.Support.V4.View.ViewPager>(Resource.Id.viewpager);
        if (viewPager != null)
        {
            var fragments = new List<MvxViewPagerFragmentAdapter.FragmentInfo>
            {
                new MvxViewPagerFragmentAdapter.FragmentInfo
                {
                    FragmentType = typeof(SomeFragment1),
                    Title = "",
                    ViewModel = ViewModel.ViewModelSomething1
                },
                new MvxViewPagerFragmentAdapter.FragmentInfo
                {
                    FragmentType = typeof(SomeFragment2),
                    Title = ViewModel.ViewModelPopular.TextSource.GetText("Title"),
                    ViewModel = ViewModel.ViewModelSomething2
                },
                new MvxViewPagerFragmentAdapter.FragmentInfo
                {
                    FragmentType = typeof(SomeFragment3),
                    Title = ViewModel.ViewModelContributors.TextSource.GetText("Title"),
                    ViewModel = ViewModel.ViewModelSomething3
                }
            };
            viewPager.Adapter = new MvxViewPagerFragmentAdapter(Activity, Activity.SupportFragmentManager, fragments);
        }

        var tabLayout = view.FindViewById<TabLayout>(Resource.Id.tabs);
        tabLayout.SetupWithViewPager(viewPager);

        return view;
    }
}

public class MvxFragmentStatePagerAdapter
        : FragmentStatePagerAdapter
    {
        private readonly Context _context;

        public IEnumerable<FragmentInfo> Fragments { get; private set; }

        public override int Count
        {
            get { return Fragments.Count(); }
        }

        protected MvxFragmentStatePagerAdapter(IntPtr javaReference, JniHandleOwnership transfer)
            : base(javaReference, transfer)
        {
        }

        public MvxFragmentStatePagerAdapter(
            Context context, FragmentManager fragmentManager, IEnumerable<FragmentInfo> fragments)
            : base(fragmentManager)
        {
            _context = context;
            Fragments = fragments;
        }

        public override Fragment GetItem(int position)
        {
            var fragmentInfo = Fragments.ElementAt(position);
            var fragment = Fragment.Instantiate(_context,
                FragmentJavaName(fragmentInfo.FragmentType));
            ((MvxFragment)fragment).ViewModel = fragmentInfo.ViewModel;
            return fragment;
        }

        protected static string FragmentJavaName(Type fragmentType)
        {
            var namespaceText = fragmentType.Namespace ?? "";
            if (namespaceText.Length > 0)
                namespaceText = namespaceText.ToLowerInvariant() + ".";
            return namespaceText + fragmentType.Name;
        }

        public override ICharSequence GetPageTitleFormatted(int position)
        {
            return new Java.Lang.String(Fragments.ElementAt(position).Title);
        }

        public class FragmentInfo
        {
            public string Title { get; set; }

            public Type FragmentType { get; set; }

            public IMvxViewModel ViewModel { get; set; }
        }
    }