2016-08-24 7 views
0

私はこのライブラリhttps://github.com/roughike/BottomBarを使用して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)

あなたactivity_main.xmlFrameLayoutBottomBarを追加します。

FrameLayoutは、お客様のFragmentコンテナになります。だから、fragment_containerのようなIDを与えてください。

2)

あなたは下のバーのタブを持っている限り多くのフラグメントを作成します。また、それらのレイアウトを作成します。 R.id.frag_1-3各断片の主要レイアウトのIDである

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

を以下のように

3)

編集リスナー。ここで

はcommitFragment方法である:

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