android - 滑动 View 分页器 fragment 不会移动选项卡

标签 android android-layout android-viewpager swipe

我遵循了这些教程 Android TabLayout Example using ViewPager and Fragments Android TabLayout Example

但是如果我滑动 fragment , View 寻呼机将不会移动。 我得到这个结果 output I got 我按照上面的教程没有任何变化,但是当我们滑动 fragment 时,选项卡不会移动。请帮我。提前致谢。

这是activity_main.xml

<LinearLayout
android:id="@+id/main_layout"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<!-- our toolbar -->
<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

<!-- our tablayout to display tabs  -->
<android.support.design.widget.TabLayout
    android:id="@+id/tabLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

<!-- View pager to swipe views -->
<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"/>

 </LinearLayout>

最佳答案

viewpager 上的页面正在滑动,这就是它在页面上显示“Tab 3”的原因。这是由于 ViewPager 的滑动属性而发生的。但是选项卡没有相应更改,这就是它显示在 TAB1 上的原因。因为没有在 tablayout 和 viewpager 之间建立链接。

如果您将 ViewPager 与此选项卡布局一起使用,则可以调用 setupWithViewPager(ViewPager) 将两者链接在一起,描述 here .

因此无需调用 addTab()。

mTabLayout = (TabLayout)findViewById(R.id.tabLayout);
mViewPager = (ViewPager)findViewById(R.id.pager);

//mTabLayout.addTab(mTabLayout.newTab().setText("Tab1"));
//mTabLayout.addTab(mTabLayout.newTab().setText("Tab2"));
//mTabLayout.addTab(mTabLayout.newTab().setText("Tab3"));
//mTabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

Pager pager = new Pager(getSupportFragmentManager(),3);
mViewPager.setAdapter(pager);

mTabLayout.setupWithViewPager(mViewPager);

此标签布局将从 PagerAdapter 的页面标题自动填充。为此,

class Pager extends FragmentStatePagerAdapter{...

@Override
public CharSequence getPageTitle(int position) {
    super.getPageTitle(position);

    switch (position){
        case 0:
            return "Tab1";
        case 1:
            return "Tab2";
        case 2:
            return "Tab3";

        default:
            return null;
    }
}

关于android - 滑动 View 分页器 fragment 不会移动选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38658480/

相关文章:

java - 如何在删除 fragment 时动态更新ViewPager

android - 从 ViewPager fragment 隐藏 Android 应用程序中的软键盘

java - 使用单独的应用程序作为依赖项时包不存在

android - 将自定义适配器中的新元素 append 到 ListView

安卓 : How to set an image to an imageview from a url programatically

java - Android 上的 React Native 和 okhttp - 设置用户代理

android-layout - 此 View 不受垂直方向限制。在运行时它会跳到左边,除非你添加垂直约束

java - Android 将两个按钮对齐在同一行

android - 如何让 RecyclerView 停止回收定义的位置?

android - 从 onResume() 方法访问 Viewpager 中的 TextView.setText()