Skip to content

Instantly share code, notes, and snippets.

@CopyAndPasteHub
Created June 21, 2016 12:54
Show Gist options
  • Save CopyAndPasteHub/459a37cf83ddbf818eda39095a6876b1 to your computer and use it in GitHub Desktop.
Save CopyAndPasteHub/459a37cf83ddbf818eda39095a6876b1 to your computer and use it in GitHub Desktop.
Bottom bar navigation with 3 fragments -> https://goo.gl/RiwNgm
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="@+id/container">
</FrameLayout>
<?xml version="1.0" encoding="utf-8"?>
<!--Bottom bar menu by roughike-->
<!--https://github.com/roughike/BottomBar-->
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<!--Every item for another tab-->
<item
android:id="@+id/bottomBarItemOne"
android:icon="@drawable/ic_looks_one_black_24dp"
android:title="First item" />
<item
android:id="@+id/bottomBarItemSecond"
android:icon="@drawable/ic_looks_two_black_24dp"
android:title="Second item" />
<item
android:id="@+id/bottomBarItemThird"
android:icon="@drawable/ic_looks_3_black_24dp"
android:title="Third item" />
</menu>
//dependencies
compile 'com.ncapdevi:frag-nav:1.0.3'
compile 'com.roughike:bottom-bar:1.3.9'
public class MainActivity extends AppCompatActivity {
private BottomBar mBottomBar;
private FragNavController fragNavController;
//indices to fragments
private final int TAB_FIRST = FragNavController.TAB1;
private final int TAB_SECOND = FragNavController.TAB2;
private final int TAB_THIRD = FragNavController.TAB3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//FragNav
//list of fragments
List<Fragment> fragments = new ArrayList<>(3);
//add fragments to list
fragments.add(FirstFragment.newInstance(0));
fragments.add(SecondFragment.newInstance(0));
fragments.add(ThirdFragment.newInstance(0));
//link fragments to container
fragNavController = new FragNavController(getSupportFragmentManager(),R.id.container,fragments);
//End of FragNav
//BottomBar menu
mBottomBar = BottomBar.attach(this, savedInstanceState);
mBottomBar.setItems(R.menu.bottombar_menu);
mBottomBar.setOnMenuTabClickListener(new OnMenuTabClickListener() {
@Override
public void onMenuTabSelected(@IdRes int menuItemId) {
//switch between tabs
switch (menuItemId) {
case R.id.bottomBarItemOne:
fragNavController.switchTab(TAB_FIRST);
break;
case R.id.bottomBarItemSecond:
fragNavController.switchTab(TAB_SECOND);
break;
case R.id.bottomBarItemThird:
fragNavController.switchTab(TAB_THIRD);
break;
}
}
@Override
public void onMenuTabReSelected(@IdRes int menuItemId) {
if (menuItemId == R.id.bottomBarItemOne) {
fragNavController.clearStack();
}
}
});
//End of BottomBar menu
}
@Override
public void onBackPressed() {
if (fragNavController.getCurrentStack().size() > 1) {
fragNavController.pop();
} else {
super.onBackPressed();
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// Necessary to restore the BottomBar's state, otherwise we would
// lose the current tab on orientation change.
mBottomBar.onSaveInstanceState(outState);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment