0
工作時是按後退按鈕我的片段被改變爲主頁,但底部的圖標沒有改變。我在這裏發佈了我的所有代碼。如果我從底部導航視圖項目中選擇多個然後兩個導航按鈕,然後當我按回按鈕它將重定向到上次選擇的按鈕項目。
但現在發生了什麼,假設我在最後一個項目上說'通知'[正如你可以在屏幕截圖中看到的],現在當我按回按鈕時,它會直接帶我到'主頁'按鈕,但我想要的是,它應該首先讓我「搜索」按鈕項,然後到「家」不直接到「家」。
我該如何做到這一點?
這裏的佈局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:design="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.segunfamisa.sample.bottomnav.MainActivity">
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#f1f1f1">
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start"
design:menu="@menu/bottom_nav_items" />
</LinearLayout>
這裏的活動:
public class MainActivity extends AppCompatActivity {
private static final String SELECTED_ITEM = "arg_selected_item";
private BottomNavigationView mBottomNav;
private int mSelectedItem;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBottomNav = (BottomNavigationView) findViewById(R.id.navigation);
mBottomNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
selectFragment(item);
return true;
}
});
MenuItem selectedItem;
if (savedInstanceState != null) {
mSelectedItem = savedInstanceState.getInt(SELECTED_ITEM, 0);
selectedItem = mBottomNav.getMenu().findItem(mSelectedItem);
} else
{
selectedItem = mBottomNav.getMenu().getItem(0);
}
selectFragment(selectedItem);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putInt(SELECTED_ITEM, mSelectedItem);
super.onSaveInstanceState(outState);
}
@Override
public void onBackPressed() {
MenuItem homeItem = mBottomNav.getMenu().getItem(0);
if (mSelectedItem != homeItem.getItemId()) {
// select home item
// homeItem.setCheckable(true);
Log.d("backpressids","**** "+mSelectedItem);
selectFragment(homeItem);
} else {
super.onBackPressed();
}
}
private void selectFragment(MenuItem item) {
Fragment frag = null;
item.setCheckable(true);
// init corresponding fragment
switch (item.getItemId()) {
case R.id.menu_home:
TabFragmentOne fragmentone = new TabFragmentOne();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.container, fragmentone);
ft.commit();
break;
case R.id.menu_notifications:
TabFragmentThree fragmentone_Three = new TabFragmentThree();
FragmentTransaction ft_three = getSupportFragmentManager().beginTransaction();
ft_three.replace(R.id.container, fragmentone_Three);
ft_three.commit();
break;
case R.id.menu_search:
TabFragmentTwo tabFragmentTwo = new TabFragmentTwo();
FragmentTransaction ft_two = getSupportFragmentManager().beginTransaction();
ft_two.replace(R.id.container,tabFragmentTwo);
ft_two.commit();
break;
}
// update selected item
mSelectedItem = item.getItemId();
updateToolbarText(item.getTitle());
if (frag != null) {
TabFragmentOne fragmentone = new TabFragmentOne();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.container, fragmentone);
ft.commit();
}
}
private void updateToolbarText(CharSequence text) {
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setTitle(text);
}
}
}
請幫我任何一個.....
感謝您的幫助,它的工作完美。 但我更新了我的問題,你可以幫我嗎? –
我會....但請不要嘗試修改原來的問題後得到預期的答案。這會讓其他用戶感到困惑。更好地發佈您的新需求的另一個問題。希望你明白:) – FAT
謝謝你的建議.. –