Created
June 21, 2016 12:54
-
-
Save CopyAndPasteHub/459a37cf83ddbf818eda39095a6876b1 to your computer and use it in GitHub Desktop.
Bottom bar navigation with 3 fragments -> https://goo.gl/RiwNgm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//dependencies | |
compile 'com.ncapdevi:frag-nav:1.0.3' | |
compile 'com.roughike:bottom-bar:1.3.9' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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