0

I'm having a problem with Tabs in Android using Xamarin and MvvmCross. On first start everything works perfect, the problem occurs when I navigate away and come back. When I come back to the fragment that contains the tab layout it's blank, until I start swiping, then the pages are created again.

This seems to indicate that it can be fixed by simply calling Adapter.NotifyDataSetChanged() but that's not the case. The same code that runs on the first time I create it also runs when I navigate back to the view.

My code is setup exactly like in this sample The only difference being that my MainView does not contain the tabs, it is one of the child fragments that gets placed in that FragmentFrame that contains the tab layout.

My setup

The basic Android Fragment setup using Xamarin and MvvmCross. I have a container view with a fragment frame in it. All my fragments are MvxFragments with the attribute associated on it that indicates the viewmodel that contains them as well as the id for the frame to place them in. I use the default AndroidFragmentPresenter.

Has anyone experienced this before. Here are some things I have tried:

  1. Moved all the pager and adapter setup onto the UI thread

  2. Only create and set a new adapter when the current adapter is null

  3. OnViewCreated call _adapter.NotifyDataSetChanged()

  4. Along with 3 I also did an override on GetItemPosition in the adapter and returned PositionNone to reload all items.

  5. Set the page index to a different index on return to force a scroll, but I only have 3 pages so the middle one is never created again

  6. Disposed viewPager, adapter and tabLayout on DestroyView

EDIT 1: I added an example of the exact scenario here this is a stripped down version of my production code. When you press the FAB and use the back button to go back you'll see that the tab adapter is empty. And that is the bug.

I'll add my code too incase you don't want to look at that repo.

Fragment With Tabs (Layout)

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">
    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_margin="10dp"
            android:gravity="center">
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                <EditText
                    android:id="@+id/simple_search_edit_text"
                    android:layout_width="match_parent"
                    android:layout_height="60dp"
                    android:paddingLeft="70dp"
                    android:hint="New Search"
                    android:textColorHint="@color/light_grey"
                    android:background="#fff"
                    android:imeOptions="actionSearch"
                    android:inputType="text"
                    local:MvxBind="Text SearchText" />
                <ImageView
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:layout_marginLeft="20dp"
                    android:layout_centerVertical="true"
                    android:padding="3dp"
                    local:srcCompat="@drawable/mag_glass_gray" />
                <ImageView
                    android:layout_width="40dp"
                    android:layout_height="40dp"
                    android:layout_marginRight="20dp"
                    android:padding="8dp"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"
                    local:srcCompat="@drawable/general_cog_icon" />
            </RelativeLayout>
        </android.support.v7.widget.CardView>
        <android.support.design.widget.TabLayout
            android:id="@+id/main_tablayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            style="@style/SampleTabsTabLayout" />
    </android.support.design.widget.AppBarLayout>
    <android.support.v4.view.ViewPager
        android:id="@+id/main_view_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        local:layout_behavior="@string/appbar_scrolling_view_behavior" />
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <Button
            android:id="@+id/simple_search_advanced_search_btn"
            android:layout_width="wrap_content"
            android:layout_height="@dimen/simplesearch_advancedsearch_height"
            android:layout_marginBottom="@dimen/simplesearch_advanced_search_margin_bottom"
            android:layout_above="@+id/simple_search_fab"
            android:layout_centerHorizontal="true"
            android:gravity="center"
            android:textSize="@dimen/simplesearch_advanced_search_text_size"
            android:textColor="@color/foundit_blue"
            android:background="@android:color/transparent"
            local:MvxBind="Click AdvancedSearchCommand; Text AdvancedSearchButtonText" />
    <!-- Search Button -->
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/simple_search_fab"
            android:layout_width="@dimen/general_fab_dimensions"
            android:layout_height="@dimen/general_fab_dimensions"
            android:layout_marginBottom="@dimen/general_fab_margin_bottom"
            android:padding="@dimen/simplesearch_fab_icon_padding"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:scaleType="centerInside"
            android:src="@drawable/general_search_icon"
            local:MvxBind="Click SearchCommand" />
    </RelativeLayout>
</android.support.design.widget.CoordinatorLayout>

Fragment View Code

// Pager and adapter setup
var fragments = GetTabFragments();
var presenter = Mvx.Resolve<IMvxAndroidViewPresenter>() as AndroidViewPresenter;
_adapter = new SimpleSearchTabAdapter(Context, presenter.FragmentManager, fragments);
_fragmentPager.Adapter = _adapter;

_tabLayout.SetupWithViewPager(_fragmentPager);

// Just for completion sake of the code above    
 private List<MvxCachingFragmentStatePagerAdapter.FragmentInfo> GetTabFragments()
 {
   var fragments = new List<MvxCachingFragmentStatePagerAdapter.FragmentInfo>();

   foreach (var tabViewModel in ViewModel.TabViewModels)
     fragments.Add(new MvxCachingFragmentStatePagerAdapter.FragmentInfo(GetTabNameFromViewModel(tabViewModel),
                                                                           GetViewTypeFromViewModel(tabViewModel),
                                                                           tabViewModel));

   return fragments;
 }
Filled Stacks
  • 4,116
  • 1
  • 23
  • 36

1 Answers1

0

So the issue was that I was using the FragmentManager of the Activity that was housing the fragment instead of the ChildFragmentManager within the fragment.

Helpful information from this answer

Filled Stacks
  • 4,116
  • 1
  • 23
  • 36