我所拥有的是一个名为FullImageActivity2
的类,它显示图像,我可以在此活动中使用寻呼机适配器在图像之间切换我有一个TextView
,它应该显示每个图像的项目ID和它从字符串数组中获取值。我可以将TextView
与值连接,但是当我翻转图像时值不会改变,我的意思是我希望每个图像都与它的id相关联,当我翻转图像时它应该改变并显示正确的图片。
这是我的活动代码:
public class FullImageActivity2 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_full_image_activity2);
String[] nameArray={"Su6670","SU10670","PB6670","PB10670"};
TextView itemid=(TextView)findViewById(R.id.itemid);
//get intent data
//Selected image id
Integer[] newArray={R.drawable.bottle1,R.drawable.bottle2,R.drawable.bottle3,R.drawable.bottle4};
ImageAdapter imageAdapter = new ImageAdapter(this, newArray);
List<ImageView> images = new ArrayList<ImageView>();
for (int i1 = 0; i1 < imageAdapter.getCount(); i1++) {
ImageView imageView = new ImageView(this);
imageView.setImageResource(imageAdapter.mThumbIds[i1]);
itemid.setText(nameArray[i1]);
images.add(imageView);
}
// Finally create the adapter
ImagePagerAdapter imagePagerAdapter = new ImagePagerAdapter(images);
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
viewPager.setAdapter(imagePagerAdapter);
for (int m=0; m<imagePagerAdapter.getCount();m++){
itemid.setText(nameArray[m]);
}
}
}
这是图像寻呼机适配器代码:
public class ImagePagerAdapter extends PagerAdapter{
private List<ImageView> images;
public ImagePagerAdapter(List<ImageView> images) {
this.images = images;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = images.get(position);
container.addView(imageView);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(images.get(position));
}
@Override
public int getCount() {
return images.size();
}
@Override
public boolean isViewFromObject(View view, Object o) {
// TODO Auto-generated method stub
return view == o;
}
}
它始终在PB10670
中显示值TextView
。我该如何解决这个问题?
答案 0 :(得分:3)
您必须将OnPageChangeListener
设置为ViewPager
viewPager.setOnPageChangeListener(new OnPageChangeListener()
{
@Override
public void onPageSelected(int arg0)
{
itemid.setText(nameArray[arg0]);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2)
{
}
@Override
public void onPageScrollStateChanged(int arg0)
{
}
});
这样,只要您翻转图片,TextView
中的文字就会根据当前位置发生变化。