How to create Tabs using Android Xamarin with Mvvm

2019-07-11 03:05发布

问题:

I'm trying to implement a tabs control of ViewPager inside the android project of a MVVMCross xamarin solution. The binding is working in the windows phone app so i assume the Viewmodel is working.

I have an activity that inherits MvxTabsFragmentActivity that i want to add Tabs into. My app can not detect the id : Resource.Id.actualtabcontent.

This is how i Attempted it, (https://github.com/Cheesebaron/Cheesebaron.MvvmCross.Bindings/blob/master/Bindings.Droid/BindableViewPager.cs) as well as attempting this https://github.com/MvvmCross/MvvmCross-Tutorials/tree/master/Fragments/FragmentSample.UI.Droid

Does anyone have any idea about how we can solve this?

basically we have a parent viewmodel with a collection of LIst inside of it, but we are struggling to get the binding to work inside of android. we have tried even creating a new app with just the tabs in it, any ideas?

回答1:

I wouldn't use MvxTabsFragmentActivity as it is a bit outdated. This sample is the latest available: https://github.com/MvvmCross/MvvmCross-AndroidSupport/tree/master/Samples

Your layout should be something like this:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="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"
        local:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        local: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"
        local:tabGravity="center"
        local: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"
    local:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>

The TabLayout makes the tabs for you.