片段onCreateView被多次调用,以便每次都创建片段的新实例

时间:2019-09-12 04:54:29

标签: android android-fragments android-viewpager android-viewpager2 fragment-oncreateview

我正在尝试在视图分页器的页面更改中添加片段。但是每次我滚动视图分页器时,都会创建新的片段瞬间。

public void initializeViewPager(int currentPage, String selectedCardNumber) {
    currentPosition = currentPage;
    FragmentPagerAdapter journeyViewPagerAdapter =
            new ViewPagerAdapter(getSupportFragmentManager());


    ViewPager.OnPageChangeListener onPageChangeListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            //Default Implementation
        }

        @Override
        public void onPageSelected(int position) {
            currentPosition = position;
            addFragments(position, selectedCardNumber);
            handleScrollState();
        }

        @Override
        public void onPageScrollStateChanged(int state) {
            //Default Implementation
        }
    };

    viewPager.addOnPageChangeListener(onPageChangeListener);
    initCarousalAdapter();

}

addFragments()方法

private void addFragments(int position, final String selectedCardNumber) {

    FragmentTransaction fragTransaction = getSupportFragmentManager().beginTransaction();
    Bundle bundle = new Bundle();
    bundle.putInt(ApplicationConstants.SELECTED__TYPE, position);
    bundle.putString(DataConstants.SELECTED__NUMBER, selectedCardNumber);

    switch (position) {
        case 1:
            OffersFragmentOne fragment = OffersFragmentOne.getInstance();
            fragment.setArguments(bundle);
            fragTransaction.replace(R.id.btmt_fragment_container, fragment);
            break;

        case 2:
            OffersFragmentTwo mtFragment = OffersFragmentTwo.getInstance();
            mtFragment.setArguments(bundle);
            fragTransaction.replace(R.id.btmt_fragment_container, mtFragment);
            break;

        case 3:
            OffersFragmentThree poFragment = OffersFragmentThree.getInstance();
            poFragment.setArguments(bundle);
            fragTransaction.replace(R.id.btmt_fragment_container, poFragment);
            break;
        default:
            //Default Implementation
    }

    fragTransaction.commit();
}

getInstance()方法

private static OffersFragmentOne offersFragmentOne;

public static OffersFragmentOne getInstance() {
    if (OffersFragmentOne == null) {
        offersFragmentOne = new OffersFragmentOne();
    }
    return offersFragmentOne;
}

ViewPagerAdapter.java

public class ViewPagerAdapter extends FragmentPagerAdapter {

private final int[] carousalImages = {R.drawable.ic_po_carousal_banner, 
R.drawable.ic_bt_carousal_banner,
    R.drawable.ic_mt_carousal_banner, R.drawable.ic_po_carousal_banner, 
R.drawable.ic_bt_carousal_banner};

public ViewPagerAdapter(FragmentManager fragmentManager) {
    super(fragmentManager);
}

@Override
public Fragment getItem(int position) {
    CarousalFragment frag = new CarousalFragment();
    CarousalFragmentPresenter.createAndAttach(frag, carousalImages[position]);
    return frag;
}

@Override
public int getCount() {
    return carousalImages != null ? carousalImages.length : 0;
}
}

2 个答案:

答案 0 :(得分:0)

如何在视图分页器的容器活动中创建片段实例?这样,仅当创建容器时才应创建它们。

编辑:添加示例代码。 示例:

public class ContainerActivity extends AppCompatActivity {
    private FragmentOne fragmentOne;
    private FragmentTwo fragmentTwo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.container_with_view_pager);
        fragmentOne = FragmentOne.getInstance();
        fragmentTwo = FragmentTwo.getInstance();

        ViewPager viewPager = findViewById(R.id.viewpager);
        setupViewPager(viewPager);
    }

    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFragment(fragmentOne, "Fragment One");
        adapter.addFragment(fragmentTwo, "Fragment Two");
        viewPager.setAdapter(adapter);
    }
}

答案 1 :(得分:0)

您应该在getItem中编写切换用例,而不需要bundle和分段事务,因为getItem本身返回选择的Fragment并将与{{1}绑定}

ViewPager

@Override public Fragment getItem(int position) { switch (position) { case 0: CarousalFragment(); break; case 1: OffersFragmentOne.getInstance(); break; case 2: OffersFragmentTwo.getInstance(); break; case 3: OffersFragmentThree.getInstance(); break; default: //Default Implementation } } 仅提供选定和滚动项或片段的位置,因此不需要从OnPageChangeListener中添加片段

onPageSelected