什麼我的代碼呢:滑動操作的圖像(不是佈局)與viewpager
這裏是我的代碼這對於viewpager該XML佈局之間揮筆(名爲left.xml,right.xml和center.xml)。
我想要它做的
我想圖像(存儲在繪製文件夾)之間的刷卡什麼。當我用R.drawable.image替換R.layout.xml時,我的應用崩潰了。有人能幫我弄清楚嗎?
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyPagerAdapter adapter = new MyPagerAdapter();
ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(0);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
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.left;
break;
case 1:
resId = R.layout.center;
break;
case 2:
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 boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
@Override
public Parcelable saveState() {
return null;
}
}
}
謝謝,但它仍然無法正常工作,我嘗試了xml版本的imageview以及動態圖像視圖。沒有我有很多的圖像顯示,所以我不能爲此寫一個XML文件。 – 2012-07-24 23:37:12
我忘了1行代碼:'((ViewPager)collection).addView(img,0);' – 10s 2012-07-24 23:46:11
我只是想說,歡呼! – 2012-07-24 23:47:49