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

Add MODE_ALIGN for LinePagerIndicator #289

Open
wants to merge 1 commit into
base: main
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 @@ -27,6 +27,7 @@
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;
import net.lucode.hackware.magicindicatordemo.R;
import net.lucode.hackware.magicindicatordemo.ext.titles.ScaleTransitionPagerTitleView;
import net.lucode.hackware.magicindicatordemo.ext.titles.WithIconTitleView;

import java.util.Arrays;
import java.util.List;
Expand All @@ -50,6 +51,7 @@ protected void onCreate(Bundle savedInstanceState) {
initMagicIndicator2();
initMagicIndicator3();
initMagicIndicator4();
initMagicIndicator5();
}

private void initMagicIndicator1() {
Expand Down Expand Up @@ -240,4 +242,53 @@ public void onPageSelected(int position) {
}
});
}


private void initMagicIndicator5() {
MagicIndicator magicIndicator = (MagicIndicator) findViewById(R.id.magic_indicator5);
CommonNavigator commonNavigator = new CommonNavigator(this);
commonNavigator.setAdapter(new CommonNavigatorAdapter() {

@Override
public int getCount() {
return mDataList.size();
}

@Override
public IPagerTitleView getTitleView(Context context, final int index) {
IPagerTitleView view;
if (index == 1) {
WithIconTitleView withIconTitleView = new WithIconTitleView(context);
withIconTitleView.setNormalColor(Color.GRAY);
withIconTitleView.setSelectedColor(Color.WHITE);
view = withIconTitleView;
} else {
SimplePagerTitleView simplePagerTitleView = new SimplePagerTitleView(context);
simplePagerTitleView.setNormalColor(Color.GRAY);
simplePagerTitleView.setSelectedColor(Color.WHITE);
simplePagerTitleView.setText(mDataList.get(index));
view = simplePagerTitleView;
}
((View)view).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mViewPager.setCurrentItem(index);
}
});
return view;
}

@Override
public IPagerIndicator getIndicator(Context context) {
LinePagerIndicator linePagerIndicator = new LinePagerIndicator(context);
linePagerIndicator.setMode(LinePagerIndicator.MODE_ALIGN);
linePagerIndicator.setLineHeight(UIUtil.dip2px(context, 4.0));
linePagerIndicator.setLineWidth(UIUtil.dip2px(context, 20.0));
linePagerIndicator.setColors(Color.BLUE);
return linePagerIndicator;
}
});
magicIndicator.setNavigator(commonNavigator);
ViewPagerHelper.bind(magicIndicator, mViewPager);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class CommonPagerIndicator extends View implements IPagerIndicator {
public static final int MODE_MATCH_EDGE = 0; // drawable宽度 == title宽度 - 2 * mXOffset
public static final int MODE_WRAP_CONTENT = 1; // drawable宽度 == title内容宽度 - 2 * mXOffset
public static final int MODE_EXACTLY = 2;
public static final int MODE_ALIGN_CONTENT = 3;

private int mMode; // 默认为MODE_MATCH_EDGE模式
private Drawable mIndicatorDrawable;
Expand Down Expand Up @@ -61,27 +62,40 @@ public void onPageScrolled(int position, float positionOffset, int positionOffse
float nextLeftX;
float rightX;
float nextRightX;
if (mMode == MODE_MATCH_EDGE) {
leftX = current.mLeft + mXOffset;
nextLeftX = next.mLeft + mXOffset;
rightX = current.mRight - mXOffset;
nextRightX = next.mRight - mXOffset;
mDrawableRect.top = (int) mYOffset;
mDrawableRect.bottom = (int) (getHeight() - mYOffset);
} else if (mMode == MODE_WRAP_CONTENT) {
leftX = current.mContentLeft + mXOffset;
nextLeftX = next.mContentLeft + mXOffset;
rightX = current.mContentRight - mXOffset;
nextRightX = next.mContentRight - mXOffset;
mDrawableRect.top = (int) (current.mContentTop - mYOffset);
mDrawableRect.bottom = (int) (current.mContentBottom + mYOffset);
} else { // MODE_EXACTLY
leftX = current.mLeft + (current.width() - mDrawableWidth) / 2;
nextLeftX = next.mLeft + (next.width() - mDrawableWidth) / 2;
rightX = current.mLeft + (current.width() + mDrawableWidth) / 2;
nextRightX = next.mLeft + (next.width() + mDrawableWidth) / 2;
mDrawableRect.top = (int) (getHeight() - mDrawableHeight - mYOffset);
mDrawableRect.bottom = (int) (getHeight() - mYOffset);
switch (mMode) {
case MODE_MATCH_EDGE:
leftX = current.mLeft + mXOffset;
nextLeftX = next.mLeft + mXOffset;
rightX = current.mRight - mXOffset;
nextRightX = next.mRight - mXOffset;
mDrawableRect.top = (int) mYOffset;
mDrawableRect.bottom = (int) (getHeight() - mYOffset);
break;
case MODE_WRAP_CONTENT:
leftX = current.mContentLeft + mXOffset;
nextLeftX = next.mContentLeft + mXOffset;
rightX = current.mContentRight - mXOffset;
nextRightX = next.mContentRight - mXOffset;
mDrawableRect.top = (int) (current.mContentTop - mYOffset);
mDrawableRect.bottom = (int) (current.mContentBottom + mYOffset);
break;
case MODE_ALIGN_CONTENT:
leftX = (current.mContentRight + current.mContentLeft - mDrawableWidth) / 2;
nextLeftX = (next.mContentRight + next.mContentLeft - mDrawableWidth) / 2;
rightX = leftX + mDrawableWidth;
nextRightX = nextLeftX + mDrawableWidth;
mDrawableRect.top = (int) (getHeight() - mDrawableHeight - mYOffset);
mDrawableRect.bottom = (int) (getHeight() - mYOffset);
break;
case MODE_EXACTLY:
default:
leftX = current.mLeft + (current.width() - mDrawableWidth) / 2;
nextLeftX = next.mLeft + (next.width() - mDrawableWidth) /2;
rightX = current.mLeft + (current.width() + mDrawableWidth) / 2;
nextRightX = next.mLeft + (next.width() + mDrawableWidth) / 2;
mDrawableRect.top = (int) (getHeight() - mDrawableHeight - mYOffset);
mDrawableRect.bottom = (int) (getHeight() - mYOffset);
break;
}

mDrawableRect.left = (int) (leftX + (nextLeftX - leftX) * mStartInterpolator.getInterpolation(positionOffset));
Expand Down Expand Up @@ -140,7 +154,7 @@ public int getMode() {
}

public void setMode(int mode) {
if (mode == MODE_EXACTLY || mode == MODE_MATCH_EDGE || mode == MODE_WRAP_CONTENT) {
if (mode >= MODE_EXACTLY && mode <= MODE_ALIGN_CONTENT) {
mMode = mode;
} else {
throw new IllegalArgumentException("mode " + mode + " not supported.");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package net.lucode.hackware.magicindicatordemo.ext.titles;

import android.content.Context;
import android.view.LayoutInflater;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IMeasurablePagerTitleView;
import net.lucode.hackware.magicindicatordemo.R;

public class WithIconTitleView extends FrameLayout implements IMeasurablePagerTitleView {
protected int mSelectedColor;
protected int mNormalColor;
private TextView mTabWithIconText;

public WithIconTitleView(@NonNull Context context) {
super(context);
initView(context);
}

private void initView(Context context) {
LayoutInflater.from(context).inflate(R.layout.simple_title_with_icon_layout, this, true);
mTabWithIconText = findViewById(R.id.tab_with_icon_text);
}

@Override
public int getContentLeft() {
return getLeft() + mTabWithIconText.getLeft();
}

@Override
public int getContentTop() {
return getTop();
}

@Override
public int getContentRight() {
return getLeft() + mTabWithIconText.getRight();
}

@Override
public int getContentBottom() {
return getBottom();
}

@Override
public void onSelected(int index, int totalCount) {
mTabWithIconText.setTextColor(mSelectedColor);
}

@Override
public void onDeselected(int index, int totalCount) {
mTabWithIconText.setTextColor(mNormalColor);
}

@Override
public void onLeave(int index, int totalCount, float leavePercent, boolean leftToRight) {

}

@Override
public void onEnter(int index, int totalCount, float enterPercent, boolean leftToRight) {

}

public int getSelectedColor() {
return mSelectedColor;
}

public void setSelectedColor(int selectedColor) {
mSelectedColor = selectedColor;
}

public int getNormalColor() {
return mNormalColor;
}

public void setNormalColor(int normalColor) {
mNormalColor = normalColor;
}

}
16 changes: 16 additions & 0 deletions app/src/main/res/layout/activity_fixed_tab_example_layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,22 @@

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/navigator_margin_top"
android:background="@android:color/black"
android:orientation="vertical">

<net.lucode.hackware.magicindicator.MagicIndicator
android:id="@+id/magic_indicator5"
android:layout_width="wrap_content"
android:layout_height="@dimen/common_navigator_height"
android:layout_gravity="center_horizontal" />

</LinearLayout>


<androidx.viewpager.widget.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
Expand Down
27 changes: 27 additions & 0 deletions app/src/main/res/layout/simple_title_with_icon_layout.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/tab_with_icon_text"
android:gravity="center_vertical"
android:text="long long text" />


<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="@android:drawable/ic_menu_search"
android:layout_gravity="center_vertical"
android:background="@android:color/transparent"
android:id="@+id/tab_with_icon_btn" />

</LinearLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class LinePagerIndicator extends View implements IPagerIndicator {
public static final int MODE_MATCH_EDGE = 0; // 直线宽度 == title宽度 - 2 * mXOffset
public static final int MODE_WRAP_CONTENT = 1; // 直线宽度 == title内容宽度 - 2 * mXOffset
public static final int MODE_EXACTLY = 2; // 直线宽度 == mLineWidth
public static final int MODE_ALIGN = 3; // 直线宽度 == mLineWidth, 且尝试居中内容

private int mMode; // 默认为MODE_MATCH_EDGE模式

Expand Down Expand Up @@ -94,6 +95,11 @@ public void onPageScrolled(int position, float positionOffset, int positionOffse
nextLeftX = next.mContentLeft + mXOffset;
rightX = current.mContentRight - mXOffset;
nextRightX = next.mContentRight - mXOffset;
} else if (mMode == MODE_ALIGN) {
leftX = (current.mContentRight + current.mContentLeft - mLineWidth) / 2;
nextLeftX = (next.mContentRight + next.mContentLeft - mLineWidth) / 2;
rightX = leftX + mLineWidth;
nextRightX = nextLeftX + mLineWidth;
} else { // MODE_EXACTLY
leftX = current.mLeft + (current.width() - mLineWidth) / 2;
nextLeftX = next.mLeft + (next.width() - mLineWidth) / 2;
Expand Down Expand Up @@ -167,7 +173,7 @@ public int getMode() {
}

public void setMode(int mode) {
if (mode == MODE_EXACTLY || mode == MODE_MATCH_EDGE || mode == MODE_WRAP_CONTENT) {
if (mode <= MODE_ALIGN) {
mMode = mode;
} else {
throw new IllegalArgumentException("mode " + mode + " not supported.");
Expand Down