我想在我的項目中創建一個底部導航欄。每個視圖都有它自己的片段。問題是,每次我點擊按鈕來改變視圖,例如從最近到最喜歡它創建新的片段與全新的狀態(例如,滾動位置,文字改變我的片段包含)。我知道在官方的Android文檔中寫道,底部的導航欄應該重置任務狀態,但我認爲這對用戶來說太不舒服。 我想有一種類似的功能,如Instagram,你從飼料改變到探索和回滾飼料的滾動位置圖像緩存一切仍然保存。我幾乎想通過各種方式解決這個問題,唯一有效的方法是通過設置可見性GONE並根據情況設置可見性VISIBLE,但我知道這不是正確的方法,應該有更好的方法來做到這一點,我不是在說手動節省需要的實例。我幾乎跟隨了關於底部導航片段的所有教程,但有趣的是,沒有人有興趣在不每次調用新的情況下使用它。BottomNavigationView - 如何避免片段的重新創建並重用它們
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frameLayout, FirstFragment.newInstance());
fragmentTransaction.commit();
bottomNavigationView = (BottomNavigationView) findViewById(R.id.navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment fragment = null;
switch (item.getItemId()) {
case R.id.menu_dialer:
fragment = FirstFragment.newInstance();
break;
case R.id.menu_email:
fragment = SecondFragment.newInstance();
break;
case R.id.menu_map:
fragment = ThirdFragment.newInstance();
break;
}
if (fragment != null) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frameLayout, fragment);
fragmentTransaction.commit();
}
return true;
}
});
我也試過onAttach和Deattach解決方案,而且又沒有成功。
VIDEO LINK : new i tried Nino Handler version it only works when i tap on the same fragment button
也許連接,我用我的gradle產出依賴金絲雀版本還是出問題了嗎?
最新更新:
public class MainActivity extends AppCompatActivity {
private TextView mTextMessage;
private static final String TAG_FRAGMENT_ONE = "fragment_one";
private static final String TAG_FRAGMENT_TWO = "fragment_two";
private FragmentManager fragmentManager;
private Fragment currentFragment;
String TAG = "babken";
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
Fragment fragment = null;
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
fragment = fragmentManager.findFragmentByTag(TAG_FRAGMENT_ONE);
if (fragment == null) {
fragment = FragmentFirst.newInstance();
}
replaceFragment(fragment, TAG_FRAGMENT_ONE);
break;
case R.id.navigation_dashboard:
fragment = fragmentManager.findFragmentByTag(TAG_FRAGMENT_TWO);
if (fragment == null) {
fragment = FragmentSecond.newInstance();
}
replaceFragment(fragment, TAG_FRAGMENT_TWO);
break;
}
return true;
}
};
private void replaceFragment(@NonNull Fragment fragment, @NonNull String tag) {
if (!fragment.equals(currentFragment)) {
fragmentManager
.beginTransaction()
.replace(R.id.armen, fragment, tag)
.commit();
currentFragment = fragment;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragmentManager = getSupportFragmentManager();
Fragment fragment = fragmentManager.findFragmentByTag(TAG_FRAGMENT_ONE);
if (fragment == null) {
fragment = FragmentFirst.newInstance();
}
replaceFragment(fragment, TAG_FRAGMENT_ONE);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}
}
嗨,我有完全相同的問題..你能給我一些關於解決方案或一些代碼的細節嗎? – 2017-11-30 11:39:31
你的問題在你自己的問題上工作嗎?我也有同樣的問題。 @ArmenHovhannisian – TiagoIB