我必须实现ViewPager来滑动DetailViewFragments来做我编写的适配器(使用https://github.com/Cheesebaron/ViewPagerIndicator)并且它工作正常,但在旋转期间,活动片段不会显示。然而,ViewPager在那里工作,甚至创建新的片段(在旋转之前没有创建的片段)。我浏览了这个网站,发现了一堆类似的未回答的问题,但因为MvvmCross使这个特殊,我决定试试你的运气。
查看
protected override void OnCreate(Bundle bundle)
{
SetTheme(Resource.Style.Theme_ActionBar_Custom);
base.OnCreate(bundle);
SetContentView(Resource.Layout.TipListView);
var models = _viewModel.SomeModelList;
_viewPager = FindViewById<ViewPager>(Resource.Id.pager);
_adapter = new MvxFragmentStatePagerAdapter<ViewModel, Model>
(this, SupportFragmentManager, typeof(Fragment), models,
()=>FuncToBuildViewModel);
_viewPager.Adapter = _adapter;
var indicator = FindViewById<UnderlinePageIndicator>(Resource.Id.indicator);
indicator.SetViewPager(_viewPager);
indicator.Fades = false;
if (bundle == null)
_adapterPosition = models.GetPosition(firstModelToShow);
else
_adapterPosition = bundle.GetInt("adapterposition");
_viewPager.CurrentItem = _adapterPosition;
...
}
适配器
public class MvxFragmentStatePagerAdapter<TViewModel, TModel> : FragmentStatePagerAdapter
where TViewModel : BaseViewModel<TModel>
where TModel : BaseModel, new()
{
//introduction of constructor parameters
public MvxFragmentStatePagerAdapter(Context context, FragmentManager fragmentManager, Type fragmentType, List<TModel> models, Func<TViewModel> vmCtor)
: base(fragmentManager)
{ //initialization of constructor parameters }
public override int Count { get { return _models.Count; } }
public override Fragment GetItem(int position)
{
var fragment = Fragment.Instantiate(_context, FragmentJavaName(_fragmentType));
((MvxFragment)fragment).DataContext = BuildVM(position);
return fragment;
}
protected virtual string FragmentJavaName(Type fragmentType)
{
var namespaceText = fragmentType.Namespace ?? string.Empty;
if (namespaceText.Length > 0)
{
namespaceText = namespaceText.ToLowerInvariant() + ".";
}
return namespaceText + fragmentType.Name;
}
....
片段
public class MyFrag : MvxFragment
{
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var ignored = base.OnCreateView(inflater, container, savedInstanceState);
return this.BindingInflate(Resource.Layout.MyFrag, null);
}
}
这里没有什么可复杂的,但在轮换后,我只得到背景和指示。