增加Viewpager中可见选项卡的数量

时间:2016-01-08 16:42:01

标签: android android-fragments android-viewpager

我在Viewpager中总共有5个片段,我想在当前可见标签的两侧显示2个标签。

示例:我当前的标签是2。我想要标签0,标签1向左和标签3,标签4在右边一次显示。

我尝试设置 viewPager.setOffscreenPageLimit(2),但它没有工作。我仍然看到两边只显示一个标签。我错过了什么?

public class TabsFragment extends Fragment {
    mViewPager = (ViewPager) view.findViewById(R.id.pager);
    titleStrip = (PagerTitleStrip) view.findViewById(R.id.titlestrip);
    adapter = new TabsPagerAdapter(getChildFragmentManager(), getActivity());
    mViewPager.setAdapter(adapter);
    mViewPager.setOffscreenPageLimit(2);
    mViewPager.setCurrentItem(2);
 }

public class TabsPagerAdapter extends FragmentStatePagerAdapter {

private Context context;

public TabsPagerAdapter(FragmentManager fm, Context c) {
    super(fm);
    context = c;
}

@Override
public Fragment getItem(int index) {

    switch (index) {

        case 0:
            return new FirstFrag();
        case 1:
            return new SecondFrag();
        case 2:
            return new ThirdFrag();
        case 3:
            return new FourthFrag();
        case 4:
            return new FifthFrag();
        default:
            return new SecondFrag();
    }

}

@Override
public int getCount() {
    // get item count - equal to number of tabs
    return 5;
}
}

2 个答案:

答案 0 :(得分:3)

使用TabLayout解决了这个问题。为未来的读者发布代码。

  

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Light">

   <android.support.v7.widget.Toolbar
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/awesome_toolbar"
        android:layout_width="match_parent"
        android:layout_height="74dp"
        android:background="#ff31353f"
        android:paddingTop="20dp"
        app:popupTheme="@style/AppTheme"
        app:theme="@style/ToolbarTheme"/>

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="scrollable"/>
</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
public class TabsFragment extends Fragment {
 @Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    mViewPager = (ViewPager) view.findViewById(R.id.pager);
    tabLayout = (TabLayout)view.findViewById(R.id.tabs);
    adapter = new TabsPagerAdapter(getChildFragmentManager(), getActivity());
    adapter.addFragment(new FirstFrag(), getResources().getString(R.string.frag1));
    adapter.addFragment(new SecondFrag(), getResources().getString(R.string.frag2));
    adapter.addFragment(new ThirdFrag(), getResources().getString(R.string.frag3));
    adapter.addFragment(new FourthFrag(), getResources().getString(R.string.frag4));
    adapter.addFragment(new FifthFrag(), getResources().getString(R.string.frag5));
    mViewPager.setAdapter(adapter);
    mViewPager.setOffscreenPageLimit(2);
    mViewPager.setCurrentItem(2);
    tabLayout.setupWithViewPager(mViewPager);
}

答案 1 :(得分:0)

您需要修改ViewPager适配器。覆盖public Fragment getItem(int position)方法,并创建一个自定义Fragment,其中包含当前片段和左侧位置-1,右侧位置+ 1的片段。您可能还必须覆盖public int getCount()方法。我以前没有尝试这样的实现,但我认为这是解决这个问题的方法。