我想随时保留3个观点的列表。应用程序从位置1开始(位置0,1,2之外)。当有人滚动到位置0时,我想删除视图2,并在位置0之前创建一个视图。这样,对用户来说,它是无限的视图。以同样的方式,当有人滚动到位置2时,我想删除位置0处的视图并在末尾添加一个。
但是我在添加和删除视图方面遇到了问题。当我到达位置0时,除非我尝试滚动到位置0(到位置-1,即边界被击中),否则没有任何变化。那时,我可以看到它是我的视图的边界,但然后setCurrentItem(1,false)
被触发,我被带回到视图的中间。当我滚动到位置2时,我看到位置2已更新。但是位置0和1保持不变。
当我滚动到位置2时,没有任何反应。但是,如果我尝试滚动到边界,由于某种原因,位置0会更新并触发setCurrentItem(1,false)
。
我不知道为什么会这样发生。任何人都可以对此有所了解吗?
这是我的代码:
public class MainActivity extends Activity {
ArrayList<Integer> showThree = new ArrayList<Integer>();
int focusedPage = 0;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showThree.add(0,5); //adding integers 5,6,7 for positions 0,1,2
showThree.add(1,6);
showThree.add(2,7);
final MyPagerAdapter adapter = new MyPagerAdapter(getApplicationContext(),showThree);
final ViewPager myPager = (ViewPager) findViewById(R.id.mypanelpager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(1);
myPager.setOnPageChangeListener(new OnPageChangeListener(){
@Override
public void onPageScrollStateChanged(int state) {
if (state == ViewPager.SCROLL_STATE_IDLE) {
//when position= 0, change the 3 views from 5,6,7 to 4,5,6.
if (focusedPage == 0) {
showThreeMonths.set(0,4);
showThreeMonths.set(1,5);
showThreeMonths.set(2,6);
adapter.notifyDataSetChanged();
adapter.startUpdate(myPager);
}
else if (focusedPage ==2){
//ignore, just testing focusPage=0 for now }
}
//set current page to the middle of the 3 new views, which would be
//the same view at position 0 of the old 3 views.
//Thus user doesn't experience the views changing despite being 3 new views.
myPager.setCurrentItem(1,false);
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
// TODO Auto-generated method stub
}
@Override
public void onPageSelected(int position) {
focusedPage = position;
}
});
}
PagerAdapter
public class MyPagerAdapter extends PagerAdapter {
private ArrayList<Integer> showThreeMonths;
private Context ctx;
public MyPagerAdapter (Context ctx, ArrayList<Integer> showThree){
this.ctx = ctx ;
this.showThree = showThree;
}
@Override
public int getCount() {
return showThree.size();
}
public Object instantiateItem(ViewGroup collection, int position ){
//NewCustomView is a class I made that takes parameters context and an integer and creates a view based on the integer
NewCustomView MyOwnView = new NewCustomView(ctx, showThree.get(position));
View customViewLayout = MyOwnView.newLayout; //part of the class object
collection.addView(customViewLayout);
return customViewLayout;
}
@Override
public void destroyItem(ViewGroup collection, int position, Object arg2) {
((ViewPager) collection).removeView((ViewGroup) arg2);}
@Override
public Parcelable saveState() {
return null;}
@Override
public boolean isViewFromObject(View view, Object arg1) {
return view==arg1;}
@Override
public void startUpdate(ViewGroup collection) {}
@Override
public void finishUpdate(ViewGroup collection) {}
}
答案 0 :(得分:1)
默认情况下,instantiateItem()方法在内存中创建2个视图页面。因此,当您滑动到第二页时,将重新创建0页,因为它超出了内存中保存的2页的范围。请尝试使用
myViewPager.setOffscreenPageLimit(numberOfPages)
接收整数作为参数的方法,并声明在回收它之前应该保留多少页。