我想始终保留 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;
}
});
}
寻呼机适配器
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) {}
}