0

I must implement ViewPager for swiping DetailViewFragments to do that I wrote an Adapter (using https://github.com/Cheesebaron/ViewPagerIndicator ) and it works fine, but during rotation the active fragment is not shown. ViewPager however is there and working, even creating new fragments ( ones that were not created before rotation). I've browsed this site and found just bunch of similar not answered questions but because MvvmCross makes this special i decided to try my luck with you.

View

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;

    ...
}

Adapter

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;
    }
....

Fragment

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);
    }
}

There's nothing to complicated here but after rotation I get just background and indicator.

ozapa
  • 281
  • 4
  • 16
  • if you are displaying the same layout for both rotations, then the easiest thing to do is to disable activity recreation on rotation. You can do this by setting the configChanges attribute for your Activity - similar to the Java code in http://stackoverflow.com/questions/456211/activity-restart-on-rotation-android and – Stuart Apr 13 '14 at 17:42
  • Thanx for pointing direction. I ended using RetainInstance=true; in OnCreateView in MvxFragment class. – ozapa Apr 13 '14 at 22:52

0 Answers0