1

反正有没有用无限滚动创建一个有限的Fragment+ ?ViewPager

例如 :

我要创建 10 Fragment,当我滚动到第五个时Fragment,索引号 0 片段被破坏并出现第十一个Fragment

**1** 2 3 4 5 6 7 8 9 10

滚动到第五个后

2 3 4 **5** 6 7 8 9 10 11

多变的

private static final int NUM_PAGES = 10;

这是我的Fragment adapter

private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
    public ScreenSlidePagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        return PageFragment.create(position);
    }

    @Override
    public int getCount() {
        return NUM_PAGES;
    }

}

这是Fragment要显示的以数字 1 开头的内容

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    ViewGroup rootView = (ViewGroup) inflater.inflate(
            R.layout.fragment_view, container, false);

    ((TextView) rootView.findViewById(R.id.tvUsername)).setText(
            ""+ (mPageNumber + 1));
    return rootView;
}

这是所选事件发生的时间

mPager.setOnPageChangeListener(new OnPageChangeListener() {

    @Override
    public void onPageSelected(int pageSelected) {
        // TODO Auto-generated method stub
        Log.d(TAG, "selected = " + pageSelected);
        if (pageSelected > (NUM_PAGES - 6)) {
                   //Event do it here?
        }
    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onPageScrollStateChanged(int arg0) {
        // TODO Auto-generated method stub

    }
});

问题:如何创建事件?

我认为可以通过更改position. getItem()但我不知道该怎么做

4

1 回答 1

0

我能想出的唯一方法是只制作三页,当用户滚动到最后一页时,您替换每个片段。像这样的东西:

数组

ArrayList<Integer> positions = new ArrayList<Integer>();

public void initPositions() {
    positions.add(0);
    positions.add(1);
    positions.add(2);
}

适配器

private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int position) {
    return PageFragment.create(positions.get(position));
}

@Override
public int getCount() {
    return 3;
}

}

听者

mPager.setOnPageChangeListener(new OnPageChangeListener() {

    @Override
    public void onPageSelected(int pageSelected) {
        //scrolled to first page
        if(pageSelected == 0) {
                    //Make sure its not unlimited upwards
                    if(positions.get(pageSelected == 0))
                        return;
                    //remove last and insert new at bottom
            positions.remove(2);
            positions.insert(0, positions.get(0) - 1);
        //scrolled to last page
        } else if(pageSelected == 2) {
                    //remove first and add one last
            positions.remove(0);
            positions.add((positions.get(1) + 1) % NUM_PAGES);
        }
            //make sure the adapter understands
            mAdapter.notifyDataSetChanged();
            //move pager to the middle again
            mPager.setCurrentItem(1);
    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onPageScrollStateChanged(int arg0) {
        // TODO Auto-generated method stub

    }
    });

不确定这将如何在性能方面发挥作用,或者它是否会起作用,但试一试:)

于 2013-03-03T08:12:54.063 回答