粗糙的底部栏是设置默认项目,在去新的活动
问题描述:
我已经创建了一个Android应用程序,有一个底部导航栏。我使用粗糙的底部导航栏,并传递了每个项目的意图。在每次活动中,我再次创建了底部栏。 但问题是,当新的活动被创建时,选择的默认项目被重置。请帮助我。粗糙的底部栏是设置默认项目,在去新的活动
答
您应该使用片段代替,将您的底部栏放置在您的活动和由其处理的框架布局中。
可以实现类似的东西用下面的代码:
MainActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.roughike.bottombar.BottomBar
android:id="@+id/bottomBar"
android:layout_width="match_parent"
android:layout_alignParentBottom="true"
app:bb_tabXmlResource="@xml/bottombar_tabs"
android:layout_height="60dp"
app:bb_inActiveTabColor="@color/inActiveTabColor"
app:bb_inActiveTabAlpha="0.8"
app:bb_activeTabAlpha="1"
app:bb_showShadow="false"/>
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/imageview_logo"
android:layout_above="@id/bottomBar"
android:layout_alignParentStart="true">
</FrameLayout>
</RelativeLayout>
MainActivity.java
in your onCreate method(), do the following.
bottomBar = (BottomBar) findViewById(R.id.bottomBar);
bottomBar.selectTabAtPosition(FIRST_FRAGMENT_INDEX); //Default: FIRST
bottomBar.setOnTabSelectListener(new OnTabSelectListener() {
private int last_index = FIRST_FRAGMENT_INDEX;
@Override
public void onTabSelected(@IdRes int tabId) {
//Creating the Fragment transaction
FragmentTransaction transaction = fragmentManager.beginTransaction();
switch (tabId) {
case R.id.tab_SECOND:
if (SECOND_FRAGMENT_INDEX > last_index) {
FragmentArrayList.get(SECOND_FRAGMENT_INDEX).setEnterTransition(new Slide(Gravity.RIGHT));
//transaction.setCustomAnimations(R.animator.slide_from_right, R.animator.slide_to_left);
} else if (SECOND_FRAGMENT_INDEX < last_index) {
FragmentArrayList.get(SECOND_FRAGMENT_INDEX).setEnterTransition(new Slide(Gravity.LEFT));
//transaction.setCustomAnimations(R.animator.slide_from_left, R.animator.slide_to_right);
}
transaction.replace(R.id.fragment_container, FragmentArrayList.get(SECOND_FRAGMENT_INDEX));
last_index = SECOND_FRAGMENT_INDEX;
break;
case R.id.tab_FIRST:
if (FIRST_FRAGMENT_INDEX > last_index) {
FragmentArrayList.get(FIRST_FRAGMENT_INDEX).setEnterTransition(new Slide(Gravity.RIGHT));
//transaction.setCustomAnimations(R.animator.slide_from_right, R.animator.slide_to_left);
} else if (FIRST_FRAGMENT_INDEX < last_index) {
FragmentArrayList.get(FIRST_FRAGMENT_INDEX).setEnterTransition(new Slide(Gravity.LEFT));
//transaction.setCustomAnimations(R.animator.slide_from_left, R.animator.slide_to_right);
}
transaction.replace(R.id.fragment_container, FragmentArrayList.get(FIRST_FRAGMENT_INDEX));
last_index = FIRST_FRAGMENT_INDEX;
break;
case R.id.tab_THIRD:
if (THIRD_FRAGMENT_INDEX > last_index) {
FragmentArrayList.get(THIRD_FRAGMENT_INDEX).setEnterTransition(new Slide(Gravity.RIGHT));
//transaction.setCustomAnimations(R.animator.slide_from_right, R.animator.slide_to_left);
} else if (THIRD_FRAGMENT_INDEX < last_index) {
FragmentArrayList.get(THIRD_FRAGMENT_INDEX).setEnterTransition(new Slide(Gravity.LEFT));
//transaction.setCustomAnimations(R.animator.slide_from_left, R.animator.slide_to_right);
}
transaction.replace(R.id.fragment_container, FragmentArrayList.get(THIRD_FRAGMENT_INDEX));
last_index = THIRD_FRAGMENT_INDEX;
break;
case R.id.tab_FOURTH:
if (FOURTH_FRAGMENT_INDEX > last_index) {
FragmentArrayList.get(FOURTH_FRAGMENT_INDEX).setEnterTransition(new Slide(Gravity.RIGHT));
//transaction.setCustomAnimations(R.animator.slide_from_right, R.animator.slide_to_left);
} else if (FOURTH_FRAGMENT_INDEX < last_index) {
FragmentArrayList.get(FOURTH_FRAGMENT_INDEX).setEnterTransition(new Slide(Gravity.LEFT));
//transaction.setCustomAnimations(R.animator.slide_from_left, R.animator.slide_to_right);
}
transaction.replace(R.id.fragment_container, FragmentArrayList.get(FOURTH_FRAGMENT_INDEX));
last_index = FOURTH_FRAGMENT_INDEX;
break;
}
transaction.commit();
}
});
欢迎SO!请访问[如何提出一个好问题](http://stackoverflow.com/help/how-to-ask)并提供[最小,完整且可验证的示例](http://stackoverflow.com/help/mcve )到目前为止你所拥有的。 – Olaia