在下面粘贴的代码中,完美运行,myPagerAdapter类的instantiateItem方法在int myint = 7之后执行;指令。这引起了我的问题,因为我想引用myint = 7指令所在的一些xml页面,但抛出异常,因为当我尝试进行这些引用时,xml的4页还没有膨胀。
instantiateItem被调用了4次,并且它尽职地为每个xml页面充气,但显然是以异步方式完成并且为时已晚。我需要在mint = 7指令(实际上是我的其他计划代码)执行之前同步充气页面。我怎样才能做到这一点?发生了什么事?
谢谢,加里
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PSContext = this;
setContentView(R.layout.main);
MyPagerAdapter adapter = new MyPagerAdapter();
ViewPager myPager = (ViewPager) findViewById(R.id.mysevenpanelpager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(0);
myPager.setOffscreenPageLimit(4);
myPager.setOnPageChangeListener(adapter);
int myint = 7; //just a debug stop
}
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);
答案 0 :(得分:1)
我需要同步充气页面
不支持。
在mint = 7指令(实际上是我的其他计划代码)之前执行
这并不意味着你“需要让页面同步膨胀”。这意味着在PagerAdapter
完成工作后,您需要将此工作延迟到工作队列中的后期。
更换:
mint = 7;
使用:
myPager.post(new Runnable() {
public void run() {
mint = 7;
}
});
可能就够了。
或者,或者从MyPagerAdapter
触发工作,例如在创建页面后对页面进行处理。