我在安卓滑动标签布局和视图分页器中有12个项目。在从左至右滑动时,选定的标签应该像Play商店一样位于中心。请帮忙解决如何实现。在我的应用中,活动标签始终在屏幕左侧。
图片链接略。
图片链接略。
我的方法与Shripad Bhat的解决方案略有不同,他在幻灯片末尾弹跳标签。
这是我的解决方法...
onPageScrolled
方法的更改:
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
int tabStripChildCount = mTabStrip.getChildCount();
if ((tabStripChildCount == 0) || (position < 0) || (position >= tabStripChildCount)) {
return;
}
mTabStrip.onViewPagerPageChanged(position, positionOffset);
View selectedTitle = mTabStrip.getChildAt(position);
int selectedOffset = (selectedTitle == null) ? 0 : selectedTitle.getWidth();
int nextTitlePosition = position + 1;
View nextTitle = mTabStrip.getChildAt(nextTitlePosition);
int nextOffset = (nextTitle == null) ? 0 : nextTitle.getWidth();
int extraOffset = (int)(0.5F * (positionOffset * (float)(selectedOffset + nextOffset)));
scrollToTab(position, extraOffset);
if (mViewPagerPageChangeListener != null) {
mViewPagerPageChangeListener.onPageScrolled(position, positionOffset, positionOffsetPixels);
}
}
scrollToTab
方法的更改:
private int mLastScrollTo;
private void scrollToTab(int tabIndex, int positionOffset) {
final int tabStripChildCount = mTabStrip.getChildCount();
if (tabStripChildCount == 0 || tabIndex < 0 || tabIndex >= tabStripChildCount) {
return;
}
View selectedChild = mTabStrip.getChildAt(tabIndex);
if (selectedChild != null && selectedChild.getMeasuredWidth() != 0) {
int targetScrollX = ((positionOffset + selectedChild.getLeft()) - getWidth() / 2) + selectedChild.getWidth() / 2;
if (targetScrollX != mLastScrollTo) {
scrollTo(targetScrollX, 0);
mLastScrollTo = targetScrollX;
}
}
}
onPageScrolled
,我正在使用带有设计库的 FragmentStatePagerAdapter
和 TabLayout
。 - Lavekush Agrawal谷歌提供了滑动选项卡布局的示例。但在实现SlidingTabLayout类时,它并没有设计使选定的选项卡居中对齐。我已经修改了滚动方法,使选定/活动选项卡位于屏幕中央。以下是代码更改:
行号:241, scrollToTab{}
更新后的方法:
private void scrollToTab(int tabIndex, int positionOffset) {
final int tabStripChildCount = mTabStrip.getChildCount();
if (tabStripChildCount == 0 || tabIndex < 0 || tabIndex >= tabStripChildCount) {
return;
}
View selectedChild = mTabStrip.getChildAt(tabIndex);
if (selectedChild != null) {
int targetScrollX = selectedChild.getLeft() + positionOffset;
if (tabIndex > 0 || positionOffset > 0) {
// If we're not at the first child and are mid-scroll, make sure we obey the offset
targetScrollX -= (getWidth()-selectedChild.getWidth())/2;
}
scrollTo(targetScrollX, 0);
}
}
我也遇到了这个问题。tabLayout只提供了tabContentStart选项,但需要两边都有:
public class CenteringTabLayout extends TabLayout {
public CenteringTabLayout(Context context) {
super(context);
}
public CenteringTabLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CenteringTabLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
View firstTab = ((ViewGroup)getChildAt(0)).getChildAt(0);
View lastTab = ((ViewGroup)getChildAt(0)).getChildAt(((ViewGroup)getChildAt(0)).getChildCount()-1);
ViewCompat.setPaddingRelative(getChildAt(0), (getWidth()/2) - (firstTab.getWidth()/2),0,(getWidth()/2) - (lastTab.getWidth()/2),0);
}
}