Android 如何在游戏市场中居中滑动?

Android 如何在游戏市场中居中滑动?,android,tabs,pagerslidingtabstrip,Android,Tabs,Pagerslidingtabstrip,我正在使用https://github.com/google/iosched/blob/master/android/src/main/java/com/google/samples/apps/iosched/ui/widget/SlidingTabLayout.javatabs类 将其应用于我的项目后,我将获得(不居中)选项卡,如所示 ,但我想将我的选项卡置于选项卡的中心 如何像在Google Play应用程序中那样居中显示选项卡,谢谢?修改scrollToTab方法以居中显示所选选项卡 pr

我正在使用
https://github.com/google/iosched/blob/master/android/src/main/java/com/google/samples/apps/iosched/ui/widget/SlidingTabLayout.java
tabs类

将其应用于我的项目后,我将获得(不居中)选项卡,如所示 ,但我想将我的选项卡置于选项卡的中心


如何像在Google Play应用程序中那样居中显示选项卡,谢谢?

修改
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 viewWidth = getWidth();
        int tabWidth = selectedChild.getWidth();
        int tabPosition = selectedChild.getLeft() + positionOffset;
        int targetScrollX = tabPosition - viewWidth / 2 + tabWidth / 2;

        scrollTo(targetScrollX, 0);
    }
}
private void scrollToTab(int tabIndex,int positionOffset){
final int tabStripChildCount=mTabStrip.getChildCount();
如果(tabStripChildCount==0 | | tabIndex<0 | | tabIndex>=tabStripChildCount){
返回;
}
查看selectedChild=mTabStrip.getChildAt(tabIndex);
如果(selectedChild!=null){
int viewWidth=getWidth();
int tabWidth=selectedChild.getWidth();
int tabPosition=selectedChild.getLeft()+positionOffset;
int targetScrollX=tabPosition-viewWidth/2+tabWidth/2;
scrollTo(targetScrollX,0);
}
}

您的活动\u main.xml文件应如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

<samples.exoguru.materialtabs.SlidingTabLayout
    android:id="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:elevation="2dp"
    android:background="@color/ColorPrimary"/>

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:layout_weight="1">
</android.support.v4.view.ViewPager>

我不知道还有什么会出错的


我花了一点时间在这上面,最近才让它工作起来。这是总结

slidengtablayout.java 必须修改
scrollToTab
方法才能计算正确的滚动。我在这里添加了解释性意见:

private void scrollToTab(int tabIndex, float positionOffset) {
    final int tabStripChildCount = mTabStrip.getChildCount();
    if (tabStripChildCount == 0 || tabIndex < 0 || tabIndex >= tabStripChildCount) {
        return;
    }

    View selectedChild = mTabStrip.getChildAt(tabIndex);
    View nextChild = mTabStrip.getChildAt(tabIndex + 1);
    if (selectedChild != null) {
        // c1 is the center of the currently selected tab
        int c1 = selectedChild.getLeft() + (selectedChild.getWidth() / 2);

        // viewCenter is the center of the parent view
        int viewCenter = getWidth() / 2;

        int targetCenter = c1;
        if (nextChild != null) {
            // c2 is the center of the tab to the right of the currently selected
            int c2 = nextChild.getLeft() + (nextChild.getWidth() / 2);

            // the 'targetCenter' is partway between c1 & c2 depending on how far the viewpager
            // is currently scrolled
            targetCenter = (int) (c1 + ((c2 - c1) * positionOffset));
        }

        int targetScrollX = targetCenter - viewCenter;

        scrollTo(targetScrollX, 0);
    }
}
将是:

        scrollToTab(position, positionOffset);
这使我们能够访问执行正确计算所需的原始
位置偏移量
参数


这些是唯一需要的修改。我在这里发布了整个课程的要点:

可能与@karaokyo重复很明显,它不是重复的:D@AndreyLaw你能展示一下你使用的代码吗?我现在所能做的就是给你一个好教程的链接——和你链接上的代码一样。我认为-需要更改SlidingTabLayout.java以获得对中支持。你怎么知道的?也许有人会这么做)@edwinj这在概念上非常相似,谢谢,但没什么不顺利的。在制表符到达中心位置后,制表符稍微向左(或向右)滑动。你是说在你做出改变之前它没有这么做吗?这似乎是动画逻辑的一个问题,我没有接触也没有看它,因为它与你原来的问题无关。你不正确-在你改变之前动画是好的。我现在使用这个库(见屏幕:全部ok:)动画,居中-全部ok。我想你们完全不理解我,你们只有两个选项卡,两个选项卡如何在屏幕上居中。这里是我的字符串数组从字符串数组创建所有选项卡,并尝试将选项卡居中,就像我在屏幕和视频上显示的那样。。。你的布局对我来说没什么问题。。。布局很好,slidengtablayout.java-不好。您只有2个选项卡-创建更多选项卡
        scrollToTab(position, positionOffset);