Android-新的BottomNavigationBar中的PageView-防止重新加载片段 [英] Android - PageView in new BottomNavigationBar - prevent reload fragments
问题描述
在我的StartActivity中,BottomNavigationBar侦听器具有以下设置:
In my StartActivity the BottomNavigationBar Listener has the following setup:
private GuideFragment guideFragment = new GuideFragment();
private MapFragment mapFragment = new MapFragment();
private MoreFragment moreFragment = new MoreFragment();
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment selectedFragment = null;
switch (item.getItemId()) {
case R.id.navigation_guide:
selectedFragment = guideFragment;
break;
case R.id.navigation_map:
selectedFragment = mapFragment;
break;
case R.id.navigation_more:
selectedFragment = moreFragment;
break;
}
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.content, selectedFragment);
transaction.commit();
return true;
}
};
如上所述,我想防止所选片段始终重新加载源/视图.我在片段中尝试了一些类似的东西:
As I mentioned above I want to prevent that the selected fragments always reloads the sources/view. I tried out some stuff like - in the fragments:
if (rootView == null)
inflater.inflate...
但是这些片段仍然会重新创建视图并加载(以我为例)新的webresources.
But the fragments still recreate the view and load (in my case) webresources new.
我阅读了PageView可以提供帮助的内容,尤其是
I read something that a PageView could help, especially
offScreenPageLimit
应该做魔术.
我的主要问题是我应该在哪里实现PageViewer-我的StartActivity是否可以实现?还是可以通过其他方式解决问题?
My main question is where should I implement a PageViewer - Is it possible in my StartActivity? Or can I solve the problem in an other way?
推荐答案
我做到了!不需要 ViewPager
.
这是我的解决方案(所有代码均编码在 StartActivity
中,而不是 Fragments
中):
Here is my solution (all coded in StartActivity
not in Fragments
):
private final GuideFragment guideFragment = new GuideFragment();
private final MapFragment mapFragment = new MapFragment();
private final MoreFragment moreFragment = new MoreFragment();
private final android.app.FragmentManager fm = getFragmentManager();
Fragment active = guideFragment;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_guide:
if(active != guideFragment) {
fm.beginTransaction().show(guideFragment).commit();
}
else {
fm.beginTransaction().hide(active).show(guideFragment).commit();
}
active = guideFragment;
break;
case R.id.navigation_map:
fm.beginTransaction().hide(active).show(mapFragment).commit();
active = mapFragment;
break;
case R.id.navigation_more:
fm.beginTransaction().hide(active).show(moreFragment).commit();
active = moreFragment;
break;
}
return true;
}
};
在onCreate列表中,事务将提交.
and in onCreate list the transaction commits.
fm.beginTransaction().add(R.id.content,moreFragment).commit();
fm.beginTransaction().add(R.id.content, mapFragment).commit();
fm.beginTransaction().add(R.id.content, guideFragment).commit();
如果您有3个标签页,那么最后提交第一个标签页片段(fragm3,fragm2,fragm1)非常重要.
It is very important to commit the first tabs fragment last(fragm3,fragm2,fragm1) if you have 3 tabs.
通过不加载每个新片段/刷新,现在可以在智能手机上实现高速性能.
Highly speed performance on the smartphone now by not loading every fragment new/refresh.
这篇关于Android-新的BottomNavigationBar中的PageView-防止重新加载片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!