2016-11-14 54 views
0

我最近使用AHBottomNavigation,其中有5個項目在底欄中,例如 我點擊Home Button它會進入homeActivty,我有3個標籤欄。直到這裏的一切都沒問題,但是我意識到當我們使用AHBottomNavigation時,我們應該使用片段,但是我需要使用帶有條形的活動並在其中顯示AHBottomNavigation。 我在我的所有活動中都使用AHBottomNavigation來處理這個問題,它的工作和我的所有活動都有AHBottomNavigation,但是當我更改活動時,AHBottomNavigation並不像平常那​​樣工作,而且似乎延遲了一段時間。 有沒有更好的解決方案或請告訴我,如果我做錯了什麼?帶標籤欄和底欄的多活動管理

HomeActivity.class

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_home); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 


     setSupportActionBar(toolbar); 
     getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 


     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
       this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
     if (drawer != null) { 
      drawer.setDrawerListener(toggle); 
     } 
     toggle.syncState(); 

     NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
     navigationView.setNavigationItemSelectedListener(this); 

     //[START Tab ] 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

     viewPager = (ViewPager) findViewById(R.id.viewpager); 
     setupViewPager(viewPager); 

     tabLayout = (TabLayout) findViewById(R.id.tabs); 
     tabLayout.setupWithViewPager(viewPager); 
     //[*End Tab] 

     //[Start Bottom N] 
     bottomNavigation(); 

    } 

     private void setupViewPager(ViewPager viewPager){ 
     ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager()); 
     adapter.addFragment(new Fragment1(),"Tab1"); 
     adapter.addFragment(new Fragment2() , "Tab2"); 
     adapter.addFragment(new Fragment3() , "Tab3"); 
     viewPager.setAdapter(adapter); 
    } 
    public void bottomNavigation(){ 
     // [START BottomNavigation] 
     // TODO add other activitys to setOnTabSelectedListener (HOME , SEARCH , ...) 
     AHBottomNavigation bottomNavigation = (AHBottomNavigation) findViewById(R.id.bottom_navigation); 
     // Create items 

     AHBottomNavigationAdapter navigationAdapter = new AHBottomNavigationAdapter(this, R.menu.bottom_navigation); 
     navigationAdapter.setupWithBottomNavigation(bottomNavigation); 


     bottomNavigation.setBehaviorTranslationEnabled(true); 
     bottomNavigation.setAccentColor(Color.parseColor("#3498db")); 
     bottomNavigation.setCurrentItem(0); 

     bottomNavigation.setOnTabSelectedListener(new AHBottomNavigation.OnTabSelectedListener() { 
      @Override 
      public boolean onTabSelected(int position, boolean wasSelected) { 


       switch (position){ 
        case 0 : 

         break; 
        case 1 : 
         // Add 
         Intent goToAddStuff = new Intent(HomeActivity.this , AddStuffActivity.class); 
         startActivity(goToAddStuff); 
         finish(); 
         break; 
        case 2 : 
         // Search 
         break; 
        case 3: 
         // Activity's 
        //My AONActivity have 2 tab too 
         Intent goToAON = new Intent(HomeActivity.this , AONActivity.class); 
         startActivity(goToAON); 
         finish(); 
         break; 
        case 4: 
         //Account 
         break; 

       } 
       return true; 
      } 
     }); 
     // [*END BottomNavigation] 
    } 

content_home.xml

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
app:layout_behavior="@string/appbar_scrolling_view_behavior" 
tools:context=".HomeActivity" 
android:id="@+id/content_id" 
tools:showIn="@layout/activity_home"> 


<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context=".HomeActivity"> 


    <android.support.v4.view.ViewPager 
     android:id="@+id/viewpager" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 

</RelativeLayout> 



    <FrameLayout 
     android:id="@+id/contentContainer" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_above="@+id/bottomBar" /> 

    <include layout="@layout/bottom_navigation" /> 

</android.support.design.widget.CoordinatorLayout> 

bottom_navigation.xml

<com.aurelhubert.ahbottomnavigation.AHBottomNavigation 

    android:id="@+id/bottom_navigation" 
    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="wrap_content" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    android:layout_gravity="bottom"> 

</com.aurelhubert.ahbottomnavigation.AHBottomNavigation> 
+0

考慮分享您的代碼爲更好undestanding –

回答

1

我也用AHBottomNavigation但你需要由於谷歌材料設計需要使用Fragments,否則你執行它不正確。

這裏就應該是這樣:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    > 

<FrameLayout 
    android:id="@+id/framelayout_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    /> 

<com.aurelhubert.ahbottomnavigation.AHBottomNavigation 
    android:id="@+id/bottom_navigation" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    /> 

</RelativeLayout> 

這是一些代碼只是爲了顯示如何更好,如果你不知道用碎片的工作,只是我不知道你的背景,也許你不使用這是因爲你對此感到困惑。

@Override 
public void onTabSelected(int position, boolean wasSelected) { 
    Log.d(TAG, "Position " + position + " was selected" + wasSelected); 
    if(!wasSelected) { 
     getSupportFragmentManager() 
       .beginTransaction() 
       .replace(FRAME_LAYOUT, 
         createFragment(position), 
         createItemDescription(position)) 
       .addToBackStack(null) 
       .commit(); 
    } 
} 

private void createBottomNavigationMenu(int defaultCurrentItem){ 

    for(int i = 0; i<5; i++){ 
     // Create bottom navigation item 
     AHBottomNavigationItem item = 
       new AHBottomNavigationItem(createItemDescription(i), 
              createPicture(i)); 
     // Add bottom navigation item 
     mBottomNavigation.addItem(item); 
    } 

// Set current item programmatically 
    mBottomNavigation.setCurrentItem(defaultCurrentItem, true); 

// Set background color 
    mBottomNavigation.setDefaultBackgroundColor(Color.parseColor("#56ABF8")); 

    // Disable the translation inside the CoordinatorLayout 
    mBottomNavigation.setBehaviorTranslationEnabled(false); 

    // Set listener 
    mBottomNavigation.setOnTabSelectedListener(this); 
} 

// Factory-method patterns 
private Fragment createFragment(int position){ 
    switch (position) { 
     case 1: 
      return new SettingsFragment_(); 
     case 2: 
      return new StatisticsFragment_(); 
     case 3: 
      return new MainFragment_(); 
     case 4: 
      return new ForthFragment_(); 
     case 5: 
      return new ReminderFragment_(); 
    } 
    throw new IllegalArgumentException(); 
} 

private String createItemDescription(int position){ 
    switch (position){ 
     case 1: 
      return BOT_NAV_ITEM_SETTINGS; 
     case 2: 
      return BOT_NAV_ITEM_STATISTICS; 
     case 3: 
      return BOT_NAV_ITEM_MAIN; 
     case 4: 
      return BOT_NAV_ITEM_FORTH; 
     case 5: 
      return BOT_NAV_ITEM_REMINDERS; 
    } 
    throw new IllegalArgumentException(); 
} 

private int createPicture(int position){ 
    switch (position){ 
     case 1: 
      return R.mipmap.ic_settings; 
     case 2: 
      return R.mipmap.ic_statistics; 
     case 3: 
      return R.mipmap.ic_water; 
     case 4: 
      return R.mipmap.ic_launcher; 
     case 5: 
      return R.mipmap.ic_reminders; 
    } 
    throw new IllegalArgumentException(); 
} 
0

我發現解決方案

創建HomeFragment何種程度片段是這樣的:

HomeFragment.class

public class HomeFragment extends Fragment { 


    public static TabLayout tabLayout; 
    public static ViewPager viewPager; 

    private static int currentItem = 0; 
    AdapterFragment adapter; 

    public static HomeFragment newInstance(int someInt) { 
     HomeFragment frag = new HomeFragment(); 
     Bundle args = new Bundle(); 
     args.putInt("tab", someInt); 
     frag.setArguments(args); 
     return frag; 
    } 

    @Override 
    public void onSaveInstanceState(Bundle outState) { 
     super.onSaveInstanceState(outState); 
     outState.putInt("currentItem", viewPager.getCurrentItem()); 
    } 

    public static HomeFragment newInstance() { 
     HomeFragment frag = new HomeFragment(); 
     return frag; 
    } 

    @Override 
    public void onCreate(@Nullable Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     adapter= new AdapterFragment(getChildFragmentManager()); 
     currentItem=getArguments() != null ? getArguments().getInt("tab") : 0; 
    } 



    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     /** 
     *Inflate tab_layout and setup Views. 
     */ 
     View rootView = inflater.inflate(R.layout.fragment_home,container,false); 

     tabLayout = (TabLayout) rootView.findViewById(R.id.tablayout); 
     viewPager = (ViewPager) rootView.findViewById(R.id.viewpager); 

     /** 
     *Set an Adapter for the View Pager 
     */ 
     if (adapter==null) adapter=new AdapterFragment(getChildFragmentManager()); 
     viewPager.setAdapter(adapter); 
     viewPager.setCurrentItem(currentItem); 

     /** 
     * Now , this is a workaround , 
     * The setupWithViewPager dose't works without the runnable . 
     * Maybe a Support Library Bug . 
     */ 

     tabLayout.post(new Runnable() { 
      @Override 
      public void run() { 
       tabLayout.setupWithViewPager(viewPager); 
      } 
     }); 

     return rootView; 

    } 

    public class AdapterFragment extends FragmentPagerAdapter { 

     public AdapterFragment(FragmentManager fm) { 
      super(fm); 
     } 

     /** 
     * Return fragment with respect to Position . 
     */ 

     @Override 
     public Fragment getItem(int position) 
     { 
      switch (position){ 
       case 0 : return new Tab1Fragment(); 
       case 1 : return new Tab2Fragment(); 
       case 2 : return new Tab3Fragment(); 
      } 
      return null; 
     } 

     @Override 
     public int getCount() { 

      return 3; 

     } 


     @Override 
     public CharSequence getPageTitle(int position) { 

      switch (position){ 
       case 0 : 
        return "Tab1"; 
       case 1 : 
        return "Tab2"; 
       case 2 : 
        return "Tab3"; 
      } 
      return null; 
     } 
    } 

在我

MainActivity

bottomNavigation.setOnTabSelectedListener(new AHBottomNavigation.OnTabSelectedListener() { 
      @Override 
      public boolean onTabSelected(int position, boolean wasSelected) { 

       switch (position){ 
        case 0 : 
         // Home 
         HomeFragment homeFragment = new HomeFragment(); 
         getSupportFragmentManager() 
           .beginTransaction() 
           .replace(R.id.content_id, homeFragment) 
           .commit(); 


         break; 
        case 1: 
         break; 
       } 
       return true; 
      } 
     }); 

我希望這將有助於你

+0

不再保留TabLayout和ViewPager爲靜態變量的最佳解決方案。這將是內存泄漏的原因,我們需要避免這種行爲。 – aleksandrbel