Skip to content

Instantly share code, notes, and snippets.

@borichellow
Last active July 2, 2017 14:46
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save borichellow/f7dd87dc4c81419efe48d1026b561b98 to your computer and use it in GitHub Desktop.
Save borichellow/f7dd87dc4c81419efe48d1026b561b98 to your computer and use it in GitHub Desktop.
extension for BottomNavigationView from Design Support Library
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomNavigationView;
import android.util.AttributeSet;
import android.view.MenuItem;
import javax.annotation.Nonnull;
public class BottomNavigationViewExtended extends BottomNavigationView {
private ItemSelectedListener listener;
private ItemIndexSelectedListener listenerByIndex;
private int currentItem = 0;
public BottomNavigationViewExtended(Context context) {
super(context);
init();
}
public BottomNavigationViewExtended(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public BottomNavigationViewExtended(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
public void setCurrentItem(int itemIndex) {
final boolean wasChecked = checkItem(itemIndex);
if (listener != null) {
listener.onItemSelected(getMenu().getItem(itemIndex), wasChecked);
} else if (listenerByIndex != null) {
listenerByIndex.onItemSelected(itemIndex, wasChecked);
}
}
public int getCurrentItem() {
return currentItem;
}
public MenuItem getItem(int itemIndex) {
return getMenu().getItem(itemIndex);
}
public void setItemSelectedListener(@Nullable ItemSelectedListener listener) {
this.listener = listener;
}
public void setItemSelectedListener(@Nullable ItemIndexSelectedListener listener) {
this.listenerByIndex = listener;
}
private void init() {
setOnNavigationItemSelectedListener(new OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
final boolean wasChecked = checkItem(item);
if (listener != null) {
return listener.onItemSelected(item, wasChecked);
}
return listenerByIndex != null && listenerByIndex.onItemSelected(getItemIndex(item), wasChecked);
}
});
}
private boolean checkItem(int itemIndex) {
if (itemIndex < 0) return true;
if (currentItem == itemIndex) return true;
for (int i = 0; i < getMenu().size(); i++) {
getMenu().getItem(i).setChecked(i == itemIndex);
}
currentItem = itemIndex;
return false;
}
private boolean checkItem(MenuItem item) {
item.setChecked(true);
return checkItem(getItemIndex(item));
}
/**
* @return item's index in Menu or -1 if item was not found
*/
private int getItemIndex(MenuItem item) {
for (int i = 0; i < getMenu().size(); i++) {
if (getMenu().getItem(i).equals(item)) {
return i;
}
}
return -1;
}
public interface ItemSelectedListener {
/**
* Called when an item in the bottom navigation menu is selected.
*
* @param item The selected item
* @param wasSelected was this item selected before
* @return true to display the item as the selected item
*/
boolean onItemSelected(@Nonnull MenuItem item, boolean wasSelected);
}
public interface ItemIndexSelectedListener {
/**
* Called when an item in the bottom navigation menu is selected.
*
* @param itemIndex The selected item's index
* @param wasSelected was this item selected before
* @return true to display the item as the selected item
*/
boolean onItemSelected(int itemIndex, boolean wasSelected);
}
}
@borichellow
Copy link
Author

borichellow commented Oct 26, 2016

NOTE: To use it you have to use Design Support Library, starting with version 25.0.0.

This Extended View has all the same methods like the parent (you can find it's description here )
And more:

  • setCurrentItem(int i) set checked item at the i place AND notify listener about changes
  • getCurrentItem() returns currently checked item index
  • getItem(int i) returns MenuItem object at the i place

+ There are 2 kind of Listener Interfaces that you can provide (use which one you want):

  1. ItemSelectedListener with method onItemSelected which has parameters: MenuItem item which was checked and boolean wasSelected which provide you "was this item checked before" and user just click on it again
  2. ItemIndexSelectedListener with method onItemSelected which has parameters: int itemIndex index of item that was checked and boolean wasSelected which provide you "was this item checked before" and user just click on it again

(If both listeners were set it will use only the first one with MenuItem)

@BenoitDuffez
Copy link

BenoitDuffez commented Dec 10, 2016

You use javax Nonnul annotation, maybe the one from android.support.annotation would be more suitable. Otherwise, thanks a lot for your code!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment