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

Implemented auto select on scroll feature. #198

Open
wants to merge 1 commit 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
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public int[] tabs() {
}
},

AUTOSELECT_ON_SCROLL(R.string.demo_title_autoselect_on_scroll, R.layout.demo_autoselect_on_scroll),

ALWAYS_IN_CENTER(R.string.demo_title_always_in_center, R.layout.demo_always_in_center),

CUSTOM_TAB(R.string.demo_title_custom_tab_text, R.layout.demo_custom_tab_text),
Expand Down
18 changes: 18 additions & 0 deletions demo/src/main/res/layout/demo_autoselect_on_scroll.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<com.ogaclejapan.smarttablayout.SmartTabLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@id/viewpagertab"
android:layout_width="match_parent"
android:layout_height="@dimen/tab_height"
android:background="@color/primary"
app:stl_defaultTabTextColor="@color/white"
app:stl_defaultTabTextHorizontalPadding="24dp"
app:stl_indicatorColor="@color/accent"
app:stl_indicatorInterpolation="linear"
app:stl_indicatorThickness="3dp"
app:stl_underlineThickness="1dp"
app:stl_autoSelectOnScroll="true"
app:stl_indicatorAlwaysInCenter="true"
/>


1 change: 1 addition & 0 deletions demo/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<string name="demo_title_basic2">Basic(Title Offset Auto Center)</string>
<string name="demo_title_smart_indicator">Smart Indicator</string>
<string name="demo_title_distribute_evenly">Distribute Evenly</string>
<string name="demo_title_autoselect_on_scroll">Auto Select On Scroll</string>
<string name="demo_title_always_in_center">Always In Center</string>
<string name="demo_title_custom_tab_text">Custom Tab Text</string>
<string name="demo_title_custom_tab_margin">Custom Tab Margin (since 1.2~)</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.SparseIntArray;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
Expand Down Expand Up @@ -85,6 +87,8 @@ public class SmartTabLayout extends HorizontalScrollView {
private InternalTabClickListener internalTabClickListener;
private OnTabClickListener onTabClickListener;
private boolean distributeEvenly;
private boolean autoSelectOnScroll;
private SparseIntArray offsetArray = new SparseIntArray();

public SmartTabLayout(Context context) {
this(context, null);
Expand Down Expand Up @@ -114,6 +118,7 @@ public SmartTabLayout(Context context, AttributeSet attrs, int defStyle) {
int customTabLayoutId = NO_ID;
int customTabTextViewId = NO_ID;
boolean clickable = TAB_CLICKABLE;
boolean autoSelectOnScroll = false;
int titleOffset = (int) (TITLE_OFFSET_DIPS * density);

TypedArray a = context.obtainStyledAttributes(
Expand All @@ -140,6 +145,8 @@ public SmartTabLayout(Context context, AttributeSet attrs, int defStyle) {
R.styleable.stl_SmartTabLayout_stl_clickable, clickable);
titleOffset = a.getLayoutDimension(
R.styleable.stl_SmartTabLayout_stl_titleOffset, titleOffset);
autoSelectOnScroll = a.getBoolean(
R.styleable.stl_SmartTabLayout_stl_autoSelectOnScroll, autoSelectOnScroll);
a.recycle();

this.titleOffset = titleOffset;
Expand All @@ -153,6 +160,7 @@ public SmartTabLayout(Context context, AttributeSet attrs, int defStyle) {
this.tabViewTextMinWidth = textMinWidth;
this.internalTabClickListener = clickable ? new InternalTabClickListener() : null;
this.distributeEvenly = distributeEvenly;
this.autoSelectOnScroll = autoSelectOnScroll;

if (customTabLayoutId != NO_ID) {
setCustomTabView(customTabLayoutId, customTabTextViewId);
Expand All @@ -165,13 +173,39 @@ public SmartTabLayout(Context context, AttributeSet attrs, int defStyle) {
"'distributeEvenly' and 'indicatorAlwaysInCenter' both use does not support");
}

if (autoSelectOnScroll && !tabStrip.isIndicatorAlwaysInCenter()) {
throw new UnsupportedOperationException(
"'autoSelectOnScroll' and 'indicatorAlwaysInCenter' both should be enabled");
}

// Make sure that the Tab Strips fills this View
setFillViewport(!tabStrip.isIndicatorAlwaysInCenter());

addView(tabStrip, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

}

@Override
public boolean onTouchEvent(MotionEvent ev) {
if (autoSelectOnScroll) {
if (ev.getAction() == MotionEvent.ACTION_UP || ev.getAction() == MotionEvent.ACTION_CANCEL) {
int scrollX = getScrollX();
int currPos = viewPager.getCurrentItem();
int featureWidth = getTabAt(currPos).getMeasuredWidth();
// Calculate the compensation to add/remove to the scroll for tabs with different widths
for (int i = 0; i < currPos; i++) {
scrollX += (featureWidth - getTabAt(i).getMeasuredWidth());
}
int activeTab = ((scrollX + (featureWidth / 2)) / featureWidth);
int scrollTo = offsetArray.get(activeTab);
smoothScrollTo(scrollTo, 0);
viewPager.setCurrentItem(activeTab);
return true;
}
}
return super.onTouchEvent(ev);
}

@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
Expand Down Expand Up @@ -201,6 +235,14 @@ protected void onLayout(boolean changed, int l, int t, int r, int b) {
if (changed && viewPager != null) {
scrollToTab(viewPager.getCurrentItem(), 0);
}
if (autoSelectOnScroll) {
int totalWidth = 0;
offsetArray.put(0, totalWidth);
for (int i = 1; i < tabStrip.getChildCount(); i++) {
totalWidth += getTabAt(i - 1).getMeasuredWidth();
offsetArray.put(i, totalWidth);
}
}
}

/**
Expand Down Expand Up @@ -299,7 +341,7 @@ public void setOnTabClickListener(OnTabClickListener listener) {
* Set the custom layout to be inflated for the tab views.
*
* @param layoutResId Layout id to be inflated
* @param textViewId id of the {@link android.widget.TextView} in the inflated view
* @param textViewId id of the {@link TextView} in the inflated view
*/
public void setCustomTabView(int layoutResId, int textViewId) {
tabProvider = new SimpleTabProvider(getContext(), layoutResId, textViewId);
Expand Down
1 change: 1 addition & 0 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,6 @@
<enum name="auto_center" value="-1"/>
</attr>
<attr name="stl_drawDecorationAfterTab" format="boolean"/>
<attr name="stl_autoSelectOnScroll" format="boolean"/>
</declare-styleable>
</resources>