我有一个链接到tablayout的ViewPager。我使用FragmentStatePagerAdapter来填充我的适配器,并将它与SetupWithViewPager链接到tabLayout。
如果我向我的viewpager添加500个片段,则需要2秒才能显示。
如果我删除了TabLayout的链接并将其删除,则没有延迟,所有内容都会直接显示。
是否有东西可以缓存未显示的项目tabLayout ???
有人有想法吗?
编辑:
[MvxFragment(typeof(MainViewModel), Resource.Id.content_frame, true)]
[Register("mvvmcrosslearning.droid.fragments.DateFragment")]
public class DateFragment : BaseFragment<DateViewModel>
{
protected override int FragmentId => Resource.Layout.fragment_date;
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var view = base.OnCreateView(inflater, container, savedInstanceState);
var viewPager = view.FindViewById<ViewPager>(Resource.Id.viewpager);
var tabLayout = view.FindViewById<TabLayout>(Resource.Id.tabs);
new Thread(() =>
{
if (viewPager == null) return;
var fragments = new List<MvxCachingFragmentStatePagerAdapter.FragmentInfo>();
for (var i = 0; i < 500; i++)
fragments.Add(new MvxCachingFragmentStatePagerAdapter.FragmentInfo(
$"Ceci est du text : {i}",
typeof(RecyclerViewFragment),
typeof(RecyclerViewModel),
new Device() { Date = $"index from frag: {i}" }
));
Activity.RunOnUiThread(() => viewPager.Adapter =
new MvxCachingFragmentStatePagerAdapter(Activity, ChildFragmentManager, fragments));
viewPager.OffscreenPageLimit = 1;
tabLayout.SetupWithViewPager(viewPager);
}).Start();
return view;
}
}
和视图
<?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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable"
/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
local:layout_behavior="@string/appbar_scrolling_view_behavior" >
</android.support.v4.view.ViewPager>
</android.support.design.widget.CoordinatorLayout>
片段中有一个简单的按钮。