我正在尝试在pageradapter
课堂外更新图像,但它不起作用。我已经通过其他方式做到了,但这样做效率不高。
int pos = mPager.getCurrentItem();
mAdapter.notifyDataSetChanged();
mPager.setAdapter(mAdapter);
mPager.setCurrentItem(pos);
显示它的效果需要很多时间,任何其他方式都将受到高度赞赏。我有将近 20 多张图像,所以我无法删除和恢复视图。我正在尝试从options menu
. 期待答案或建议。谢谢!
这是我的代码
public class pageAdapter extends PagerAdapter {
@Override
public int getCount() {
// TODO Auto-generated method stub
return Global.imageList.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
// TODO Auto-generated method stub
return view.equals(object);
}
@Override
public Object instantiateItem(View container, final int position) {
// TODO Auto-generated method stub
View v = getLayoutInflater().inflate(R.layout.pager_item, null);
ImageView ivBackground = (ImageView) v
.findViewById(R.id.pager_image);
final ImageView ivFavorite = (ImageView) v
.findViewById(R.id.fav_image);
ivFavorite.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (!helper.isExist(Global.imageList[position])) {
int i = helper.insertFavourites(Global.imageList[position]);
if (i != -1) {
ivFavorite.setImageResource(R.drawable.fav_selected);
}
} else {
if (helper.removeFavorite(Global.imageList[position])) {
ivFavorite .setImageResource(R.drawable.fav_unselected);
}
}
}
});
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
Global.imageList[position]);
ivBackground.setImageBitmap(bitmap);
((ViewPager) container).addView(v, 0);
return v;
}
@Override
public void destroyItem(View container, int position, Object object) {
// TODO Auto-generated method stub
((ViewPager) container).removeView((View) object);
}
}