添加/删除PagerAdapter/ViewPager问题Android的视图



我想一直保留一个包含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) {}

}
默认情况下,instantiateItem()方法在内存中创建两个视图页。因此,当您滑动到第二页时,0页将被重新创建,因为它不在内存中保存的2页的范围内。请尝试使用
myViewPager.setOffscreenPageLimit(numberOfPages) 

方法,该方法接收一个整数作为参数,并声明在回收之前应该保留多少页。

最新更新