2
我的當前實現FragmentStatePagerAdapter每次被viewpager調用時都會創建新的片段。防止FragmentStatePagerAdapter在viewpager被下一個或之前刷過的時候創建新的片段
我想保存先前加載的碎片狀態並重新使用它們。
我認爲要實現這一點,我必須實現InstantiateItem但不清楚如何?
這是我FragmentStatePagerAdapter
代碼using System.Collections.Generic;
//using Android.App;
using Android.Views;
using Hollard.MLAM.Android.Fragments.Fna;
using Object = Java.Lang.Object;
using Android.Support.V4.App;
public FnaFragmentPagerAdapter(FragmentManager fm)
: base(fm)
{
_registeredFragments = new Dictionary<int, Fragment>();
}
public override Fragment GetItem(int position)
{
Fragment fragment = null;
switch ((FnaStep)position)
{
case FnaStep.First:
fragment = new FnaStep1Fragment();
break;
case FnaStep.Second:
fragment = new FnaStep2Fragment();
break;
case FnaStep.Third:
fragment = new FnaStep3Fragment();
break;
case FnaStep.Fourth:
fragment = new FnaStep4Fragment();
break;
case FnaStep.Fifth:
fragment = new FnaStep5Fragment();
break;
case FnaStep.Sixth:
fragment = new FnaStep6Fragment();
break;
default:
break;
}
return fragment;
}
public override int Count
{
get { return PageCount; }
}
public Fragment GetRegisteredFragment(ViewGroup container, int position)
{
global::Android.Support.V4.App.Fragment fragment = null;
if (!_registeredFragments.TryGetValue(position, out fragment))
{
InstantiateItem(container, position);
_registeredFragments.TryGetValue(position, out fragment);
}
return fragment;
}
}
public enum FnaStep
{
First = 0,
Second,
Third,
Fourth,
Fifth,
Sixth
}
}
請幫助我如何實現FragmentStatePagerAdapter,所以它可以重複使用這些已創建的片段。
上傳你的活動java文件代碼.. –