英文:
Don't save fragment state in ViewPager2
问题
我正在使用ViewPager2
与FragmentStateAdapter
,如果我有三个片段,默认行为是保存片段状态,实际上我只有一个片段,但创建实例并传递不同类型给它,所以如果我有Fragment A、A1和A2,然后从A1滚动到A,然后再回到A,适配器不会重新创建A,而是使用旧的A实例,这就是发生在我身上的情况。
但是,我不想要这种行为,我希望适配器在使用上一个实例之前创建一个新的片段A实例,然后如果我滚动到B,创建一个新的B实例,而不是使用上一个实例。
public class JobsViewPagerAdapter extends FragmentStateAdapter {
private List<Integer> fragmentsType = new ArrayList<Integer>(){
add(JobsType.Booked.numValue);
add(JobsType.INVITED.numValue);
add(JobsType.PAST.numValue);
};
public JobsViewPagerAdapter(@NonNull FragmentActivity fragmentActivity) {
super(fragmentActivity);
}
@NonNull
@Override
public Fragment createFragment(int position) {
return JobsInnerFragment.newInstance(fragmentsType.get(position));
}
@Override
public int getItemCount() {
return fragmentsType.size();
}
public enum JobsType {
Booked(0), TODAY(1), INVITED(2), UPCOMING(3), PAST(4);
private int numValue;
JobsType(int value) {
this.numValue = value;
}
public int getNumValue() {
return numValue;
}
}
}
public static JobsInnerFragment newInstance(int jobsType){
JobsInnerFragment jobFragment = new JobsInnerFragment();
Bundle todayJobsBundle = new Bundle();
todayJobsBundle.putInt(Constants.JOBS_TYPE, jobsType);
jobFragment.setArguments(todayJobsBundle);
return jobFragment;
}
英文:
I am using ViewPager2
with FragmentStateAdapter
and if I have three fragments the default behavior is to save fragment state, actually I have one fragment but create instances and pass different types to it, so if I have Fragment A, A1 and A2 and I scroll from a1 to a then back to a the adapter doesn't recreate A but use an old instance of A and that's what is happening to me
But instead, I don't want that behaviour I want the adapter to create a new instance of fragment A instead of using the last instance and then if I scroll to B create a new instance of B and don't use the last instance.
public class JobsViewPagerAdapter extends FragmentStateAdapter {
private List<Integer> fragmentsType = new ArrayList<Integer>(){{
add(JobsType.Booked.numValue);
add(JobsType.INVITED.numValue);
add(JobsType.PAST.numValue);
}};
public JobsViewPagerAdapter(@NonNull FragmentActivity fragmentActivity) {
super(fragmentActivity);
}
@NonNull
@Override
public Fragment createFragment(int position) {
return JobsInnerFragment.newInstance(fragmentsType.get(position));
}
@Override
public int getItemCount() {
return fragmentsType.size();
}
public enum JobsType {
Booked(0), TODAY(1), INVITED(2), UPCOMING(3), PAST(4);
private int numValue;
JobsType(int value) {
this.numValue = value;
}
public int getNumValue() {
return numValue;
}
}
}
public static JobsInnerFragment newInstance(int jobsType){
JobsInnerFragment jobFragment = new JobsInnerFragment();
Bundle todayJobsBundle = new Bundle();
todayJobsBundle.putInt(Constants.JOBS_TYPE, jobsType);
jobFragment.setArguments(todayJobsBundle);
return jobFragment;
}
答案1
得分: 4
尝试将ViewPager
的offscreenPageLimit
设置为1
viewPager2.offscreenPageLimit = 1
来自文档的说明:
设置当前可见页面两侧应保留的页面数量。超出此限制的页面将在需要时从适配器重新创建
英文:
Try setting offscreenPageLimit
to ViewPager
as 1
viewPager2.offscreenPageLimit=1
From documentation
>Set the number of pages that should be retained to either side of the currently visible page(s). Pages beyond this limit will be recreated from the adapter when needed
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论