[英]TabLayout get stuck with viewpager when screen orientation change
I am using the design support library. 我正在使用设计支持库。 I've a Fragment and inside this fragment I have another fragment which has view pager with 3 new fragment
我有一个片段,在这个片段中,我还有另一个片段,其中包含具有3个新片段的分页器
My fragment code - 我的片段代码-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
app:tabIndicatorColor="@android:color/white"
app:tabIndicatorHeight="4dp"
app:tabMode="fixed" />
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Fragment Java code 片段Java代码
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_main, container, false);
TabLayout tabLayout = (TabLayout) v.findViewById(R.id.tab_layout);
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
tabLayout.setTabTextColors(Color.parseColor("#ffffff"), Color.parseColor("#000000"));
ViewPager viewPager = (ViewPager) v.findViewById(R.id.pager);
viewPager.setOffscreenPageLimit(3);
viewPager.setAdapter(new SectionPagerAdapter(getActivity().getSupportFragmentManager()));
tabLayout.setupWithViewPager(viewPager);
return v;
}
public class SectionPagerAdapter extends FragmentPagerAdapter {
public SectionPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new Tab1();
case 1:
return new Tab2();
case 2:
return new Tab3();
default:
return null;
}
}
@Override
public int getCount() {
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Tab 1";
case 1:
return "Tab 2";
case 2:
return "Tab 3";
default:
return "";
}
}
}
Now its all working fine but when I rotate the screen, swipe on viewpager does not work correctly. 现在一切正常,但是当我旋转屏幕时,在viewpager上滑动无法正常工作。 I mean it get stuck in between two tabs.
我的意思是它卡在两个选项卡之间。 Also the tab indicator also get stuck between tabs.
此外,选项卡指示器也卡在选项卡之间。 It takes almost 6-7 swipe to go from 1st to 3rd tab.
从1st到3rd选项卡大约需要6-7次滑动。 Also when swiping after rotating there is only one tab fragment which was previously choose before rotating.
同样,在旋转后滑动时,只有一个制表符片段是先前在旋转之前选择的。
您可以使用FragmentStatePagerAdapter而不是FragmentPagerAdapter
In your onCreateView() method, try changing this: 在您的onCreateView()方法中,尝试更改此方法:
viewPager.setAdapter(new SectionPagerAdapter(getActivity().getSupportFragmentManager()));
to this: 对此:
viewPager.setAdapter(new SectionPagerAdapter(getChildFragmentManager()));
I had the same symptoms as you did, until I made this change. 在做出此更改之前,我的症状与您相同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.