1

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

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;
}
}
4

2 回答 2

0

您应该将您的 switch case 写入getItem并且不需要bundle和分段事务,因为getItem它本身会返回 selectedFragment并将绑定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

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

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {
        //Default Implementation
    }
};
于 2019-09-12T05:21:08.460 回答
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);
    }
}
于 2019-09-12T05:11:27.960 回答