我在使用Xamarin和MvvmCross的Android中的选项卡出现问题。首先开始一切都很完美,当我离开并回来时,问题就会出现。当我回到包含选项卡布局的片段时,它是空白的,直到我开始滑动,然后再次创建页面。MvvmCross选项卡片段中断导航
这似乎表明它可以通过简单地调用Adapter.NotifyDataSetChanged()来解决,但事实并非如此。我创建它时运行的相同代码在导航回到视图时也会运行。
我的代码的设置与this sample完全一样唯一的区别是我的MainView不包含选项卡,它是放置在包含选项卡布局的FragmentFrame中的子片段之一。
我的设置
使用Xamarin和MvvmCross基本的Android碎片设置。我有一个容器视图,其中有一个片段框架。我所有的片段都是MvxFragments,它的属性与它关联,它指示包含它们的视图模型以及将它们放入的帧的ID。我使用默认的AndroidFragmentPresenter。
有没有人经历过此之前。这里有一些事情我已经尝试:
感动了所有的寻呼机和适配器安装到UI线程
只有创建和设置一个新的适配器当电流适配器为空
OnViewCreated调用_adapter.NotifyDataSetChanged()
随着3我还做了适配器中的GetItemPosition覆盖,并返回PositionNone重新加载所有项目。
设置页面索引到一个不同的指数回报率来强制滚动,但我只有3页,所以中间是不会再创建
弃置viewPager,适配器和tabLayout上DestroyView
编辑1:我添加了一个确切场景的例子here这是我的生产代码的精简版。当您按下FAB并使用后退按钮返回时,您会看到选项卡适配器为空。那就是错误。
我会加我的代码,因为你不想看那个回购。
片段用制表符(布局)
<?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>
片段查看代码
// 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;
}