以下代码可以很好地滚动 4 页。我的问题是onPageScrolled
滚动页面时不会触发事件。无论我是否使用该myPager.SetOffsccreenPageLimit(4)
命令,都会发生这种情况。阅读文档后,我的理解是,当我将事件implements ViewPager.OnPageChangeListener
包含在MyPagerAdapter
课程中时,应该触发该事件。我究竟做错了什么?
谢谢,加里布莱克利
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MyPagerAdapter adapter = new MyPagerAdapter();
ViewPager myPager = (ViewPager) findViewById(R.id.mysevenpanelpager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(0);
myPager.setOffscreenPageLimit(4);
}
private class MyPagerAdapter extends PagerAdapter
implements ViewPager.OnPageChangeListener {
public int getCount() {
return 4;
}
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resId = 0;
switch (position) {
case 0:
resId = R.layout.left;
break;
case 1:
resId = R.layout.gps;
break;
case 2:
resId = R.layout.map;
break;
case 3:
resId = R.layout.right;
break;
}
View view = inflater.inflate(resId, null);
((ViewPager) collection).addView(view, 0);
return view;
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
@Override
public void onPageScrollStateChanged(int arg0) {
int myint = 9;
myint = myint * 2;
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
int myint = 9;
myint = myint * 2;
}
@Override
public void onPageSelected(int arg0) {
int myint = 9;
myint = myint * 2;
}
}