gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 08:38:24 25 4
gpt4 key购买 nike

我在 Viewpager 中总共有 5 个 fragment ,我想在当前可见选项卡的两侧显示 2 个选项卡。

示例:我当前的选项卡是 2。我希望同时显示左侧的选项卡 0、选项卡 1 和右侧的选项卡 3、选项卡 4。

我尝试设置 viewPager.setOffscreenPageLimit(2) 但没有成功。我仍然看到两边只显示 1 个选项卡。我错过了什么?

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

最佳答案

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

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

关于android - 增加 Viewpager 中可见选项卡的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34681939/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com