我有一个带导航抽屉的应用程序,它为每个菜单项使用片段 每次单击一个项目时,我都会替换当前的片段。
问题是在用户点击后显示新片段需要很长时间才会卡住。 加载时间最长的片段是一个片段,其中还包含子片段(一个片段包含RecyclerView)。 有什么方法可以加快碎片的装载速度吗? (如果可能的话,也许事先将它们初始化?)
这是我的代码:
protected override void OnCreate(Bundle bundle)
{
drawerLayout = FindViewById<DrawerLayout>(Resource.Id.drawer_layout);
navigationView = FindViewById<NavigationView>(Resource.Id.nav_view);
drawerLayout.DrawerClosed += DrawerLayout_DrawerClosed;
navigationView.NavigationItemSelected += (sender, e) =>
{
e.MenuItem.SetChecked(true);
//react to click here and swap fragments or navigate
switch (e.MenuItem.ItemId)
{
case (Resource.Id.nav_home):
ListItemClicked(0);
break;
case (Resource.Id.nav_halachot):
ListItemClicked(1);
break;
case (Resource.Id.nav_times):
ListItemClicked(2);
break;
case (Resource.Id.nav_siddur):
ListItemClicked(3);
break;
case (Resource.Id.nav_compass):
ListItemClicked(4);
break;
case (Resource.Id.nav_settings):
ListItemClicked(5);
break;
}
drawerLayout.CloseDrawers();
};
if (bundle == null)
{
ListItemClicked(0);
navigationView.Menu.GetItem(0).SetChecked(true);
fragment = new HomeFragment();
SupportFragmentManager.BeginTransaction()
.Replace(Resource.Id.content_frame, fragment)
.Commit();
}
}
public override void OnBackPressed()
{
if (drawerLayout.IsDrawerOpen((int)GravityFlags.Start))
{
drawerLayout.CloseDrawer((int)GravityFlags.Start);
}
else
{
base.OnBackPressed();
}
}
private void ListItemClicked(int position)
{
switch (position)
{
case 0:
fragment = new HomeFragment();
Title = "Home";
SupportActionBar.Elevation = 8;
break;
case 1:
fragment = new HalachaFragment();
Title = "aaa";
SupportActionBar.Elevation = 0;
break;
case 2:
fragment = new TimesFragment();
Title = "bbb";
SupportActionBar.Elevation = 8;
break;
case 3:
fragment = new SiddurFragment();
Title = "ccc";
SupportActionBar.Elevation = 8;
break;
case 4:
fragment = new CompassFragment();
Title = "ddd";
SupportActionBar.Elevation = 8;
break;
case 5:
fragment = new SettingsFragment();
Title = "eee";
break;
}
}
private void DrawerLayout_DrawerClosed(object sender, DrawerLayout.DrawerClosedEventArgs e)
{
SupportFragmentManager.BeginTransaction()
.Replace(Resource.Id.content_frame, fragment).AddToBackStack("BACK")
.Commit();
}
HalachaFragment.cs(包含标签的片段):
public class HalachaFragment : Fragment
{
private ViewPager halachotPager;
private PagerSlidingTabStrip tabs;
public HalachaFragment()
{
this.RetainInstance = true;
}
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your fragment here
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Use this to return your custom view for this Fragment
var view = inflater.Inflate(Resource.Layout.HalachaSection, container, false);
var fragments = new Android.Support.V4.App.Fragment[]
{
new HalachotFragment(),
new BooksFragment(),
};
var titles = CharSequence.ArrayFromStringArray(new[]
{
"הלכות",
"ספרים",
});
halachotPager = view.FindViewById<ViewPager>(Resource.Id.halachotPager);
halachotPager.Adapter = new TabsFragmentPagerAdapter(this.ChildFragmentManager, fragments, titles);
halachotPager.OffscreenPageLimit = 2;
halachotPager.SetCurrentItem(1, true);
// Bind the tabs to the ViewPager
tabs = view.FindViewById<PagerSlidingTabStrip>(Resource.Id.halachotTabs);
tabs.SetViewPager(halachotPager);
return view;
}
}
希望有人可以帮助我。
感谢。
答案 0 :(得分:0)
请参阅this解决方案。只需使用一个线程来加载片段子。更改onItemClick时。
layoutDrawer.closeDrawer(linearDrawer);
setLastPosition(posicao);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
setFragmentList(lastPosition);
}
}, 0);