我遵循了这些教程 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/