不要在ViewPager2中保存片段状态。

huangapple go评论72阅读模式
英文:

Don't save fragment state in ViewPager2

问题

我正在使用ViewPager2FragmentStateAdapter,如果我有三个片段,默认行为是保存片段状态,实际上我只有一个片段,但创建实例并传递不同类型给它,所以如果我有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&lt;Integer&gt; fragmentsType = new ArrayList&lt;Integer&gt;(){{
    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

尝试将ViewPageroffscreenPageLimit设置为1

viewPager2.offscreenPageLimit = 1

来自文档的说明:

设置当前可见页面两侧应保留的页面数量。超出此限制的页面将在需要时从适配器重新创建

https://developer.android.com/reference/androidx/viewpager2/widget/ViewPager2#setOffscreenPageLimit(int)

英文:

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

https://developer.android.com/reference/androidx/viewpager2/widget/ViewPager2#setOffscreenPageLimit(int)

huangapple
  • 本文由 发表于 2020年8月15日 10:26:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/63422015.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定