Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a setUpWithViewPager() method in BottomBar class #800

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,32 @@ bottomBar.setTabSelectionInterceptor(new TabSelectionInterceptor() {
});
```

### Setting up with ViewPager

If you want to set up the tabs with ViewPager (v4), which usually is the case of tabs with FragmentPagerAdapter,
you dont have to do any extra work, just pass your ViewPager in ```setUpWithViewPager()``` method. And it will behave
accordingly. Any change in tab will update fragment (ViewPager) and any change in ViewPager (slide/setCurrentItem)
will update tabs.

```java

// get viewpager instance
viewPager = (ViewPager) findViewById(R.id.viewpager_button);

// initiate viewpager adapter
ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());


// set adapter to viewpager
viewPager.setAdapter(viewPagerAdapter);

// just add viewpager to bottombar
bottomBar.setUpWithViewPager(viewPager);

// Now viewpager and bottom bar will work together
```


### Changing icons based on selection state

If you want to have different icon when a specific tab is selected, just use state list drawables.
Expand Down
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ dependencies {
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test:rules:0.5'
androidTestCompile 'org.mockito:mockito-core:1.10.19'
androidTestCompile "com.google.dexmaker:dexmaker:1.2"
androidTestCompile "com.google.dexmaker:dexmaker-mockito:1.2"
androidTestCompile 'com.google.dexmaker:dexmaker:1.2'
androidTestCompile 'com.google.dexmaker:dexmaker-mockito:1.2'
}
9 changes: 7 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,15 @@
</activity>
<activity android:name=".ThreeTabsActivity" />
<activity android:name=".IconsOnlyActivity" />
<activity android:name=".FiveColorChangingTabsActivity" android:theme="@style/AppTheme.TransNav" />
<activity android:name=".ThreeTabsQRActivity" android:theme="@style/AppTheme.TransNav" />
<activity
android:name=".FiveColorChangingTabsActivity"
android:theme="@style/AppTheme.TransNav" />
<activity
android:name=".ThreeTabsQRActivity"
android:theme="@style/AppTheme.TransNav" />
<activity android:name=".BadgeActivity" />
<activity android:name=".CustomColorAndFontActivity" />
<activity android:name=".ViewPagerActivity"></activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ protected void onCreate(Bundle savedInstanceState) {
findViewById(R.id.five_tabs_changing_colors).setOnClickListener(this);
findViewById(R.id.three_tabs_quick_return).setOnClickListener(this);
findViewById(R.id.five_tabs_custom_colors).setOnClickListener(this);
findViewById(R.id.viewpager_button).setOnClickListener(this);
findViewById(R.id.badges).setOnClickListener(this);
}

Expand All @@ -40,6 +41,9 @@ public void onClick(View v) {
case R.id.five_tabs_custom_colors:
clazz = CustomColorAndFontActivity.class;
break;
case R.id.viewpager_button:
clazz = ViewPagerActivity.class;
break;
case R.id.badges:
clazz = BadgeActivity.class;
break;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package com.example.bottombar.sample;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.roughike.bottombar.BottomBar;

/**
* The ViewPager example activity.
* This illustrates how simply you can hook up bottom bar with your viewpager by calling just one method
* Please note that you need a working viewpager adapter to achieve error free results.
*/
public class ViewPagerActivity extends AppCompatActivity {

/**
* The View pager.
*/
ViewPager viewPager;
/**
* The Bottom bar.
*/
BottomBar bottomBar;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_pager);


// initialize views by xml references
initViews();

// initiate viewpager
ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());


// set adapter to viewpager
viewPager.setAdapter(viewPagerAdapter);

// just add viewpager to bottombar
bottomBar.setUpWithViewPager(viewPager);

// Now viewpager and bottom bar will work together


}


private void initViews() {

viewPager = (ViewPager) findViewById(R.id.viewpager_button);
bottomBar = (BottomBar) findViewById(R.id.bottomBar);

}

/**
* The View pager adapter to manage fragments.
*/

public class ViewPagerAdapter extends FragmentStatePagerAdapter {


/**
* Instantiates a new View pager adapter.
*
* @param fm the SupportFragmentManager object
*/
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}



@Override
public Fragment getItem(int position) {


return SampleFragment.newInstance("This is Fragment #" + (position + 1));
}

@Override
public int getCount() {
return 3; // working with 3 fragments for now
}
}

}
6 changes: 6 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@
android:layout_height="wrap_content"
android:text="Three tabs with Quick Return" />

<Button
android:id="@+id/viewpager_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="With ViewPager" />

<Button
android:id="@+id/badges"
android:layout_width="wrap_content"
Expand Down
28 changes: 28 additions & 0 deletions app/src/main/res/layout/activity_view_pager.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.bottombar.sample.ViewPagerActivity">

<android.support.v4.view.ViewPager
android:id="@+id/viewpager_button"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">


</android.support.v4.view.ViewPager>

<com.roughike.bottombar.BottomBar
android:id="@+id/bottomBar"
android:layout_width="match_parent"
app:bb_tabXmlResource="@xml/bottombar_tabs_three"
android:layout_height="60dp">


</com.roughike.bottombar.BottomBar>

</LinearLayout>
92 changes: 70 additions & 22 deletions bottom-bar/src/main/java/com/roughike/bottombar/BottomBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPropertyAnimatorListenerAdapter;
import android.util.AttributeSet;
import android.util.Log;
Expand Down Expand Up @@ -89,6 +90,10 @@ public class BottomBar extends LinearLayout implements View.OnClickListener, Vie
private ViewGroup outerContainer;
private ViewGroup tabContainer;

// Attached ViewPager
private ViewPager viewPager;


private int defaultBackgroundColor = Color.WHITE;
private int currentBackgroundColor;
private int currentTabPosition;
Expand Down Expand Up @@ -189,7 +194,7 @@ private void populateAttributes(Context context, AttributeSet attrs, int defStyl
maxFixedItemWidth = MiscUtils.dpToPixel(getContext(), 168);

TypedArray ta = context.getTheme()
.obtainStyledAttributes(attrs, R.styleable.BottomBar, defStyleAttr, defStyleRes);
.obtainStyledAttributes(attrs, R.styleable.BottomBar, defStyleAttr, defStyleRes);

try {
tabXmlResource = ta.getResourceId(R.styleable.BottomBar_bb_tabXmlResource, 0);
Expand Down Expand Up @@ -395,7 +400,7 @@ private void resizeTabsToCorrectSizes(BottomBarTab[] tabsToAdd) {
inActiveShiftingItemWidth = (int) (proposedItemWidth * 0.9);
activeShiftingItemWidth = (int) (proposedItemWidth + (proposedItemWidth * ((tabsToAdd.length - 1) * 0.1)));
int height = Math.round(getContext().getResources()
.getDimension(R.dimen.bb_height));
.getDimension(R.dimen.bb_height));

for (BottomBarTab tabView : tabsToAdd) {
ViewGroup.LayoutParams params = tabView.getLayoutParams();
Expand Down Expand Up @@ -738,6 +743,39 @@ public void setTabTitleTypeface(String fontPath) {
setTabTitleTypeface(actualTypeface);
}

/**
* Set tab behaviour with respect to viewpager.
* If tab is changed, viewpager will also be updated.
* If viewpager is changed, tab will be updated.
*
*
* @param viewPager ViewPager object that is to be coupled with navigation bar.
*/
public void setUpWithViewPager(ViewPager viewPager) {


this.viewPager = viewPager;

if (viewPager != null) {
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

}

@Override
public void onPageSelected(int position) {
selectTabAtPosition(position, true);
}

@Override
public void onPageScrollStateChanged(int state) {

}
});
}
}

/**
* Set a custom typeface for all tab's titles.
*/
Expand Down Expand Up @@ -935,7 +973,7 @@ private boolean handleLongClick(BottomBarTab longClickedTab) {

if (shouldShowHint) {
Toast.makeText(getContext(), longClickedTab.getTitle(), Toast.LENGTH_SHORT)
.show();
.show();
}

return true;
Expand All @@ -944,6 +982,14 @@ private boolean handleLongClick(BottomBarTab longClickedTab) {
private void updateSelectedTab(int newPosition) {
int newTabId = getTabAtPosition(newPosition).getId();


// check if viewpager is not null
if (viewPager != null && newPosition != currentTabPosition) // check if same tab is clicked
{

viewPager.setCurrentItem(newPosition); // set viewpager position to tab position
}

if (newPosition != currentTabPosition) {
if (onTabSelectListener != null) {
onTabSelectListener.onTabSelected(newTabId);
Expand Down Expand Up @@ -1054,24 +1100,26 @@ private void onEnd() {
private void backgroundCrossfadeAnimation(final int newColor) {
ViewCompat.setAlpha(backgroundOverlay, 0);
ViewCompat.animate(backgroundOverlay)
.alpha(1)
.setListener(new ViewPropertyAnimatorListenerAdapter() {
@Override
public void onAnimationEnd(View view) {
onEnd();
}

@Override
public void onAnimationCancel(View view) {
onEnd();
}

private void onEnd() {
outerContainer.setBackgroundColor(newColor);
backgroundOverlay.setVisibility(View.INVISIBLE);
ViewCompat.setAlpha(backgroundOverlay, 1);
}
})
.start();
.alpha(1)
.setListener(new ViewPropertyAnimatorListenerAdapter() {
@Override
public void onAnimationEnd(View view) {
onEnd();
}

@Override
public void onAnimationCancel(View view) {
onEnd();
}

private void onEnd() {
outerContainer.setBackgroundColor(newColor);
backgroundOverlay.setVisibility(View.INVISIBLE);
ViewCompat.setAlpha(backgroundOverlay, 1);
}
})
.start();
}


}