【问题标题】:Swiping the view pager fragment will not move the tabs滑动视图寻呼机片段不会移动选项卡
【发布时间】:2016-07-29 12:08:49
【问题描述】:

我按照这些教程Android TabLayout Example using ViewPager and Fragments Android TabLayout Example

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

这是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>

【问题讨论】:

  • 显示一些代码。 Android Studio 中还有一个默认模板,在创建 Activity 时,选择 Activity with tabs + ViewPager,你会得到一个可以检查和学习的工作代码。
  • 我按照上面的教程完美运行,除了这个问题。当我滑动标签时不会切换为什么?
  • 不知道没有你的代码。没有人会。请分享相关代码(尤其是您声明 TabLayout 的 xml)。
  • 确保在 Activity 中使用 tablayout 设置查看器:tabLayout.setupWithViewPager(yourViewPager);
  • 您发现问题了吗?

标签: android android-layout android-viewpager swipe


【解决方案1】:

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;
    }
}

【讨论】:

    【解决方案2】:
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }
    
            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
    
            }
    
            @Override
            public void onTabReselected(TabLayout.Tab tab) {
    
            }
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多