replace page indicator style, add data manager interface, etc.
This commit is contained in:
@@ -14,7 +14,7 @@ import com.alibaba.android.arouter.launcher.ARouter;
|
||||
import com.google.android.material.bottomsheet.BottomSheetBehavior;
|
||||
import com.mogo.commons.mvp.MvpFragment;
|
||||
import com.mogo.module.apps.model.AppInfo;
|
||||
import com.mogo.module.apps.view.LinePageIndicator;
|
||||
import com.mogo.module.apps.view.PagerSlidingTabStripV2;
|
||||
import com.mogo.service.MogoServicePaths;
|
||||
import com.mogo.service.fragmentmanager.IMogoFragmentManager;
|
||||
import com.mogo.utils.logger.Logger;
|
||||
@@ -40,7 +40,7 @@ public class AppsFragment extends MvpFragment< AppsView, AppsPresenter > impleme
|
||||
private IMogoFragmentManager mMogoFragmentManager;
|
||||
|
||||
private View mLoadingView;
|
||||
private LinePageIndicator mIndicator;
|
||||
private PagerSlidingTabStripV2 mIndicator;
|
||||
|
||||
@Override
|
||||
protected int getLayoutId() {
|
||||
@@ -77,6 +77,7 @@ public class AppsFragment extends MvpFragment< AppsView, AppsPresenter > impleme
|
||||
mLoadingView = findViewById( R.id.module_apps_id_loading );
|
||||
mLoadingView.setVisibility( View.VISIBLE );
|
||||
mIndicator = findViewById( R.id.module_apps_id_indicator );
|
||||
mIndicator.setOpenPadding( true );
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@@ -93,6 +94,7 @@ public class AppsFragment extends MvpFragment< AppsView, AppsPresenter > impleme
|
||||
|
||||
@Override
|
||||
public void renderApps( Map< Integer, List< AppInfo > > appInfos ) {
|
||||
mAppsPager.setOffscreenPageLimit( appInfos.size() );
|
||||
mLoadingView.setVisibility( View.GONE );
|
||||
if ( mAppsPagerAdapter == null ) {
|
||||
mAppsPagerAdapter = new AppsPagerAdapter( appInfos );
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.mogo.module.apps;
|
||||
|
||||
import android.content.Context;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
@@ -8,8 +9,12 @@ import android.widget.GridView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.viewpager.widget.PagerAdapter;
|
||||
import androidx.viewpager.widget.ViewPager;
|
||||
|
||||
import com.mogo.module.apps.model.AppInfo;
|
||||
import com.mogo.module.apps.view.PagerIndicator;
|
||||
import com.mogo.module.apps.view.PagerSlidingTabStripV2;
|
||||
import com.mogo.utils.ResourcesHelper;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -20,7 +25,7 @@ import java.util.Map;
|
||||
* <p>
|
||||
* 描述
|
||||
*/
|
||||
public class AppsPagerAdapter extends PagerAdapter {
|
||||
public class AppsPagerAdapter extends PagerAdapter implements PagerSlidingTabStripV2.ViewTabProvider {
|
||||
|
||||
private Map< Integer, List< AppInfo > > mPagedApps;
|
||||
private OnAppClickedListener mOnAppClickedListener;
|
||||
@@ -82,4 +87,9 @@ public class AppsPagerAdapter extends PagerAdapter {
|
||||
public interface OnAppClickedListener {
|
||||
void onClick( AppInfo appInfo, int position );
|
||||
}
|
||||
|
||||
@Override
|
||||
public View getPageTabView( Context context, int position ) {
|
||||
return new PagerIndicator( context );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,455 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2012 Jake Wharton
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.mogo.module.apps.view;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.Resources;
|
||||
import android.content.res.TypedArray;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Paint;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
import android.view.ViewConfiguration;
|
||||
import androidx.core.view.MotionEventCompat;
|
||||
import androidx.core.view.ViewConfigurationCompat;
|
||||
import androidx.viewpager.widget.ViewPager;
|
||||
|
||||
import com.mogo.module.apps.R;
|
||||
|
||||
/**
|
||||
* Draws a line for each page. The current page line is colored differently
|
||||
* than the unselected page lines.
|
||||
*/
|
||||
public class LinePageIndicator extends View implements PageIndicator {
|
||||
private static final int INVALID_POINTER = -1;
|
||||
|
||||
private final Paint mPaintUnselected = new Paint(Paint.ANTI_ALIAS_FLAG);
|
||||
private final Paint mPaintSelected = new Paint(Paint.ANTI_ALIAS_FLAG);
|
||||
private ViewPager mViewPager;
|
||||
private ViewPager.OnPageChangeListener mListener;
|
||||
private int mCurrentPage;
|
||||
private boolean mCentered;
|
||||
private float mLineWidth;
|
||||
private float mGapWidth;
|
||||
|
||||
private int mTouchSlop;
|
||||
private float mLastMotionX = -1;
|
||||
private int mActivePointerId = INVALID_POINTER;
|
||||
private boolean mIsDragging;
|
||||
|
||||
|
||||
public LinePageIndicator(Context context) {
|
||||
this(context, null);
|
||||
}
|
||||
|
||||
public LinePageIndicator(Context context, AttributeSet attrs) {
|
||||
this(context, attrs, R.attr.vpiLinePageIndicatorStyle);
|
||||
}
|
||||
|
||||
public LinePageIndicator(Context context, AttributeSet attrs, int defStyle) {
|
||||
super(context, attrs, defStyle);
|
||||
if (isInEditMode()) {
|
||||
return;
|
||||
}
|
||||
|
||||
final Resources res = getResources();
|
||||
|
||||
//Load defaults from resources
|
||||
final int defaultSelectedColor = res.getColor(R.color.default_line_indicator_selected_color);
|
||||
final int defaultUnselectedColor = res.getColor(R.color.default_line_indicator_unselected_color);
|
||||
final float defaultLineWidth = res.getDimension(R.dimen.default_line_indicator_line_width);
|
||||
final float defaultGapWidth = res.getDimension(R.dimen.default_line_indicator_gap_width);
|
||||
final float defaultStrokeWidth = res.getDimension(R.dimen.default_line_indicator_stroke_width);
|
||||
final boolean defaultCentered = res.getBoolean(R.bool.default_line_indicator_centered);
|
||||
|
||||
//Retrieve styles attributes
|
||||
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.LinePageIndicator, defStyle, 0);
|
||||
|
||||
mCentered = a.getBoolean(R.styleable.LinePageIndicator_centered, defaultCentered);
|
||||
mLineWidth = a.getDimension(R.styleable.LinePageIndicator_lineWidth, defaultLineWidth);
|
||||
mGapWidth = a.getDimension(R.styleable.LinePageIndicator_gapWidth, defaultGapWidth);
|
||||
setStrokeWidth(a.getDimension(R.styleable.LinePageIndicator_strokeWidth, defaultStrokeWidth));
|
||||
mPaintUnselected.setColor(a.getColor(R.styleable.LinePageIndicator_unselectedColor, defaultUnselectedColor));
|
||||
mPaintSelected.setColor(a.getColor(R.styleable.LinePageIndicator_selectedColor, defaultSelectedColor));
|
||||
|
||||
Drawable background = a.getDrawable(R.styleable.LinePageIndicator_android_background);
|
||||
if (background != null) {
|
||||
setBackgroundDrawable(background);
|
||||
}
|
||||
|
||||
a.recycle();
|
||||
|
||||
final ViewConfiguration configuration = ViewConfiguration.get(context);
|
||||
mTouchSlop = ViewConfigurationCompat.getScaledPagingTouchSlop(configuration);
|
||||
}
|
||||
|
||||
|
||||
public void setCentered(boolean centered) {
|
||||
mCentered = centered;
|
||||
invalidate();
|
||||
}
|
||||
|
||||
public boolean isCentered() {
|
||||
return mCentered;
|
||||
}
|
||||
|
||||
public void setUnselectedColor(int unselectedColor) {
|
||||
mPaintUnselected.setColor(unselectedColor);
|
||||
invalidate();
|
||||
}
|
||||
|
||||
public int getUnselectedColor() {
|
||||
return mPaintUnselected.getColor();
|
||||
}
|
||||
|
||||
public void setSelectedColor(int selectedColor) {
|
||||
mPaintSelected.setColor(selectedColor);
|
||||
invalidate();
|
||||
}
|
||||
|
||||
public int getSelectedColor() {
|
||||
return mPaintSelected.getColor();
|
||||
}
|
||||
|
||||
public void setLineWidth(float lineWidth) {
|
||||
mLineWidth = lineWidth;
|
||||
invalidate();
|
||||
}
|
||||
|
||||
public float getLineWidth() {
|
||||
return mLineWidth;
|
||||
}
|
||||
|
||||
public void setStrokeWidth(float lineHeight) {
|
||||
mPaintSelected.setStrokeWidth(lineHeight);
|
||||
mPaintUnselected.setStrokeWidth(lineHeight);
|
||||
invalidate();
|
||||
}
|
||||
|
||||
public float getStrokeWidth() {
|
||||
return mPaintSelected.getStrokeWidth();
|
||||
}
|
||||
|
||||
public void setGapWidth(float gapWidth) {
|
||||
mGapWidth = gapWidth;
|
||||
invalidate();
|
||||
}
|
||||
|
||||
public float getGapWidth() {
|
||||
return mGapWidth;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDraw(Canvas canvas) {
|
||||
super.onDraw(canvas);
|
||||
|
||||
if (mViewPager == null) {
|
||||
return;
|
||||
}
|
||||
final int count = mViewPager.getAdapter().getCount();
|
||||
if (count == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (mCurrentPage >= count) {
|
||||
setCurrentItem(count - 1);
|
||||
return;
|
||||
}
|
||||
|
||||
final float lineWidthAndGap = mLineWidth + mGapWidth;
|
||||
final float indicatorWidth = (count * lineWidthAndGap) - mGapWidth;
|
||||
final float paddingTop = getPaddingTop();
|
||||
final float paddingLeft = getPaddingLeft();
|
||||
final float paddingRight = getPaddingRight();
|
||||
|
||||
float verticalOffset = paddingTop + ((getHeight() - paddingTop - getPaddingBottom()) / 2.0f);
|
||||
float horizontalOffset = paddingLeft;
|
||||
if (mCentered) {
|
||||
horizontalOffset += ((getWidth() - paddingLeft - paddingRight) / 2.0f) - (indicatorWidth / 2.0f);
|
||||
}
|
||||
|
||||
//Draw stroked circles
|
||||
for (int i = 0; i < count; i++) {
|
||||
float dx1 = horizontalOffset + (i * lineWidthAndGap);
|
||||
float dx2 = dx1 + mLineWidth;
|
||||
canvas.drawLine(dx1, verticalOffset, dx2, verticalOffset, (i == mCurrentPage) ? mPaintSelected : mPaintUnselected);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean onTouchEvent(MotionEvent ev) {
|
||||
if (super.onTouchEvent(ev)) {
|
||||
return true;
|
||||
}
|
||||
if ((mViewPager == null) || (mViewPager.getAdapter().getCount() == 0)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;
|
||||
switch (action) {
|
||||
case MotionEvent.ACTION_DOWN:
|
||||
mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
|
||||
mLastMotionX = ev.getX();
|
||||
break;
|
||||
|
||||
case MotionEvent.ACTION_MOVE: {
|
||||
final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
|
||||
final float x = MotionEventCompat.getX(ev, activePointerIndex);
|
||||
final float deltaX = x - mLastMotionX;
|
||||
|
||||
if (!mIsDragging) {
|
||||
if (Math.abs(deltaX) > mTouchSlop) {
|
||||
mIsDragging = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (mIsDragging) {
|
||||
mLastMotionX = x;
|
||||
if (mViewPager.isFakeDragging() || mViewPager.beginFakeDrag()) {
|
||||
mViewPager.fakeDragBy(deltaX);
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case MotionEvent.ACTION_CANCEL:
|
||||
case MotionEvent.ACTION_UP:
|
||||
if (!mIsDragging) {
|
||||
final int count = mViewPager.getAdapter().getCount();
|
||||
final int width = getWidth();
|
||||
final float halfWidth = width / 2f;
|
||||
final float sixthWidth = width / 6f;
|
||||
|
||||
if ((mCurrentPage > 0) && (ev.getX() < halfWidth - sixthWidth)) {
|
||||
if (action != MotionEvent.ACTION_CANCEL) {
|
||||
mViewPager.setCurrentItem(mCurrentPage - 1);
|
||||
}
|
||||
return true;
|
||||
} else if ((mCurrentPage < count - 1) && (ev.getX() > halfWidth + sixthWidth)) {
|
||||
if (action != MotionEvent.ACTION_CANCEL) {
|
||||
mViewPager.setCurrentItem(mCurrentPage + 1);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
mIsDragging = false;
|
||||
mActivePointerId = INVALID_POINTER;
|
||||
if (mViewPager.isFakeDragging()) {
|
||||
mViewPager.endFakeDrag();
|
||||
}
|
||||
break;
|
||||
|
||||
case MotionEventCompat.ACTION_POINTER_DOWN: {
|
||||
final int index = MotionEventCompat.getActionIndex(ev);
|
||||
mLastMotionX = MotionEventCompat.getX(ev, index);
|
||||
mActivePointerId = MotionEventCompat.getPointerId(ev, index);
|
||||
break;
|
||||
}
|
||||
|
||||
case MotionEventCompat.ACTION_POINTER_UP:
|
||||
final int pointerIndex = MotionEventCompat.getActionIndex(ev);
|
||||
final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);
|
||||
if (pointerId == mActivePointerId) {
|
||||
final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
|
||||
mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
|
||||
}
|
||||
mLastMotionX = MotionEventCompat.getX(ev, MotionEventCompat.findPointerIndex(ev, mActivePointerId));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setViewPager(ViewPager viewPager) {
|
||||
if (mViewPager == viewPager) {
|
||||
return;
|
||||
}
|
||||
if (mViewPager != null) {
|
||||
//Clear us from the old pager.
|
||||
// mViewPager.setOnPageChangeListener(null);
|
||||
}
|
||||
if (viewPager.getAdapter() == null) {
|
||||
throw new IllegalStateException("ViewPager does not have adapter instance.");
|
||||
}
|
||||
mViewPager = viewPager;
|
||||
mViewPager.setOnPageChangeListener(this);
|
||||
invalidate();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setViewPager(ViewPager view, int initialPosition) {
|
||||
setViewPager(view);
|
||||
setCurrentItem(initialPosition);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCurrentItem(int item) {
|
||||
if (mViewPager == null) {
|
||||
throw new IllegalStateException("ViewPager has not been bound.");
|
||||
}
|
||||
mViewPager.setCurrentItem(item);
|
||||
mCurrentPage = item;
|
||||
invalidate();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void notifyDataSetChanged() {
|
||||
invalidate();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPageScrollStateChanged(int state) {
|
||||
if (mListener != null) {
|
||||
mListener.onPageScrollStateChanged(state);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
|
||||
if (mListener != null) {
|
||||
mListener.onPageScrolled(position, positionOffset, positionOffsetPixels);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPageSelected(int position) {
|
||||
mCurrentPage = position;
|
||||
invalidate();
|
||||
|
||||
if (mListener != null) {
|
||||
mListener.onPageSelected(position);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setOnPageChangeListener(ViewPager.OnPageChangeListener listener) {
|
||||
mListener = listener;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
||||
setMeasuredDimension(measureWidth(widthMeasureSpec), measureHeight(heightMeasureSpec));
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines the width of this view
|
||||
*
|
||||
* @param measureSpec
|
||||
* A measureSpec packed into an int
|
||||
* @return The width of the view, honoring constraints from measureSpec
|
||||
*/
|
||||
private int measureWidth(int measureSpec) {
|
||||
float result;
|
||||
int specMode = MeasureSpec.getMode(measureSpec);
|
||||
int specSize = MeasureSpec.getSize(measureSpec);
|
||||
|
||||
if ((specMode == MeasureSpec.EXACTLY) || (mViewPager == null)) {
|
||||
//We were told how big to be
|
||||
result = specSize;
|
||||
} else {
|
||||
//Calculate the width according the views count
|
||||
final int count = mViewPager.getAdapter().getCount();
|
||||
result = getPaddingLeft() + getPaddingRight() + (count * mLineWidth) + ((count - 1) * mGapWidth);
|
||||
//Respect AT_MOST value if that was what is called for by measureSpec
|
||||
if (specMode == MeasureSpec.AT_MOST) {
|
||||
result = Math.min(result, specSize);
|
||||
}
|
||||
}
|
||||
return (int)Math.ceil(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines the height of this view
|
||||
*
|
||||
* @param measureSpec
|
||||
* A measureSpec packed into an int
|
||||
* @return The height of the view, honoring constraints from measureSpec
|
||||
*/
|
||||
private int measureHeight(int measureSpec) {
|
||||
float result;
|
||||
int specMode = MeasureSpec.getMode(measureSpec);
|
||||
int specSize = MeasureSpec.getSize(measureSpec);
|
||||
|
||||
if (specMode == MeasureSpec.EXACTLY) {
|
||||
//We were told how big to be
|
||||
result = specSize;
|
||||
} else {
|
||||
//Measure the height
|
||||
result = mPaintSelected.getStrokeWidth() + getPaddingTop() + getPaddingBottom();
|
||||
//Respect AT_MOST value if that was what is called for by measureSpec
|
||||
if (specMode == MeasureSpec.AT_MOST) {
|
||||
result = Math.min(result, specSize);
|
||||
}
|
||||
}
|
||||
return (int)Math.ceil(result);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRestoreInstanceState(Parcelable state) {
|
||||
SavedState savedState = (SavedState)state;
|
||||
super.onRestoreInstanceState(savedState.getSuperState());
|
||||
mCurrentPage = savedState.currentPage;
|
||||
requestLayout();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Parcelable onSaveInstanceState() {
|
||||
Parcelable superState = super.onSaveInstanceState();
|
||||
SavedState savedState = new SavedState(superState);
|
||||
savedState.currentPage = mCurrentPage;
|
||||
return savedState;
|
||||
}
|
||||
|
||||
static class SavedState extends BaseSavedState {
|
||||
int currentPage;
|
||||
|
||||
public SavedState(Parcelable superState) {
|
||||
super(superState);
|
||||
}
|
||||
|
||||
private SavedState(Parcel in) {
|
||||
super(in);
|
||||
currentPage = in.readInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToParcel(Parcel dest, int flags) {
|
||||
super.writeToParcel(dest, flags);
|
||||
dest.writeInt(currentPage);
|
||||
}
|
||||
|
||||
@SuppressWarnings("UnusedDeclaration")
|
||||
public static final Creator<SavedState> CREATOR = new Creator<SavedState>() {
|
||||
@Override
|
||||
public SavedState createFromParcel(Parcel in) {
|
||||
return new SavedState(in);
|
||||
}
|
||||
|
||||
@Override
|
||||
public SavedState[] newArray(int size) {
|
||||
return new SavedState[size];
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,64 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2011 Patrik Akerfeldt
|
||||
* Copyright (C) 2011 Jake Wharton
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.mogo.module.apps.view;
|
||||
|
||||
|
||||
import androidx.viewpager.widget.ViewPager;
|
||||
|
||||
/**
|
||||
* A PageIndicator is responsible to show an visual indicator on the total views
|
||||
* number and the current visible view.
|
||||
*/
|
||||
public interface PageIndicator extends ViewPager.OnPageChangeListener {
|
||||
/**
|
||||
* Bind the indicator to a ViewPager.
|
||||
*
|
||||
* @param view
|
||||
*/
|
||||
void setViewPager( ViewPager view );
|
||||
|
||||
/**
|
||||
* Bind the indicator to a ViewPager.
|
||||
*
|
||||
* @param view
|
||||
* @param initialPosition
|
||||
*/
|
||||
void setViewPager( ViewPager view, int initialPosition );
|
||||
|
||||
/**
|
||||
* <p>Set the current page of both the ViewPager and indicator.</p>
|
||||
*
|
||||
* <p>This <strong>must</strong> be used if you need to set the page before
|
||||
* the views are drawn on screen (e.g., default start page).</p>
|
||||
*
|
||||
* @param item
|
||||
*/
|
||||
void setCurrentItem( int item );
|
||||
|
||||
/**
|
||||
* Set a page change listener which will receive forwarded events.
|
||||
*
|
||||
* @param listener
|
||||
*/
|
||||
void setOnPageChangeListener( ViewPager.OnPageChangeListener listener );
|
||||
|
||||
/**
|
||||
* Notify the indicator that the fragment list has changed.
|
||||
*/
|
||||
void notifyDataSetChanged();
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.mogo.module.apps.view;
|
||||
|
||||
import android.content.Context;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.widget.LinearLayout;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import com.mogo.module.apps.R;
|
||||
|
||||
/**
|
||||
* @author congtaowang
|
||||
* @since 2020-02-11
|
||||
* <p>
|
||||
* 描述
|
||||
*/
|
||||
public class PagerIndicator extends LinearLayout implements PagerSlidingTabStripV2.SelectedState {
|
||||
|
||||
private View mIndicator;
|
||||
|
||||
public PagerIndicator( Context context ) {
|
||||
this( context, null );
|
||||
}
|
||||
|
||||
public PagerIndicator( Context context, @Nullable AttributeSet attrs ) {
|
||||
this( context, attrs, 0 );
|
||||
}
|
||||
|
||||
public PagerIndicator( Context context, @Nullable AttributeSet attrs, int defStyleAttr ) {
|
||||
super( context, attrs, defStyleAttr );
|
||||
LayoutInflater.from( context ).inflate( R.layout.modle_apps_page_indicator, this, true );
|
||||
mIndicator = findViewById( R.id.module_apps_id_indicator_dot );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSelectedState( boolean isSelected ) {
|
||||
mIndicator.setSelected( isSelected );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,763 @@
|
||||
/*
|
||||
* Copyright (C) 2013 Andreas Stuetz <andreas.stuetz@gmail.com>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.mogo.module.apps.view;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.TypedArray;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Paint;
|
||||
import android.graphics.Paint.Style;
|
||||
import android.graphics.Typeface;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.Build;
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
import android.text.TextPaint;
|
||||
import android.text.TextUtils;
|
||||
import android.util.AttributeSet;
|
||||
import android.util.DisplayMetrics;
|
||||
import android.util.TypedValue;
|
||||
import android.view.Gravity;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
|
||||
import android.widget.HorizontalScrollView;
|
||||
import android.widget.ImageButton;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.viewpager.widget.ViewPager;
|
||||
|
||||
|
||||
import com.mogo.module.apps.R;
|
||||
import com.mogo.utils.WindowUtils;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* Reference : http://doc.okbase.net/HarryWeasley/archive/121430.html
|
||||
*/
|
||||
public class PagerSlidingTabStripV2 extends HorizontalScrollView {
|
||||
|
||||
public interface IconTabProvider {
|
||||
public int getPageIconResId( int position );
|
||||
}
|
||||
|
||||
public interface ViewTabProvider {
|
||||
public View getPageTabView( Context context, int position );
|
||||
}
|
||||
|
||||
public interface SelectedState {
|
||||
void setSelectedState( boolean isSelected );
|
||||
}
|
||||
|
||||
// @formatter:off
|
||||
private static final int[] ATTRS = new int[]{
|
||||
android.R.attr.textSize,
|
||||
android.R.attr.textColor
|
||||
};
|
||||
// @formatter:on
|
||||
|
||||
public interface OnBeforeTabAction {
|
||||
boolean doAction( int position );
|
||||
}
|
||||
|
||||
private LinearLayout.LayoutParams defaultTabLayoutParams;
|
||||
private LinearLayout.LayoutParams expandedTabLayoutParams;
|
||||
|
||||
private final PageListener pageListener = new PageListener();
|
||||
public ViewPager.OnPageChangeListener delegatePageListener;
|
||||
|
||||
private LinearLayout tabsContainer;
|
||||
private ViewPager pager;
|
||||
|
||||
private int tabCount;
|
||||
|
||||
private int currentPosition = 0;
|
||||
private int selectedPosition = 0;
|
||||
private float currentPositionOffset = 0f;
|
||||
|
||||
private Paint rectPaint;
|
||||
private Paint dividerPaint;
|
||||
|
||||
private int indicatorColor = 0xFF666666;
|
||||
private int underlineColor = 0x1A000000;
|
||||
private int dividerColor = 0x1A000000;
|
||||
|
||||
private boolean shouldExpand = false;
|
||||
private boolean textAllCaps = true;
|
||||
|
||||
private int scrollOffset = 52;
|
||||
private int indicatorHeight = 8;
|
||||
private int underlineHeight = 2;
|
||||
private int dividerPadding = 12;
|
||||
private int tabPadding = 24;
|
||||
private int dividerWidth = 1;
|
||||
private int indicatorMarginBottom = 0;
|
||||
private int indicatorMarginLeft = 0;
|
||||
private int indicatorMarginRight = 0;
|
||||
|
||||
private int tabTextSize = 13;
|
||||
private int tabTextColor = 0xFF666666;
|
||||
private int selectedTabTextColor = 0xFF666666;
|
||||
|
||||
private Typeface tabTypeface = null;
|
||||
private int tabTypefaceStyle = Typeface.NORMAL;
|
||||
|
||||
private int lastScrollX = 0;
|
||||
|
||||
private int tabBackgroundResId = R.drawable.module_apps_pager_sliding_background_tab;
|
||||
|
||||
public static final int INDICATOR_MODE_UNDERLINE = -1;
|
||||
public static final int INDICATOR_MODE_SHADOW = 1;
|
||||
public static final int INDICATOR_MODE_RES = 2;
|
||||
private int indicatorMode = INDICATOR_MODE_SHADOW;
|
||||
|
||||
private Drawable indicatorRes;
|
||||
|
||||
public static final int INDICATOR_FIT_MODE_AUTO = 0;
|
||||
public static final int INDICATOR_FIT_MODE_FIXED = 1;
|
||||
private int indicatorFitMode = INDICATOR_FIT_MODE_AUTO;
|
||||
|
||||
private float indicatorFixedSize = 0;
|
||||
|
||||
private Locale locale;
|
||||
|
||||
private OnBeforeTabAction onBeforeTabAction;
|
||||
private boolean openPadding = false;
|
||||
|
||||
|
||||
public boolean isOpenPadding() {
|
||||
return openPadding;
|
||||
}
|
||||
|
||||
public void setOpenPadding( boolean openPadding ) {
|
||||
this.openPadding = openPadding;
|
||||
}
|
||||
|
||||
public PagerSlidingTabStripV2( Context context ) {
|
||||
this( context, null );
|
||||
}
|
||||
|
||||
public PagerSlidingTabStripV2( Context context, AttributeSet attrs ) {
|
||||
this( context, attrs, 0 );
|
||||
}
|
||||
|
||||
public PagerSlidingTabStripV2( Context context, AttributeSet attrs, int defStyle ) {
|
||||
super( context, attrs, defStyle );
|
||||
|
||||
setFillViewport( true );
|
||||
setWillNotDraw( false );
|
||||
|
||||
tabsContainer = new LinearLayout( context );
|
||||
tabsContainer.setOrientation( LinearLayout.HORIZONTAL );
|
||||
tabsContainer.setLayoutParams( new LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT ) );
|
||||
addView( tabsContainer );
|
||||
|
||||
DisplayMetrics dm = getResources().getDisplayMetrics();
|
||||
|
||||
scrollOffset = ( int ) TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_DIP, scrollOffset, dm );
|
||||
indicatorHeight = ( int ) TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_DIP, indicatorHeight, dm );
|
||||
underlineHeight = ( int ) TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_DIP, underlineHeight, dm );
|
||||
dividerPadding = ( int ) TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_DIP, dividerPadding, dm );
|
||||
tabPadding = ( int ) TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_DIP, tabPadding, dm );
|
||||
dividerWidth = ( int ) TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_DIP, dividerWidth, dm );
|
||||
tabTextSize = ( int ) TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_SP, tabTextSize, dm );
|
||||
indicatorMarginBottom = ( int ) TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_DIP, indicatorMarginBottom, dm );
|
||||
indicatorMarginLeft = ( int ) TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_DIP, indicatorMarginLeft, dm );
|
||||
indicatorMarginRight = ( int ) TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_DIP, indicatorMarginRight, dm );
|
||||
|
||||
// get system attrs (android:textSize and android:textColor)
|
||||
|
||||
TypedArray a = context.obtainStyledAttributes( attrs, ATTRS );
|
||||
|
||||
tabTextSize = a.getDimensionPixelSize( 0, tabTextSize );
|
||||
tabTextColor = a.getColor( 1, tabTextColor );
|
||||
|
||||
a.recycle();
|
||||
|
||||
// get custom attrs
|
||||
|
||||
a = context.obtainStyledAttributes( attrs, R.styleable.PagerSlidingTabStripV2 );
|
||||
|
||||
indicatorColor = a.getColor( R.styleable.PagerSlidingTabStripV2_pstsV2IndicatorColor, indicatorColor );
|
||||
//tab文字选中时的颜色,默认和滑动指示器的颜色一致
|
||||
selectedTabTextColor = a.getColor( R.styleable.PagerSlidingTabStripV2_pstsV2SelectedTabTextColor, indicatorColor );
|
||||
tabTextColor = a.getColor( R.styleable.PagerSlidingTabStripV2_pstsV2TabTextColorValue, tabTextColor );
|
||||
underlineColor = a.getColor( R.styleable.PagerSlidingTabStripV2_pstsV2UnderlineColor, underlineColor );
|
||||
dividerColor = a.getColor( R.styleable.PagerSlidingTabStripV2_pstsV2DividerColor, dividerColor );
|
||||
indicatorHeight = a.getDimensionPixelSize( R.styleable.PagerSlidingTabStripV2_pstsV2IndicatorHeight, indicatorHeight );
|
||||
underlineHeight = a.getDimensionPixelSize( R.styleable.PagerSlidingTabStripV2_pstsV2UnderlineHeight, underlineHeight );
|
||||
dividerPadding = a.getDimensionPixelSize( R.styleable.PagerSlidingTabStripV2_pstsV2DividerPadding, dividerPadding );
|
||||
tabPadding = a.getDimensionPixelSize( R.styleable.PagerSlidingTabStripV2_pstsV2TabPaddingLeftRight, tabPadding );
|
||||
tabBackgroundResId = a.getResourceId( R.styleable.PagerSlidingTabStripV2_pstsV2TabBackground, tabBackgroundResId );
|
||||
shouldExpand = a.getBoolean( R.styleable.PagerSlidingTabStripV2_pstsV2ShouldExpand, shouldExpand );
|
||||
scrollOffset = a.getDimensionPixelSize( R.styleable.PagerSlidingTabStripV2_pstsV2ScrollOffset, scrollOffset );
|
||||
textAllCaps = a.getBoolean( R.styleable.PagerSlidingTabStripV2_pstsV2TextAllCaps, textAllCaps );
|
||||
indicatorMarginBottom = a.getDimensionPixelSize( R.styleable.PagerSlidingTabStripV2_pstsV2IndicatorMarginBottom, indicatorMarginBottom );
|
||||
indicatorMarginLeft = a.getDimensionPixelSize( R.styleable.PagerSlidingTabStripV2_pstsV2IndicatorMarginLeft, indicatorMarginLeft );
|
||||
indicatorMarginRight = a.getDimensionPixelSize( R.styleable.PagerSlidingTabStripV2_pstsV2IndicatorMarginRight, indicatorMarginRight );
|
||||
indicatorMode = a.getInt( R.styleable.PagerSlidingTabStripV2_pstsV2IndicatorMode, INDICATOR_MODE_SHADOW );
|
||||
indicatorRes = a.getDrawable( R.styleable.PagerSlidingTabStripV2_pstsV2IndicatorRes );
|
||||
a.recycle();
|
||||
|
||||
rectPaint = new Paint();
|
||||
rectPaint.setAntiAlias( true );
|
||||
rectPaint.setStyle( Style.FILL );
|
||||
|
||||
dividerPaint = new Paint();
|
||||
dividerPaint.setAntiAlias( true );
|
||||
dividerPaint.setStrokeWidth( dividerWidth );
|
||||
|
||||
defaultTabLayoutParams = new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT );
|
||||
expandedTabLayoutParams = new LinearLayout.LayoutParams( 0, LayoutParams.MATCH_PARENT, 1.0f );
|
||||
|
||||
if ( locale == null ) {
|
||||
locale = getResources().getConfiguration().locale;
|
||||
}
|
||||
}
|
||||
|
||||
public OnBeforeTabAction getOnBeforeTabAction() {
|
||||
return onBeforeTabAction;
|
||||
}
|
||||
|
||||
public void setOnBeforeTabAction( OnBeforeTabAction onBeforeTabAction ) {
|
||||
this.onBeforeTabAction = onBeforeTabAction;
|
||||
}
|
||||
|
||||
public void setViewPager( ViewPager pager ) {
|
||||
this.pager = pager;
|
||||
this.currentPosition = this.selectedPosition = pager.getCurrentItem();
|
||||
|
||||
if ( pager.getAdapter() == null ) {
|
||||
throw new IllegalStateException( "ViewPager does not have adapter instance." );
|
||||
}
|
||||
|
||||
pager.removeOnPageChangeListener( pageListener );
|
||||
pager.addOnPageChangeListener( pageListener );
|
||||
|
||||
notifyDataSetChanged();
|
||||
}
|
||||
|
||||
public void setOnPageChangeListener( ViewPager.OnPageChangeListener listener ) {
|
||||
this.delegatePageListener = listener;
|
||||
}
|
||||
|
||||
public void notifyDataSetChanged() {
|
||||
|
||||
indicatorLineOffset = -1;
|
||||
tabsContainer.removeAllViews();
|
||||
|
||||
tabCount = pager.getAdapter().getCount();
|
||||
|
||||
for ( int i = 0; i < tabCount; i++ ) {
|
||||
|
||||
if ( pager.getAdapter() instanceof ViewTabProvider ) {
|
||||
addViewTab( i, ( ( ViewTabProvider ) pager.getAdapter() ).getPageTabView( getContext(), i ) );
|
||||
} else {
|
||||
addTextTab( i, TextUtils.isEmpty( pager.getAdapter().getPageTitle( i ) ) ? "" : pager.getAdapter().getPageTitle( i ).toString() );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
updateTabStyles();
|
||||
|
||||
getViewTreeObserver().addOnGlobalLayoutListener( new OnGlobalLayoutListener() {
|
||||
|
||||
@Override
|
||||
public void onGlobalLayout() {
|
||||
getViewTreeObserver().removeGlobalOnLayoutListener( this );
|
||||
currentPosition = pager.getCurrentItem();
|
||||
scrollToChild( currentPosition, 0 );
|
||||
}
|
||||
} );
|
||||
|
||||
}
|
||||
|
||||
private void addTextTab( final int position, String title ) {
|
||||
|
||||
TextView tab = new TextView( getContext() );
|
||||
tab.setText( title );
|
||||
tab.setGravity( Gravity.CENTER );
|
||||
tab.setSingleLine();
|
||||
tab.setPadding( WindowUtils.dip2px( getContext(), 10 ),
|
||||
WindowUtils.dip2px( getContext(), 3 ),
|
||||
WindowUtils.dip2px( getContext(), 10 ),
|
||||
WindowUtils.dip2px( getContext(), 3 ) );
|
||||
|
||||
LinearLayout linearLayout = new LinearLayout( getContext() );
|
||||
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT );
|
||||
linearLayout.addView( tab, layoutParams );
|
||||
linearLayout.setGravity( Gravity.CENTER );
|
||||
addTab( position, linearLayout );
|
||||
}
|
||||
|
||||
private void addIconTab( final int position, int resId ) {
|
||||
|
||||
ImageButton tab = new ImageButton( getContext() );
|
||||
tab.setImageResource( resId );
|
||||
|
||||
addTab( position, tab );
|
||||
|
||||
}
|
||||
|
||||
private void addViewTab( final int position, View v ) {
|
||||
addTab( position, v );
|
||||
}
|
||||
|
||||
public void setCurrentTab( final int position ) {
|
||||
pager.post( new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
pager.setCurrentItem( position, false );
|
||||
}
|
||||
} );
|
||||
}
|
||||
|
||||
private void addTab( final int position, View tab ) {
|
||||
tab.setFocusable( true );
|
||||
tab.setOnClickListener( new OnClickListener() {
|
||||
@Override
|
||||
public void onClick( View v ) {
|
||||
if ( getOnBeforeTabAction() != null ) {
|
||||
if ( getOnBeforeTabAction().doAction( position ) ) {
|
||||
pager.setCurrentItem( position, false );
|
||||
}
|
||||
} else {
|
||||
pager.setCurrentItem( position, false );
|
||||
}
|
||||
}
|
||||
} );
|
||||
|
||||
if ( openPadding ) {
|
||||
tab.setPadding( position == 0 ? 0 : tabPadding, 0, tabPadding, 0 );
|
||||
}
|
||||
|
||||
tabsContainer.addView( tab, position, shouldExpand ? expandedTabLayoutParams : defaultTabLayoutParams );
|
||||
}
|
||||
|
||||
private void updateTabStyles() {
|
||||
|
||||
for ( int i = 0; i < tabCount; i++ ) {
|
||||
|
||||
View v = tabsContainer.getChildAt( i );
|
||||
|
||||
if ( v instanceof SelectedState ) {
|
||||
( ( SelectedState ) v ).setSelectedState( i == selectedPosition );
|
||||
} else if ( v instanceof LinearLayout ) {
|
||||
|
||||
TextView tab = ( TextView ) ( ( LinearLayout ) v ).getChildAt( 0 );
|
||||
tab.setTextSize( TypedValue.COMPLEX_UNIT_PX, tabTextSize );
|
||||
tab.setTypeface( tabTypeface, tabTypefaceStyle );
|
||||
tab.setTextColor( tabTextColor );
|
||||
|
||||
if ( textAllCaps ) {
|
||||
tab.setAllCaps( true );
|
||||
}
|
||||
|
||||
if ( i == selectedPosition ) {
|
||||
tab.setTextColor( selectedTabTextColor );
|
||||
if ( indicatorMode == INDICATOR_MODE_SHADOW ) {
|
||||
tab.setBackgroundResource( R.drawable.module_apps_shape_deep_blue );
|
||||
}
|
||||
} else {
|
||||
tab.setTextColor( tabTextColor );
|
||||
tab.setBackgroundResource( 0 );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void scrollToChild( int position, int offset ) {
|
||||
|
||||
if ( tabCount == 0 ) {
|
||||
return;
|
||||
}
|
||||
|
||||
int newScrollX = tabsContainer.getChildAt( position ).getLeft() + offset;
|
||||
|
||||
if ( position > 0 || offset > 0 ) {
|
||||
newScrollX -= scrollOffset;
|
||||
}
|
||||
|
||||
if ( newScrollX != lastScrollX ) {
|
||||
lastScrollX = newScrollX;
|
||||
scrollTo( newScrollX, 0 );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDraw( Canvas canvas ) {
|
||||
super.onDraw( canvas );
|
||||
|
||||
if ( isInEditMode() || tabCount == 0 ) {
|
||||
return;
|
||||
}
|
||||
|
||||
final int height = getHeight();
|
||||
|
||||
|
||||
// draw indicator line
|
||||
|
||||
rectPaint.setColor( indicatorColor );
|
||||
|
||||
// default: line below current tab
|
||||
View currentTab = tabsContainer.getChildAt( currentPosition );
|
||||
float lineLeft = currentTab.getLeft();
|
||||
float lineRight = currentTab.getRight();
|
||||
|
||||
// if there is an offset, start interpolating left and right coordinates between current and next tab
|
||||
if ( currentPositionOffset > 0f && currentPosition < tabCount - 1 ) {
|
||||
|
||||
View nextTab = tabsContainer.getChildAt( currentPosition + 1 );
|
||||
final float nextTabLeft = nextTab.getLeft();
|
||||
final float nextTabRight = nextTab.getRight();
|
||||
|
||||
lineLeft = ( currentPositionOffset * nextTabLeft + ( 1f - currentPositionOffset ) * lineLeft );
|
||||
lineRight = ( currentPositionOffset * nextTabRight + ( 1f - currentPositionOffset ) * lineRight );
|
||||
}
|
||||
|
||||
if ( indicatorMode == INDICATOR_MODE_UNDERLINE ) {
|
||||
float lineOffset = getLineOffset();
|
||||
float left, right, top, bottom;
|
||||
if ( indicatorFitMode == INDICATOR_FIT_MODE_AUTO ) {
|
||||
left = lineLeft + lineOffset + indicatorMarginLeft;
|
||||
right = lineRight - lineOffset - indicatorMarginRight;
|
||||
top = height - indicatorHeight - indicatorMarginBottom;
|
||||
bottom = height - indicatorMarginBottom;
|
||||
} else {
|
||||
left = lineLeft + lineOffset;
|
||||
right = lineRight - lineOffset;
|
||||
top = height - indicatorHeight - indicatorMarginBottom;
|
||||
bottom = height - indicatorMarginBottom;
|
||||
}
|
||||
|
||||
if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ) {
|
||||
canvas.drawRoundRect( left, top, right, bottom, 90, 90, rectPaint );
|
||||
} else {
|
||||
canvas.drawRect( left, top, right, bottom, rectPaint );
|
||||
}
|
||||
}
|
||||
|
||||
if ( indicatorMode == INDICATOR_MODE_RES ) {
|
||||
if ( indicatorRes != null ) {
|
||||
int lineOffset = ( int ) getLineOffset();
|
||||
int left, right, top, bottom;
|
||||
int width = indicatorRes.getIntrinsicWidth();
|
||||
left = ( int ) lineLeft + lineOffset;
|
||||
right = ( int ) lineRight - lineOffset;
|
||||
top = height - indicatorRes.getIntrinsicHeight() - indicatorMarginBottom;
|
||||
left = left + ( right - left - width ) / 2;
|
||||
right = left + width;
|
||||
bottom = height - indicatorMarginBottom;
|
||||
indicatorRes.setBounds( left, top, right, bottom );
|
||||
indicatorRes.draw( canvas );
|
||||
}
|
||||
}
|
||||
|
||||
// draw underline
|
||||
|
||||
rectPaint.setColor( underlineColor );
|
||||
canvas.drawRect( 0, height - underlineHeight, tabsContainer.getWidth(), height, rectPaint );
|
||||
|
||||
// draw divider
|
||||
|
||||
dividerPaint.setColor( dividerColor );
|
||||
for ( int i = 0; i < tabCount - 1; i++ ) {
|
||||
View tab = tabsContainer.getChildAt( i );
|
||||
canvas.drawLine( tab.getRight(), dividerPadding, tab.getRight(), height - dividerPadding, dividerPaint );
|
||||
}
|
||||
}
|
||||
|
||||
private float indicatorLineOffset = -1;
|
||||
|
||||
private float getLineOffset() {
|
||||
|
||||
if ( tabsContainer.getChildCount() == 0 ) {
|
||||
return 0;
|
||||
}
|
||||
if ( !shouldExpand ) {
|
||||
return 0;
|
||||
}
|
||||
if ( indicatorLineOffset != -1 ) {
|
||||
return indicatorLineOffset;
|
||||
}
|
||||
|
||||
float minViewMeasuredWidth = tabsContainer.getChildAt( 0 ).getMeasuredWidth();
|
||||
float minContentWidth = getTabContentWidth( tabsContainer.getChildAt( 0 ) );
|
||||
for ( int i = 1; i < tabsContainer.getChildCount(); i++ ) {
|
||||
float nextContentWidth = getTabContentWidth( tabsContainer.getChildAt( i ) );
|
||||
if ( minContentWidth > nextContentWidth ) {
|
||||
minContentWidth = nextContentWidth;
|
||||
}
|
||||
}
|
||||
float offset = ( minViewMeasuredWidth - minContentWidth ) / 2;
|
||||
return indicatorLineOffset = offset == 0 ? WindowUtils.dip2px( getContext(), 25 ) : offset;
|
||||
}
|
||||
|
||||
private float getTabContentWidth( View tab ) {
|
||||
if ( tab instanceof TextView ) {
|
||||
TextPaint paint = ( ( TextView ) tab ).getPaint();
|
||||
return paint.measureText( ( ( TextView ) tab ).getText().toString() );
|
||||
}
|
||||
return tab.getMeasuredWidth();
|
||||
}
|
||||
|
||||
private class PageListener implements ViewPager.OnPageChangeListener {
|
||||
|
||||
@Override
|
||||
public void onPageScrolled( int position, float positionOffset, int positionOffsetPixels ) {
|
||||
|
||||
currentPosition = position;
|
||||
currentPositionOffset = positionOffset;
|
||||
|
||||
scrollToChild( position, ( int ) ( positionOffset * tabsContainer.getChildAt( position ).getWidth() ) );
|
||||
|
||||
invalidate();
|
||||
|
||||
if ( delegatePageListener != null ) {
|
||||
delegatePageListener.onPageScrolled( position, positionOffset, positionOffsetPixels );
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPageScrollStateChanged( int state ) {
|
||||
if ( state == ViewPager.SCROLL_STATE_IDLE ) {
|
||||
scrollToChild( pager.getCurrentItem(), 0 );
|
||||
}
|
||||
|
||||
if ( delegatePageListener != null ) {
|
||||
delegatePageListener.onPageScrollStateChanged( state );
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPageSelected( int position ) {
|
||||
selectedPosition = position;
|
||||
updateTabStyles();
|
||||
if ( delegatePageListener != null ) {
|
||||
delegatePageListener.onPageSelected( position );
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void setIndicatorColor( int indicatorColor ) {
|
||||
this.indicatorColor = indicatorColor;
|
||||
invalidate();
|
||||
}
|
||||
|
||||
public void setIndicatorColorResource( int resId ) {
|
||||
this.indicatorColor = getResources().getColor( resId );
|
||||
invalidate();
|
||||
}
|
||||
|
||||
public int getIndicatorColor() {
|
||||
return this.indicatorColor;
|
||||
}
|
||||
|
||||
public void setIndicatorHeight( int indicatorLineHeightPx ) {
|
||||
this.indicatorHeight = indicatorLineHeightPx;
|
||||
invalidate();
|
||||
}
|
||||
|
||||
public int getIndicatorHeight() {
|
||||
return indicatorHeight;
|
||||
}
|
||||
|
||||
public void setUnderlineColor( int underlineColor ) {
|
||||
this.underlineColor = underlineColor;
|
||||
invalidate();
|
||||
}
|
||||
|
||||
public void setUnderlineColorResource( int resId ) {
|
||||
this.underlineColor = getResources().getColor( resId );
|
||||
invalidate();
|
||||
}
|
||||
|
||||
public int getUnderlineColor() {
|
||||
return underlineColor;
|
||||
}
|
||||
|
||||
public void setDividerColor( int dividerColor ) {
|
||||
this.dividerColor = dividerColor;
|
||||
invalidate();
|
||||
}
|
||||
|
||||
public void setDividerColorResource( int resId ) {
|
||||
this.dividerColor = getResources().getColor( resId );
|
||||
invalidate();
|
||||
}
|
||||
|
||||
public int getDividerColor() {
|
||||
return dividerColor;
|
||||
}
|
||||
|
||||
public void setUnderlineHeight( int underlineHeightPx ) {
|
||||
this.underlineHeight = underlineHeightPx;
|
||||
invalidate();
|
||||
}
|
||||
|
||||
public int getUnderlineHeight() {
|
||||
return underlineHeight;
|
||||
}
|
||||
|
||||
public void setDividerPadding( int dividerPaddingPx ) {
|
||||
this.dividerPadding = dividerPaddingPx;
|
||||
invalidate();
|
||||
}
|
||||
|
||||
public int getDividerPadding() {
|
||||
return dividerPadding;
|
||||
}
|
||||
|
||||
public void setScrollOffset( int scrollOffsetPx ) {
|
||||
this.scrollOffset = scrollOffsetPx;
|
||||
invalidate();
|
||||
}
|
||||
|
||||
public int getScrollOffset() {
|
||||
return scrollOffset;
|
||||
}
|
||||
|
||||
public void setShouldExpand( boolean shouldExpand ) {
|
||||
this.shouldExpand = shouldExpand;
|
||||
notifyDataSetChanged();
|
||||
}
|
||||
|
||||
public boolean getShouldExpand() {
|
||||
return shouldExpand;
|
||||
}
|
||||
|
||||
public boolean isTextAllCaps() {
|
||||
return textAllCaps;
|
||||
}
|
||||
|
||||
public void setAllCaps( boolean textAllCaps ) {
|
||||
this.textAllCaps = textAllCaps;
|
||||
}
|
||||
|
||||
public void setTextSize( int textSizePx ) {
|
||||
this.tabTextSize = textSizePx;
|
||||
updateTabStyles();
|
||||
}
|
||||
|
||||
public int getTextSize() {
|
||||
return tabTextSize;
|
||||
}
|
||||
|
||||
public void setTextColor( int textColor ) {
|
||||
this.tabTextColor = textColor;
|
||||
updateTabStyles();
|
||||
}
|
||||
|
||||
public void setTextColorResource( int resId ) {
|
||||
this.tabTextColor = getResources().getColor( resId );
|
||||
updateTabStyles();
|
||||
}
|
||||
|
||||
public int getTextColor() {
|
||||
return tabTextColor;
|
||||
}
|
||||
|
||||
public void setSelectedTextColor( int textColor ) {
|
||||
this.selectedTabTextColor = textColor;
|
||||
updateTabStyles();
|
||||
}
|
||||
|
||||
public void setSelectedTextColorResource( int resId ) {
|
||||
this.selectedTabTextColor = getResources().getColor( resId );
|
||||
updateTabStyles();
|
||||
}
|
||||
|
||||
public int getSelectedTextColor() {
|
||||
return selectedTabTextColor;
|
||||
}
|
||||
|
||||
public void setTypeface( Typeface typeface, int style ) {
|
||||
this.tabTypeface = typeface;
|
||||
this.tabTypefaceStyle = style;
|
||||
updateTabStyles();
|
||||
}
|
||||
|
||||
public void setTabBackground( int resId ) {
|
||||
this.tabBackgroundResId = resId;
|
||||
updateTabStyles();
|
||||
}
|
||||
|
||||
public int getTabBackground() {
|
||||
return tabBackgroundResId;
|
||||
}
|
||||
|
||||
public void setTabPaddingLeftRight( int paddingPx ) {
|
||||
this.tabPadding = paddingPx;
|
||||
updateTabStyles();
|
||||
}
|
||||
|
||||
public int getTabPaddingLeftRight() {
|
||||
return tabPadding;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRestoreInstanceState( Parcelable state ) {
|
||||
SavedState savedState = ( SavedState ) state;
|
||||
super.onRestoreInstanceState( savedState.getSuperState() );
|
||||
currentPosition = savedState.currentPosition;
|
||||
requestLayout();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Parcelable onSaveInstanceState() {
|
||||
Parcelable superState = super.onSaveInstanceState();
|
||||
SavedState savedState = new SavedState( superState );
|
||||
savedState.currentPosition = currentPosition;
|
||||
return savedState;
|
||||
}
|
||||
|
||||
static class SavedState extends BaseSavedState {
|
||||
int currentPosition;
|
||||
|
||||
public SavedState( Parcelable superState ) {
|
||||
super( superState );
|
||||
}
|
||||
|
||||
private SavedState( Parcel in ) {
|
||||
super( in );
|
||||
currentPosition = in.readInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToParcel( Parcel dest, int flags ) {
|
||||
super.writeToParcel( dest, flags );
|
||||
dest.writeInt( currentPosition );
|
||||
}
|
||||
|
||||
public static final Creator< SavedState > CREATOR = new Creator< SavedState >() {
|
||||
@Override
|
||||
public SavedState createFromParcel( Parcel in ) {
|
||||
return new SavedState( in );
|
||||
}
|
||||
|
||||
@Override
|
||||
public SavedState[] newArray( int size ) {
|
||||
return new SavedState[size];
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user