有无论如何使用无限滚动创建有限的Fragment
+ ViewPager
吗?
例如:
我要创建10 Fragment
,当我滚动到第五个Fragment
时,索引号0片段被销毁并出现第11个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;
}
}
以下是显示以数字1开头的Fragment
的包含
@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()
值来完成。
但我不知道该怎么做
答案 0 :(得分: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
}
});
不确定这在性能方面是如何工作的,或者它是否会起作用,但是试一试:)