Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
roughike committed Mar 16, 2016
1 parent 458af07 commit 6b8bd1e
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 9 deletions.
22 changes: 20 additions & 2 deletions app/src/main/java/com/example/bottombar/sample/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.example.bottombar.sample;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import com.roughike.bottombar.BottomBarLayout;
import com.roughike.bottombar.BottomBarTab;
Expand All @@ -21,10 +24,25 @@ protected void onCreate(Bundle savedInstanceState) {
new BottomBarTab(R.drawable.ic_nearby, "Nearby")
);

final TextView sampleText = (TextView) findViewById(R.id.sampleText);

bottomBarLayout.setOnItemSelectedListener(new OnTabSelectedListener() {
@Override
public void onItemSelected(int position) {

public void onItemSelected(final int position) {
sampleText.animate()
.setDuration(150)
.alpha(0.0f)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
sampleText.clearAnimation();
sampleText.setText("This would be screen number " + (position + 1));
sampleText.animate()
.setDuration(150)
.alpha(1)
.start();
}
}).start();
}
});
}
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
android:layout_height="match_parent">

<TextView
android:id="@+id/sampleText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
android:text="This would be screen number 1" />

</com.roughike.bottombar.BottomBarLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;
Expand All @@ -19,6 +21,8 @@
public class BottomBarLayout extends RelativeLayout implements View.OnClickListener {
private static final long ANIMATION_DURATION = 150;

private static final String STATE_CURRENT_TAB = "STATE_CURRENT_TAB";

private static final String TAG_BOTTOM_BAR_VIEW_INACTIVE = "BOTTOM_BAR_VIEW_INACTIVE";
private static final String TAG_BOTTOM_BAR_VIEW_ACTIVE = "BOTTOM_BAR_VIEW_ACTIVE";

Expand All @@ -33,6 +37,7 @@ public class BottomBarLayout extends RelativeLayout implements View.OnClickListe
private int mMaxItemWidth;

private OnTabSelectedListener mListener;
private int mCurrentTabPosition;

public BottomBarLayout(Context context) {
super(context);
Expand Down Expand Up @@ -113,9 +118,9 @@ public void setItems(BottomBarTab... bottomBarTabs) {
MiscUtils.setTextAppearance(title, R.style.BB_BottomBarItem_Fixed_Title);

if (index == 0) {
activateView(bottomBarView, false);
selectTab(bottomBarView, false);
} else {
inActivateView(bottomBarView, false);
unselectTab(bottomBarView, false);
}

if (bottomBarView.getWidth() > biggestWidth) {
Expand Down Expand Up @@ -150,7 +155,7 @@ public void setOnItemSelectedListener(OnTabSelectedListener listener) {
mListener = listener;
}

private void activateView(ViewGroup bottomBarView, boolean animate) {
private void selectTab(ViewGroup bottomBarView, boolean animate) {
bottomBarView.setTag(TAG_BOTTOM_BAR_VIEW_ACTIVE);

((ImageView) bottomBarView.findViewById(R.id.bottom_bar_icon)).setColorFilter(mPrimaryColor);
Expand All @@ -174,7 +179,7 @@ private void activateView(ViewGroup bottomBarView, boolean animate) {
}
}

private void inActivateView(ViewGroup bottomBarView, boolean animate) {
private void unselectTab(ViewGroup bottomBarView, boolean animate) {
bottomBarView.setTag(TAG_BOTTOM_BAR_VIEW_INACTIVE);

((ImageView) bottomBarView.findViewById(R.id.bottom_bar_icon)).setColorFilter(mInActiveColor);
Expand Down Expand Up @@ -211,8 +216,8 @@ private void clearItems() {
@Override
public void onClick(View v) {
if (v.getTag().equals(TAG_BOTTOM_BAR_VIEW_INACTIVE)) {
inActivateView((ViewGroup) findViewWithTag(TAG_BOTTOM_BAR_VIEW_ACTIVE), true);
activateView((ViewGroup) v, true);
unselectTab((ViewGroup) findViewWithTag(TAG_BOTTOM_BAR_VIEW_ACTIVE), true);
selectTab((ViewGroup) v, true);

if (mListener != null) {
int position = 0;
Expand All @@ -227,7 +232,63 @@ public void onClick(View v) {
}

mListener.onItemSelected(position);
mCurrentTabPosition = position;
}
}
}

private void selectTabAtPosition(int position) {
unselectTab((ViewGroup) mItemContainer.findViewWithTag(TAG_BOTTOM_BAR_VIEW_ACTIVE), false);
selectTab((ViewGroup) mItemContainer.getChildAt(position), false);

if (mListener != null) {
mListener.onItemSelected(position);
}
}

@Override
public Parcelable onSaveInstanceState() {
Parcelable superState = super.onSaveInstanceState();
SavedState ss = new SavedState(superState);
ss.selectedTabPosition = mCurrentTabPosition;
return ss;
}

@Override
public void onRestoreInstanceState(Parcelable state) {
SavedState ss = (SavedState) state;
super.onRestoreInstanceState(ss);
mCurrentTabPosition = ss.selectedTabPosition;
selectTabAtPosition(mCurrentTabPosition);
}

static class SavedState extends BaseSavedState {
int selectedTabPosition;

protected SavedState(Parcelable superState) {
super(superState);
}

private SavedState(Parcel in) {
super(in);
selectedTabPosition = in.readInt();
}

@Override
public void writeToParcel(Parcel out, int flags) {
super.writeToParcel(out, flags);
out.writeInt(selectedTabPosition);
}

public static final Parcelable.Creator<SavedState> CREATOR
= new Parcelable.Creator<SavedState>() {
public SavedState createFromParcel(Parcel in) {
return new SavedState(in);
}

public SavedState[] newArray(int size) {
return new SavedState[size];
}
};
}
}

0 comments on commit 6b8bd1e

Please sign in to comment.