粗糙的底部栏正在设置默认项目,在转到新活动时



我创建了一个Android应用程序,它有一个底部导航栏。我正在使用粗略的底部导航栏,并为每个项目传递意图。在每个活动中,我再次创建了底部栏。但问题是,在创建新活动时,所选的默认项正在重置。请帮帮我。

您应该改用片段,将底栏放在您的活动中,并由它处理框架布局。

您可以使用以下代码实现类似目标:

主要活动.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>

主要活动.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();
        }
    });

相关内容

  • 没有找到相关文章

最新更新