diff --git a/README.md b/README.md index 8d92ef4..76fa2ce 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,8 @@ [![AwesomeAndroid](https://img.shields.io/badge/Awesome_Android-BubbleNavigation-purple.svg?style=flat)](https://android.libhunt.com/bubble-navigation-alternatives) 🎉 A light-weight library to make beautiful Navigation Bar easily with ton of 🎨 customization option. - +
+**orientation,and first item selected bug and other bugs fixed** ## Demos | FloatingTopBarActivity | TopBarActivity | diff --git a/bubblenavigation/src/main/java/com/gauravk/bubblenavigation/BubbleNavigationLinearView.java b/bubblenavigation/src/main/java/com/gauravk/bubblenavigation/BubbleNavigationLinearView.java index 0392a72..3d1742c 100644 --- a/bubblenavigation/src/main/java/com/gauravk/bubblenavigation/BubbleNavigationLinearView.java +++ b/bubblenavigation/src/main/java/com/gauravk/bubblenavigation/BubbleNavigationLinearView.java @@ -37,198 +37,115 @@ @SuppressWarnings("unused") public class BubbleNavigationLinearView extends LinearLayout implements View.OnClickListener { - //constants - private static final String TAG = "BNLView"; - private static final int MIN_ITEMS = 2; - private static final int MAX_ITEMS = 5; - - private ArrayList bubbleNavItems; - private BubbleNavigationChangeListener navigationChangeListener; - + private String CURRENT_ACTIVE_VIEW = "x.f"; private int currentActiveItemPosition = 0; + private static final String TAG = "bnlview"; + private BubbleNavigationChangeListener navigationChangeListener = null; - /** - * Constructors - */ - public BubbleNavigationLinearView(@NonNull Context context) { - super(context); - init(context, null); - } + public BubbleNavigationLinearView(Context context, AttributeSet attrs) { + super(context,attrs); + init(); - public BubbleNavigationLinearView(@NonNull Context context, @Nullable AttributeSet attrs) { - super(context, attrs); - init(context, attrs); } - - public BubbleNavigationLinearView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) { - super(context, attrs, defStyleAttr); - init(context, attrs); - } - - @Override - protected Parcelable onSaveInstanceState() { - Bundle bundle = new Bundle(); - bundle.putParcelable("superState", super.onSaveInstanceState()); - bundle.putInt("current_item", currentActiveItemPosition); - return bundle; + public BubbleNavigationLinearView(Context context) { + super(context); + init(); } - @Override - protected void onRestoreInstanceState(Parcelable state) { - if (state instanceof Bundle) { - Bundle bundle = (Bundle) state; - currentActiveItemPosition = bundle.getInt("current_item"); - state = bundle.getParcelable("superState"); - } - super.onRestoreInstanceState(state); + public BubbleNavigationLinearView(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + init(); } - ///////////////////////////////////////// - // PRIVATE METHODS - ///////////////////////////////////////// - - /** - * Initialize - * - * @param context current context - * @param attrs custom attributes - */ - private void init(Context context, AttributeSet attrs) { - + public void init() { setOrientation(HORIZONTAL); setGravity(Gravity.CENTER); + } - post(new Runnable() { - @Override - public void run() { - updateChildNavItems(); - } - }); - } - - /** - * Finds Child Elements of type {@link BubbleToggleView} and adds them to {@link #bubbleNavItems} - */ - private void updateChildNavItems() { - bubbleNavItems = new ArrayList<>(); - for (int index = 0; index < getChildCount(); ++index) { - View view = getChildAt(index); - if (view instanceof BubbleToggleView) - bubbleNavItems.add((BubbleToggleView) view); - else { - Log.w(TAG, "Cannot have child bubbleNavItems other than BubbleToggleView"); - return; - } - } - - if (bubbleNavItems.size() < MIN_ITEMS) { - Log.w(TAG, "The bubbleNavItems list should have at least 2 bubbleNavItems of BubbleToggleView"); - } else if (bubbleNavItems.size() > MAX_ITEMS) { - Log.w(TAG, "The bubbleNavItems list should not have more than 5 bubbleNavItems of BubbleToggleView"); - } - + protected void onFinishInflate() { + super.onFinishInflate(); + checkViewsAreAllBubble(); setClickListenerForItems(); - setInitialActiveState(); + refreshActiveState(); updateMeasurementForItems(); } - /** - * Makes sure that ONLY ONE child {@link #bubbleNavItems} is active - */ - private void setInitialActiveState() { + private void refreshActiveState() { boolean foundActiveElement = false; - for (int i = 0; i < bubbleNavItems.size(); i++) { - if (bubbleNavItems.get(i).isActive() && !foundActiveElement) { + for (int i=0;i 0) { - int calculatedEachItemWidth = (getMeasuredWidth() - (getPaddingRight() + getPaddingLeft())) / numChildElements; - for (BubbleToggleView btv : bubbleNavItems) - btv.updateMeasurements(calculatedEachItemWidth); + int numChildViews = getChildCount(); + if(numChildViews > 0) { + int calculatedEachItemWidth = (getMeasuredWidth() - (getPaddingRight() + getPaddingLeft())) / numChildViews; + for(int i=0;i= bubbleNavItems.size()) - return; - BubbleToggleView btv = bubbleNavItems.get(position); + if(currentActiveItemPosition == position) return; + if(position < 0 || position >= this.getChildCount()) { + return ; + } + BubbleToggleView btv = (BubbleToggleView)this.getChildAt(position); btv.performClick(); } - @Override public void onClick(View v) { int changedPosition = getItemPositionById(v.getId()); @@ -236,20 +153,24 @@ public void onClick(View v) { if (changedPosition == currentActiveItemPosition) { return; } - BubbleToggleView currentActiveToggleView = bubbleNavItems.get(currentActiveItemPosition); - BubbleToggleView newActiveToggleView = bubbleNavItems.get(changedPosition); - if (currentActiveToggleView != null) - currentActiveToggleView.toggle(); - if (newActiveToggleView != null) - newActiveToggleView.toggle(); - + BubbleToggleView currentActiveToggleView = (BubbleToggleView)this.getChildAt(currentActiveItemPosition); + BubbleToggleView newActiveToggleView = (BubbleToggleView)this.getChildAt(changedPosition); + currentActiveToggleView.toggle(); + newActiveToggleView.toggle(); //changed the current active position currentActiveItemPosition = changedPosition; - - if (navigationChangeListener != null) - navigationChangeListener.onNavigationChanged(v, currentActiveItemPosition); + navigationChangeListener.onNavigationChanged(v, currentActiveItemPosition); } else { Log.w(TAG, "Selected id not found! Cannot toggle"); } } + + public void onSaveInstanceState(Bundle bundle) { + bundle.putInt(CURRENT_ACTIVE_VIEW,currentActiveItemPosition); + } + + public void onRestoreInstanceState(Bundle bundle) { + int currentActiveView = bundle.getInt(CURRENT_ACTIVE_VIEW,0); + setCurrentActiveItem(currentActiveView); + } }