Search code examples
androidbindingandroid-viewpagermvvmcross

ViewPager binding issues with ViewModel


I am using a ViewPager to implement tabs in MvvmCross 4.0.0-beta5. However, EditText binding property of ViewModel is always null. I also tried updating to MvvmCross 4.0.0-beta7, but no luck.

Here is my ViewPager

 <android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <include
        android:id="@+id/mspManagerToolbar"
        layout="@layout/mspmanagertoolbar" />
    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="16dp"
        local:tabGravity="fill"
        local:tabMode="scrollable" />
    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        local:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.AppBarLayout>

Here is my tab view

<EditText
    ...
    local:MvxBind="Text Title" />

The above EditText binding not working. Title always getting null.

<EditText
    ...
    local:MvxBind="Text User.Title" />

Above EditText binding working perfect. User.Title getting correct value.

Here ViewPagerView.cs code

public class CustomerDetailView : MvxCachingFragmentCompatActivity<CustomerDetailViewModel>
{
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        SetContentView(Resource.Layout.CustomerDetailView);

        // Use toolbar as action bar
        var toolbar = FindViewById<Toolbar>(Resource.Id.mspManagerToolbar);

        if (toolbar != null)
        {
            // Toolbar will now take on default actionbar characteristics
            SetSupportActionBar(toolbar);

        }

        // View Pager
        var viewPager = FindViewById<ViewPager>(Resource.Id.viewpager);
        if (viewPager != null)
        {
            // Add tabs in view pager
            var fragments = new List<MvxFragmentStatePagerAdapter.FragmentInfo>
            {
                new MvxFragmentStatePagerAdapter.FragmentInfo(TAB_DETAIL, typeof(Tab_CustomerDetail), typeof(CustomerDetailViewModel)),
                new MvxFragmentStatePagerAdapter.FragmentInfo(TAB_CONTACT, typeof(Tab_CustomerContact), typeof(CustomerDetailViewModel)),
                new MvxFragmentStatePagerAdapter.FragmentInfo(TAB_LOCATION, typeof(Tab_CustomerLocation), typeof(CustomerDetailViewModel)),
                new MvxFragmentStatePagerAdapter.FragmentInfo(TAB_CONTRACT, typeof(Tab_CustomerContract), typeof(CustomerDetailViewModel))                   
            };
            viewPager.Adapter = new MvxFragmentStatePagerAdapter(this, SupportFragmentManager, fragments);
        }

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

Here is my Fragment Code

[Register("app.droid.views.fragments.Tab_CustomerDetail")]
public class Tab_CustomerDetail : MvxFragment
{        

    #region Public Method

    /// <summary>
    /// tab customer detail view
    /// </summary>
    /// <param name="inflater"></param>
    /// <param name="container"></param>
    /// <param name="savedInstanceState"></param>
    /// <returns></returns>
    public override View OnCreateView(Android.Views.LayoutInflater inflater, Android.Views.ViewGroup container, Android.OS.Bundle savedInstanceState)
    {
        var ignored = base.OnCreateView(inflater, container, savedInstanceState);

        var view = this.BindingInflate(Resource.Layout.frg_tab_CustomerDetail, null);


        return view;
    }

    #endregion

}

Here is ViewModel Properties

public string Title
{
    get
    {
        return (title);
    }
    set
    {
        if (value != title)
        {
            title = value;

            RaisePropertyChanged(() => this.Title);
        }
    }
}

public User User
{
    get
    {
        return (repositoryService.User);
    }
    set
    {
        repositoryService.User = value;
        RaisePropertyChanged(() => User);
    }
}

Title property getting null and User.Title is working perfect. So please let me know where I going wrong ?


Solution

  • The problem was i save my data from activity, so i did not getting value of view. Because for every fragment it create new instance of view model, since not getting updated value of view. Now I save my data from fragment and now i getting all values of view in view model.