Skip to content

Commit 4483650

Browse files
committed
Legacy Parts: Update PagerSlidingTabStrip class
-- Check if tabCount > 0 (do this before attempting tabsContainer.getChildAt(position) when scrolling to child.) -- if pager == null no need to call requestLayout() -- make shouldExpand work correctly (referenced here: astuetz/PagerSlidingTabStrip#42) -- Use Gravity to center tabs -- add onTabReselected() callbackFunction (referenced here: astuetz/PagerSlidingTabStrip#92) -- Fix: Tab dislocation caused by setViewPager Change-Id: I0184de3c010a902a721572d3dcaa75101e38aa0c Signed-off-by: Andre Saddler <[email protected]>
1 parent f3b7665 commit 4483650

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

res/layout/legacy_parts.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
<com.android.settings.PALP.extensions.PagerSlidingTabStrip
2525
android:id="@+id/tabs"
26-
android:layout_width="match_parent"
26+
android:layout_width="fill_parent"
2727
android:layout_height="48dip"
2828
android:background="@drawable/background_tab"
2929
aospal:dividerColor="@android:color/white" />

src/com/android/settings/PALP/extensions/PagerSlidingTabStrip.java

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,17 @@ public class PagerSlidingTabStrip extends HorizontalScrollView {
4848
public interface IconTabProvider {
4949
public int getPageIconResId(int position);
5050
}
51+
52+
public interface TabListener {
53+
public void onTabReselected(View tab, int postion);
54+
}
5155

56+
public void setOnTabListener(TabListener listener) {
57+
mListener = listener;
58+
}
59+
60+
private TabListener mListener;
61+
5262
// @formatter:off
5363
private static final int[] ATTRS = new int[] {
5464
android.R.attr.textSize,
@@ -57,7 +67,6 @@ public interface IconTabProvider {
5767
// @formatter:on
5868

5969
private LinearLayout.LayoutParams defaultTabLayoutParams;
60-
private LinearLayout.LayoutParams expandedTabLayoutParams;
6170

6271
private final PageListener pageListener = new PageListener();
6372
public OnPageChangeListener delegatePageListener;
@@ -165,22 +174,23 @@ public PagerSlidingTabStrip(Context context, AttributeSet attrs, int defStyle) {
165174
dividerPaint.setStrokeWidth(dividerWidth);
166175

167176
defaultTabLayoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
168-
expandedTabLayoutParams = new LinearLayout.LayoutParams(0, LayoutParams.MATCH_PARENT, 1.0f);
169177

170178
if (locale == null) {
171179
locale = getResources().getConfiguration().locale;
172180
}
173181
}
174182

183+
public void setTabContainerGravity(int gravity) {
184+
tabsContainer.setGravity(gravity);
185+
}
186+
175187
public void setViewPager(ViewPager pager) {
176188
this.pager = pager;
177189

178190
if (pager.getAdapter() == null) {
179191
throw new IllegalStateException("ViewPager does not have adapter instance.");
180192
}
181193

182-
pager.setOnPageChangeListener(pageListener);
183-
184194
notifyDataSetChanged();
185195
}
186196

@@ -223,6 +233,7 @@ public void onGlobalLayout() {
223233

224234
currentPosition = pager.getCurrentItem();
225235
scrollToChild(currentPosition, 0);
236+
pager.setOnPageChangeListener(pageListener);
226237
}
227238
});
228239

@@ -239,7 +250,12 @@ private void addTextTab(final int position, String title) {
239250
tab.setOnClickListener(new OnClickListener() {
240251
@Override
241252
public void onClick(View v) {
242-
pager.setCurrentItem(position);
253+
if (position == currentPosition && mListener !=null) {
254+
// reselected tab
255+
mListener.onTabReselected(v, position);
256+
} else {
257+
pager.setCurrentItem(position);
258+
}
243259
}
244260
});
245261

@@ -317,10 +333,11 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
317333

318334
if (childWidth <= myWidth) {
319335
for (int i = 0; i < tabCount; i++) {
320-
tabsContainer.getChildAt(i).setLayoutParams(expandedTabLayoutParams);
336+
View tabView = tabsContainer.getChildAt(i);
337+
tabView.setLayoutParams(new LinearLayout.LayoutParams(0, LayoutParams.MATCH_PARENT, tabView.getMeasuredWidth()));
321338
}
322339
}
323-
340+
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
324341
checkedTabWidths = true;
325342
}
326343
}
@@ -398,8 +415,7 @@ public void onPageScrolled(int position, float positionOffset, int positionOffse
398415
currentPosition = position;
399416
currentPositionOffset = positionOffset;
400417

401-
scrollToChild(position, (int) (positionOffset * tabsContainer.getChildAt(position).getWidth()));
402-
418+
scrollToChild(position, tabCount > 0 ? (int) (positionOffset * tabsContainer.getChildAt(position).getWidth()) : 0);
403419
invalidate();
404420

405421
if (delegatePageListener != null) {
@@ -507,7 +523,9 @@ public int getScrollOffset() {
507523

508524
public void setShouldExpand(boolean shouldExpand) {
509525
this.shouldExpand = shouldExpand;
510-
requestLayout();
526+
if (pager != null) {
527+
requestLayout();
528+
}
511529
}
512530

513531
public boolean getShouldExpand() {

0 commit comments

Comments
 (0)