2016-08-24 56 views
0

我正在使用此庫https://github.com/roughike/BottomBar創建底部條 ,但到目前爲止我無法爲每個選項卡添加特定的佈局。如何爲每個選項卡提供佈局(底部條)

public class MainActivity extends Activity { 
    @Override 
    protected void onCreate(@Nullable Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     BottomBar bottomBar = (BottomBar) findViewById(R.id.bottomBar); 
     bottomBar.setOnTabSelectListener(new OnTabSelectListener() { 
      @Override 
      public void onTabSelected(@IdRes int tabId) { 
       if (tabId == R.id.tab_favorites) { 
        // The tab with id R.id.tab_favorites was selected, 
        // change your content accordingly. 
       } 
      } 
     }); 
    } 
} 

我不知道該怎麼做才說它相應地改變你的內容。任何代碼示例都會有所幫助。

回答

1

1)

添加FrameLayoutBottomBaractivity_main.xml

您的FrameLayout將是您的Fragment集裝箱。所以讓我們給它像fragment_container id。

2)

你有底部欄標籤創建儘可能多的碎片。也爲他們創建佈局。

3)

編輯您的聽者跟着

mBottomBar.setOnMenuTabClickListener(new OnMenuTabClickListener() { 

      @Override 
      public void onMenuTabSelected(@IdRes int menuItemId) { 
       switch (menuItemId) { 
        case R.id.frag_1: 
         commitFragment(new FragmentOne()); 
         break; 
        case R.id.frag_2: 
         commitFragment(new FragmentTwo()); 
         break; 
        case R.id.frag_3: 
         commitFragment(new FragmentThree()); 
         break; 
       } 
      } 

哪裏R.id.frag_1-3是每個片段的主要佈局的ID。

這裏是commitFragment方法:

private void commitFragment(Fragment fragment){ 
     android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); 
     fragmentTransaction.replace(R.id.fragment_container, fragment); 
     fragmentTransaction.commit(); 
} 
相關問題