简体   繁体   中英

How to give a layout for each tab (Bottom bar)

I'm using this library https://github.com/roughike/BottomBar to create a BottomBar but so far I wasn't able to add a specific layout for each tab.

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

I don't know what to do where it says change your content accordingly. Any code samples would be helpful.

1)

Add FrameLayout and BottomBar to your activity_main.xml .

Your FrameLayout will be your Fragment container. So lets give it id like fragment_container.

2)

Create as many Fragments as you have bottom bar tabs. Also create layouts for them.

3)

Edit your listener as followed

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

Where R.id.frag_1-3 is the id of the main Layout of each fragment.

Here is the commitFragment method:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM