Skip to content

Commit 967c366

Browse files
committed
增加修改屏幕范围内可显示的Tab个数
1 parent 0436f28 commit 967c366

File tree

3 files changed

+26
-32
lines changed

3 files changed

+26
-32
lines changed

app/src/main/res/values/attrs.xml

Lines changed: 0 additions & 29 deletions
This file was deleted.

xtablayoutlibrary/src/main/java/com/androidkun/xtablayout/XTabLayout.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import android.view.View;
3737
import android.view.ViewGroup;
3838
import android.view.ViewParent;
39+
import android.view.WindowManager;
3940
import android.view.accessibility.AccessibilityEvent;
4041
import android.view.accessibility.AccessibilityNodeInfo;
4142
import android.widget.HorizontalScrollView;
@@ -179,6 +180,7 @@ public interface OnTabSelectedListener {
179180
private int mTabMaxWidth = Integer.MAX_VALUE;
180181
private final int mRequestedTabMinWidth;
181182
private final int mRequestedTabMaxWidth;
183+
private int xTabDisplayNum;
182184
private final int mScrollableTabMinWidth;
183185

184186
private int mContentInsetStart;
@@ -271,6 +273,7 @@ public XTabLayout(Context context, AttributeSet attrs, int defStyleAttr) {
271273
mTabTextColors = createColorStateList(mTabTextColors.getDefaultColor(), selected);
272274
}
273275

276+
xTabDisplayNum = a.getInt(R.styleable.XTabLayout_xTabDisplayNum, 0);
274277
mRequestedTabMinWidth = a.getDimensionPixelSize(R.styleable.XTabLayout_xTabMinWidth,
275278
INVALID_WIDTH);
276279
mRequestedTabMaxWidth = a.getDimensionPixelSize(R.styleable.XTabLayout_xTabMaxWidth,
@@ -310,6 +313,10 @@ public void setSelectedTabIndicatorHeight(int height) {
310313
mTabStrip.setSelectedIndicatorHeight(height);
311314
}
312315

316+
public void setxTabDisplayNum(int xTabDisplayNum) {
317+
this.xTabDisplayNum = xTabDisplayNum;
318+
}
319+
313320
/**
314321
* Set the scroll position of the tabs. This is useful for when the tabs are being displayed as
315322
* part of a scrolling container such as {@link ViewPager}.
@@ -346,7 +353,7 @@ private void setScrollPosition(int position, float positionOffset, boolean updat
346353
if (updateSelectedText) {
347354
setSelectedTabView(roundedPosition);
348355
}
349-
Log.w("AAA","end");
356+
Log.w("AAA", "end");
350357
}
351358

352359

@@ -713,6 +720,7 @@ private void populateFromPagerAdapter() {
713720

714721
if (mPagerAdapter != null) {
715722
final int adapterCount = mPagerAdapter.getCount();
723+
716724
for (int i = 0; i < adapterCount; i++) {
717725
addTab(newTab().setText(mPagerAdapter.getPageTitle(i)), false);
718726
}
@@ -921,7 +929,7 @@ public void onAnimationUpdate(ValueAnimatorCompat animator) {
921929
}
922930

923931
// Now animate the indicator
924-
Log.w("AAA","123");
932+
Log.w("AAA", "123");
925933
mTabStrip.animateIndicatorToPosition(newPosition, ANIMATION_DURATION);
926934
}
927935

@@ -1781,7 +1789,7 @@ private void setIndicatorPosition(int left, int right) {
17811789
}
17821790

17831791
void animateIndicatorToPosition(final int position, int duration) {
1784-
Log.w("AAA","animateIndicatorToPosition");
1792+
Log.w("AAA", "animateIndicatorToPosition");
17851793
if (mIndicatorAnimator != null && mIndicatorAnimator.isRunning()) {
17861794
mIndicatorAnimator.cancel();
17871795
}
@@ -1901,6 +1909,20 @@ private int getDefaultHeight() {
19011909
}
19021910

19031911
private int getTabMinWidth() {
1912+
Log.w("AAA","getTabMinWidth 1111");
1913+
if (mPagerAdapter != null && xTabDisplayNum != 0) {
1914+
WindowManager wm = (WindowManager) getContext()
1915+
.getSystemService(Context.WINDOW_SERVICE);
1916+
if(mPagerAdapter.getCount()<xTabDisplayNum){
1917+
1918+
Log.w("AAA","getTabMinWidth 22222");
1919+
return wm.getDefaultDisplay().getWidth()/mPagerAdapter.getCount();
1920+
}else{
1921+
Log.w("AAA","getTabMinWidth 33333");
1922+
return wm.getDefaultDisplay().getWidth()/xTabDisplayNum;
1923+
}
1924+
}
1925+
Log.w("AAA","getTabMinWidth 44444");
19041926
if (mRequestedTabMinWidth != INVALID_WIDTH) {
19051927
// If we have been given a min width, use it
19061928
return mRequestedTabMinWidth;

xtablayoutlibrary/src/main/res/values/attrs.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@
2525

2626
<attr name="xTabTextSize" format="dimension"/>
2727
<attr name="xTabSelectedTextSize" format="dimension"/>
28+
<attr name="xTabDisplayNum" format="integer"/>
2829
</declare-styleable>
2930
</resources>

0 commit comments

Comments
 (0)