我在可绘制文件夹中有多个图像,但我只希望根据之前 ListView 活动中单击的项目加载某些图像。所以我正在尝试学习如何延迟加载图像而不是将它们硬编码到我的 xml 文件中。我是一个新手,并试图自学来自 ios 开发背景的 android。提前感谢您的帮助。
私有类 MyPagerAdapter 扩展 PagerAdapter {
public int getCount(){
return 3;
}
public Object instantiateItem(View collection, int position){
LayoutInflater inflater = (LayoutInflater)collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ImageView iv1 = (ImageView) findViewById(R.id.imageView1);
ImageView iv2 = (ImageView) findViewById(R.id.imageView2);
ImageView iv3 = (ImageView) findViewById(R.id.imageView3);
int resId = 0;
switch(position){
case 0:
iv1.setImageResource(R.drawable.amersenna);
resId = R.layout.left;
break;
case 1:
iv2.setImageResource(R.drawable.amersenna);
resId = R.layout.middle;
break;
case 2:
iv3.setImageResource(R.drawable.amersenna);
resId = R.layout.right;
break;
}
View view = inflater.inflate(resId, null);
((ViewPager) collection).addView(view, 0);
return view;
}
@Override
public void destroyItem(View agr0, int agr1, Object agr2) {
((ViewPager) agr0).removeView((View)agr2);
}
@Override
public boolean isViewFromObject(View agr0, Object agr1){
return agr0 == ((View)agr1);
}
@Override
public Parcelable saveState(){
return null;
}
}