2017-03-23 21 views
0

我已經創建了一個Android應用程序,有一個底部導航欄。我使用粗糙的底部導航欄,並傳遞了每個項目的意圖。在每次活動中,我再次創建了底部欄。 但問題是,當新的活動被創建時,選擇的默認項目被重置。請幫助我。粗糙的底部欄是設置默認項目,在去新的活動

+0

歡迎SO!請訪問[如何提出一個好問題](http://stackoverflow.com/help/how-to-ask)並提供[最小,完整且可驗證的示例](http://stackoverflow.com/help/mcve )到目前爲止你所擁有的。 – Olaia

回答

0

您應該使用片段代替,將您的底部欄放置在您的活動和由其處理的框架佈局中。

可以實現類似的東西用下面的代碼:

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(); 
     } 

    });