gpt4 book ai didi

android - 如何在android滑动标签布局中对齐 Activity 标签中心

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:55:19 25 4
gpt4 key购买 nike

我在 android 滑动选项卡布局 + 查看寻呼机中有 12 个项目。从左向右滑动时,所选选项卡应与 Play 商店居中。请帮我怎么做。在我的应用程序中, Activity 选项卡始终位于屏幕左侧。

enter image description here

enter image description here

最佳答案

我的方法与 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;
}
}
}

关于android - 如何在android滑动标签布局中对齐 Activity 标签中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27768239/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com