当fragment的个数大于2时,在fragment点击跳转到别的Acitivity再返回,fragment里面的内容会消失。
解决:
第一步:适配器继承FragmentStatePagerAdapter,并且destroyItem()方法里面删掉super()
public class MyViewPagerAdapter extends FragmentStatePagerAdapter {
Context context;
Fragment[] mFragmentArrays;
String[] mTabTitles;
public MyViewPagerAdapter(FragmentManager fm,Context context,Fragment[] mFragmentArrays,String[] mTabTitles) {
super(fm);
this.context=context;
this.mFragmentArrays=mFragmentArrays;
this.mTabTitles=mTabTitles;
}
@Override
public Fragment getItem(int position) {
return mFragmentArrays[position];
}
@Override
public int getCount() {
return mFragmentArrays.length;
}
@Override
public CharSequence getPageTitle(int position) {
return mTabTitles[position];
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
}
}
第二步:在每个fragment的xml里面添加多一层ScrollView(我这里只需要一个RecyclerView)
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:overScrollMode="never" />
</LinearLayout>
</ScrollView>
|