我目前已經實現了一個viewpager,可以順暢地翻轉三種不同的佈局 - 所有的工作都很好。問題出現在我嘗試訪問它們內部佈局的元素時,我不知道從哪裏開始?甚至可以viewflipper做到這一點?我是這麼認爲的,因爲它是在marketpalce中完成的?Android:Viewpager或Viewflipper?
活動:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//pagerviwer
MyPagerAdapter adapter = new MyPagerAdapter();
ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(1);
PageAdapter:
class MyPagerAdapter extends PagerAdapter {
public int getCount() {
return 3;
}
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.training_topics;
break;
case 1:
resId = R.layout.behaviour_topics;
break;
case 2:
resId = R.layout.tricks_topics;
break;
}
View v = inflater.inflate(resId, null);
((ViewPager) collection).addView(v, 0);
return v;
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
@Override
public Parcelable saveState() {
return null;
}
}
實施例: 說,例如,如果我試圖訪問內部traning_topics.xml一個ImageView的?通過主要活動來做到這一點只會讓它崩潰。
這完全正確!完美的工作,我會稍後嘗試這個代碼,並讓你知道!謝謝! – Jonno 2012-01-29 01:30:19