Skip to content

Commit

Permalink
Update 1.0.3
Browse files Browse the repository at this point in the history
New badges.
Minor fixes and optimizations.
Deselect option.
  • Loading branch information
GIGAMOLE committed Jul 25, 2016
1 parent 1123e79 commit 16cc474
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 47 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[![Android Arsenal](https://img.shields.io/badge/Android%20Arsenal-NavigationTabStrip-blue.svg?style=flat-square)](http://android-arsenal.com/details/1/3603)
[![Download](https://api.bintray.com/packages/gigamole/maven/navigationtabstrip/images/download.svg) ](https://bintray.com/gigamole/maven/navigationtabstrip/_latestVersion)
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/d41363ef8b8542b5ad7c6a3b1a788e95)](https://www.codacy.com/app/gigamole53/NavigationTabStrip?utm_source=github.com&utm_medium=referral&utm_content=DevLight-Mobile-Agency/NavigationTabStrip&utm_campaign=Badge_Grade)

NavigationTabStrip
==================
Expand Down Expand Up @@ -32,7 +33,7 @@ dependencies {
Or Gradle Maven Central:

```groovy
compile 'com.github.devlight.navigationtabstrip:navigationtabstrip:1.0.2'
compile 'com.github.devlight.navigationtabstrip:navigationtabstrip:1.0.3'
```

Or Maven:
Expand All @@ -41,7 +42,7 @@ Or Maven:
<dependency>
<groupId>com.github.devlight.navigationtabstrip</groupId>
<artifactId>navigationtabstrip</artifactId>
<version>1.0.2</version>
<version>1.0.3</version>
<type>aar</type>
</dependency>
```
Expand Down
1 change: 0 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
xmlns:android="http://schemas.android.com/apk/res/android">

<application
android:allowBackup="true"
android:hardwareAccelerated="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ buildscript {
dependencies {
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'
classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:1.1"
classpath 'com.android.tools.build:gradle:2.1.0'
classpath 'com.android.tools.build:gradle:2.1.2'
}
}

Expand Down
4 changes: 2 additions & 2 deletions navigationtabstrip/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ apply plugin: "com.jfrog.bintray"
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'maven'

version = "1.0.2"
version = "1.0.3"

android {
compileSdkVersion 23
Expand All @@ -29,7 +29,7 @@ android {
minSdkVersion 11
targetSdkVersion 23
versionCode 1
versionName "1.0.2"
versionName "1.0.3"
}
buildTypes {
release {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
package com.gigamole.navigationtabstrip;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ArgbEvaluator;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.TypedArray;
Expand All @@ -30,8 +32,10 @@
import android.graphics.Typeface;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPager;
import android.text.TextPaint;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
Expand All @@ -47,9 +51,11 @@
/**
* Created by GIGAMOLE on 24.03.2016.
*/
@SuppressWarnings("unused")
public class NavigationTabStrip extends View implements ViewPager.OnPageChangeListener {

// NTS constants
private final static int HIGH_QUALITY_FLAGS = Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG;
private final static String PREVIEW_TITLE = "Title";
private final static int INVALID_INDEX = -1;

Expand All @@ -76,14 +82,14 @@ public class NavigationTabStrip extends View implements ViewPager.OnPageChangeLi
private final Rect mTitleBounds = new Rect();

// Main paint
private final Paint mStripPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG) {
private final Paint mStripPaint = new Paint(HIGH_QUALITY_FLAGS) {
{
setStyle(Style.FILL);
}
};

// Paint for tav title
private final Paint mTitlePaint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG) {
private final Paint mTitlePaint = new TextPaint(HIGH_QUALITY_FLAGS) {
{
setTextAlign(Align.CENTER);
}
Expand Down Expand Up @@ -162,8 +168,9 @@ public NavigationTabStrip(final Context context, final AttributeSet attrs, final

// Always draw
setWillNotDraw(false);
// More speed!
setLayerType(LAYER_TYPE_HARDWARE, null);
// Speed and fix for pre 17 API
ViewCompat.setLayerType(this, ViewCompat.LAYER_TYPE_SOFTWARE, null);
setLayerType(LAYER_TYPE_SOFTWARE, null);

final TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.NavigationTabStrip);
try {
Expand Down Expand Up @@ -297,6 +304,7 @@ private void setStripGravity(final int index) {
case StripGravity.BOTTOM_INDEX:
default:
setStripGravity(StripGravity.BOTTOM);
break;
}
}

Expand All @@ -317,6 +325,7 @@ private void setStripType(final int index) {
case StripType.LINE_INDEX:
default:
setStripType(StripType.LINE);
break;
}
}

Expand All @@ -338,6 +347,8 @@ public Typeface getTypeface() {
}

public void setTypeface(final String typeface) {
if (TextUtils.isEmpty(typeface)) return;

Typeface tempTypeface;
try {
tempTypeface = Typeface.createFromAsset(getContext().getAssets(), typeface);
Expand Down Expand Up @@ -401,7 +412,7 @@ public void setOnTabStripSelectedIndexListener(final OnTabStripSelectedIndexList
mOnTabStripSelectedIndexListener = onTabStripSelectedIndexListener;

if (mAnimatorListener == null)
mAnimatorListener = new Animator.AnimatorListener() {
mAnimatorListener = new AnimatorListenerAdapter() {
@Override
public void onAnimationStart(final Animator animation) {
if (mOnTabStripSelectedIndexListener != null)
Expand All @@ -421,16 +432,6 @@ public void onAnimationEnd(final Animator animation) {
if (mOnTabStripSelectedIndexListener != null)
mOnTabStripSelectedIndexListener.onEndTabSelected(mTitles[mIndex], mIndex);
}

@Override
public void onAnimationCancel(final Animator animation) {

}

@Override
public void onAnimationRepeat(final Animator animation) {

}
};
mAnimator.removeListener(mAnimatorListener);
mAnimator.addListener(mAnimatorListener);
Expand All @@ -443,8 +444,9 @@ public void setViewPager(final ViewPager viewPager) {
return;
}

if (mViewPager == viewPager) return;
if (mViewPager != null) mViewPager.setOnPageChangeListener(null);
if (viewPager.equals(mViewPager)) return;
if (mViewPager != null) //noinspection deprecation
mViewPager.setOnPageChangeListener(null);
if (viewPager.getAdapter() == null)
throw new IllegalStateException("ViewPager does not provide adapter instance.");

Expand Down Expand Up @@ -490,10 +492,13 @@ public void setTabIndex(int index) {
}

// Set tab index from touch or programmatically
public void setTabIndex(int index, boolean force) {
public void setTabIndex(int tabIndex, boolean isForce) {
if (mAnimator.isRunning()) return;
if (mTitles.length == 0) return;

int index = tabIndex;
boolean force = isForce;

// This check gives us opportunity to have an non selected tab
if (mIndex == INVALID_INDEX) force = true;

Expand All @@ -510,18 +515,36 @@ public void setTabIndex(int index, boolean force) {
mIsSetIndexFromTabBar = true;
if (mIsViewPagerMode) {
if (mViewPager == null) throw new IllegalStateException("ViewPager is null.");
mViewPager.setCurrentItem(index, true);
mViewPager.setCurrentItem(index, !force);
}

// Set startX and endX for animation, where we animate two sides of rect with different interpolation
mStartStripX = mStripLeft;
mEndStripX = (mIndex * mTabSize) + (mStripType == StripType.POINT ? mTabSize * 0.5F : 0.0F);

// If it force, so update immediately, else animate
/// If it force, so update immediately, else animate
// This happens if we set index onCreate or something like this
// You can use force param or call this method in some post()
if (force) updateIndicatorPosition(MAX_FRACTION);
else mAnimator.start();
if (force) {
updateIndicatorPosition(MAX_FRACTION);
// Force onPageScrolled listener and refresh VP
if (mIsViewPagerMode) {
if (!mViewPager.isFakeDragging()) mViewPager.beginFakeDrag();
if (mViewPager.isFakeDragging()) {
mViewPager.fakeDragBy(0.0F);
mViewPager.endFakeDrag();
}
}
} else mAnimator.start();
}

// Deselect active index and reset pointer
public void deselect() {
mLastIndex = INVALID_INDEX;
mIndex = INVALID_INDEX;
mStartStripX = INVALID_INDEX * mTabSize;
mEndStripX = mStartStripX;
updateIndicatorPosition(MIN_FRACTION);
}

private void updateIndicatorPosition(final float fraction) {
Expand All @@ -544,6 +567,7 @@ private void updateIndicatorPosition(final float fraction) {

// Update NTS
private void notifyDataSetChanged() {
requestLayout();
postInvalidate();
}

Expand Down Expand Up @@ -584,6 +608,7 @@ public boolean onTouchEvent(final MotionEvent event) {
return true;
}

@SuppressLint("DrawAllocation")
@Override
protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Expand Down Expand Up @@ -685,6 +710,9 @@ private void updateInactiveTitle() {

@Override
public void onPageScrolled(int position, float positionOffset, final int positionOffsetPixels) {
if (mOnPageChangeListener != null)
mOnPageChangeListener.onPageScrolled(position, positionOffset, positionOffsetPixels);

// If we animate, don`t call this
if (!mIsSetIndexFromTabBar) {
mIsResizeIn = position < mIndex;
Expand All @@ -697,35 +725,27 @@ public void onPageScrolled(int position, float positionOffset, final int positio
updateIndicatorPosition(positionOffset);
}

if (mOnPageChangeListener != null)
mOnPageChangeListener.onPageScrolled(position, positionOffset, positionOffsetPixels);
// Stop scrolling on animation end and reset values
if (!mAnimator.isRunning() && mIsSetIndexFromTabBar) {
mFraction = MIN_FRACTION;
mIsSetIndexFromTabBar = false;
}
}

@Override
public void onPageSelected(final int position) {
// If VP idle, so update
if (mScrollState == ViewPager.SCROLL_STATE_IDLE) {
mIsResizeIn = position < mIndex;
mLastIndex = mIndex;
mIndex = position;
postInvalidate();
}
// This method is empty, because we call onPageSelected() when scroll state is idle
}

@Override
public void onPageScrollStateChanged(final int state) {
// If VP idle, reset to MIN_FRACTION
mScrollState = state;
if (state == ViewPager.SCROLL_STATE_IDLE) {
mFraction = MIN_FRACTION;
mIsSetIndexFromTabBar = false;

if (mOnPageChangeListener != null) mOnPageChangeListener.onPageSelected(mIndex);
else {
if (mOnTabStripSelectedIndexListener != null)
mOnTabStripSelectedIndexListener.onEndTabSelected(mTitles[mIndex], mIndex);
}
if (mIsViewPagerMode && mOnTabStripSelectedIndexListener != null)
mOnTabStripSelectedIndexListener.onEndTabSelected(mTitles[mIndex], mIndex);
}
mScrollState = state;

if (mOnPageChangeListener != null) mOnPageChangeListener.onPageScrollStateChanged(state);
}
Expand All @@ -748,7 +768,8 @@ public Parcelable onSaveInstanceState() {

// Save current index instance
private static class SavedState extends BaseSavedState {
int index;

private int index;

public SavedState(Parcelable superState) {
super(superState);
Expand Down Expand Up @@ -787,7 +808,7 @@ protected void onConfigurationChanged(final Configuration newConfig) {

// Refresh strip and state after config changed to current
final int tempIndex = mIndex;
setTabIndex(INVALID_INDEX, true);
deselect();
post(new Runnable() {
@Override
public void run() {
Expand Down

0 comments on commit 16cc474

Please sign in to comment.