【问题标题】:How to align active tab center in android Sliding Tab Layout如何在android滑动选项卡布局中对齐活动选项卡中心
【发布时间】:2017-07-04 06:51:05
【问题描述】:

我在 android 滑动标签布局 + 视图寻呼机中有 12 个项目。从左到右滑动时,选定的选项卡应与 Play 商店相同。请帮助我如何做到这一点。在我的应用程序中,活动选项卡始终位于屏幕左侧。

【问题讨论】:

  • 您是否在项目中使用PagerTabStrip(这是 Google Play 应用程序正在使用的)。看起来您正在使用普通标签。
  • 不,我使用了 google 提供的 SlidingTabLayout 类作为滑动标签布局的示例类。我也试过 PagerTabStrip 它不允许你像播放商店一样滑动标签标题。 SlidingTabLayout 扩展了 Horizo​​ntalScrollView。
  • 我明白了。查看来自 Android 开发人员的 video,您会发现这不是预期的行为。为了得到Google Play的效果,你必须使用PagerTabStrip。实现起来超级简单,查看this tutorial
  • 谢谢马克。我得到解决的问题是我的答案。
  • SmartTabLayout 做得很好。

标签: android android-viewpager


【解决方案1】:

我的方法与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
  • 你好@NickUnuchek 你是否设法在中心制作了第一个标签..?请帮忙
【解决方案2】:

Google 提供了sliding tab layout 的示例。但是在 SlidingTabLayout 类的实现中,它并不是为选中的选项卡居中对齐而设计的。我修改了滚动方法以使屏幕的选定/活动选项卡居中。这是代码更改:

类名: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);
    }
}

【讨论】:

  • 您不需要获取显示来测量滚动视图的宽度,只需调用 getWidth() ;) 即:targetScrollX -= (getWidth()-selectedChild.getWidth())/2 ;
  • Yuri Heupa,更新了答案。感谢您的建议。
  • +1 它可以工作 :) 但是,在将其置于中心位置时,最后会出现轻微的抖动。下面修复它。 :) 干得好。
【解决方案3】:

也有这个问题。 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);
}
}

【讨论】:

  • 您的代码在使用 ViewCompat 的行上引发了异常,您能否再解释一下您的代码,以便我了解您真正想要做什么。
  • 我扩展了 android.support.design.widget.TabLayout。第一个孩子是 SlidingTabStrip ,每个选项卡都是它的一个孩子,我正在尝试为这个 SlidingTabStrip 设置填充但即使我的也没有正确地居中最后一个选项卡。还是没找到原因。
猜你喜欢
  • 1970-01-01
  • 2018-07-12
  • 1970-01-01
  • 1970-01-01
  • 2015-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多