我有一个名为accesors
的片段,里面还有另一个片段,其中包含卡片,您可以在gif中看到。问题是,每当我转到另一个片段时,当我回到HomeFragment.java
时,所有卡都会刷新并得到新的卡...我如何才能停止刷新?
这是我的HomeFragment.java
HomeFragment.java
这是我的public class HomeFragment extends Fragment {
private ViewPager mPager;
private CardStackAdapter mAdapter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View viewHome = inflater.inflate(R.layout.fragment_home, container, false);
mPager = (ViewPager) viewHome.findViewById(R.id.viewPagerHome);
mAdapter = new CardStackAdapter(getActivity().getSupportFragmentManager());
mPager.setPageTransformer(true, new CardStackTransformer());
mPager.setOffscreenPageLimit(5);
mPager.setAdapter(mAdapter);
final TextView txtHomeTitulo = viewHome.findViewById(R.id.txtHomeTitulo);
final TextView txtHomeDesc = viewHome.findViewById(R.id.txtHomeDesc);
final ImageView imgCard1 = viewHome.findViewById(R.id.imgFotoPais);
Typeface tpTitulos = Typeface.createFromAsset(getContext().getAssets(),"fonts/RobotoSlab-Bold.ttf");
Typeface tpDesc = Typeface.createFromAsset(getContext().getAssets(),"fonts/RobotoSlab-Light.ttf");
txtHomeTitulo.setTypeface(tpTitulos);
txtHomeDesc.setTypeface(tpDesc);
return viewHome;
}
private class CardStackTransformer implements ViewPager.PageTransformer {
@Override
public void transformPage(View page, float position) {
if (position >= 0)
{
page.setScaleX(0.8f - 0.2f * position);
page.setScaleY(0.8f);
page.setTranslationX(- page.getWidth()*position);
page.setTranslationY(30 * position);
}
}
}
}
,其中包含所有导航工具栏片段:
MainActivityjava
如何停止刷新?
答案 0 :(得分:0)
每次选择导航项时,您都在创建一个新的片段。
尝试将片段作为类变量,这些变量在MainActivity的onCreate()中初始化。然后使用这些初始化的片段设置selectedFragment,而不是创建新的片段。
编辑 样本更改:
public class MainActivity extends AppCompatActivity {
BottomNavigationView bottomNav;
HomeFragment mHomeFragment;
ExploreFragment mExploreFragment;
CameraFragment mCameraFragment;
ProfileFragment mProfileFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// MODO IMERSIVO //
immersiveMode(); // Método para colocar em modo imersivo
mHomeFragment = new HomeFragment();
mExploreFragment = new ExploreFragment();
mCameraFragment = new CameraFragment();
mProfileFragment = new ProfileFragment();
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragmentContainer, selectedFragment)
.commit();
bottomNav = findViewById(R.id.bottomNavigationView);
bottomNav.setOnNavigationItemSelectedListener(navListener);
}
private BottomNavigationView.OnNavigationItemSelectedListener
navListener =
new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment selectedFragment = null;
switch (item.getItemId()) {
case R.id.itemHome:
selectedFragment = mHomeFragment;
break;
case R.id.itemExplore:
selectedFragment = mExploreFragment;
break;
case R.id.itemCamera:
bottomNav.setVisibility(View.GONE);
selectedFragment = mCameraFragment;
break;
case R.id.itemProfile:
selectedFragment = mProfileFragment;
break;
}
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragmentContainer,selectedFragment)
.commit();
return true;
}
};
private void immersiveMode(){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
int UI_OPTIONS = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
View.SYSTEM_UI_FLAG_FULLSCREEN |
View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY |
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
getWindow().getDecorView()
.setSystemUiVisibility(UI_OPTIONS);
}
}
AFAIK,这不会导致性能问题。有关更多详细信息,您可以阅读片段的生命周期here。