我正在使用Viewpager和PagerAdapter创建一个包含图像和类似按钮的图库。请参考我的代码:
public class GalleryAdapter extends PagerAdapter {
private Activity _activity;
private ArrayList<MyPhotoItem> _imageCollection;
// Components in fullsceen layout
ImageView imgDisplay;
private ToggleButton btnLike;
// Variable for btnLike
private boolean isChecked;
// constructor
public GalleryAdapter(Activity activity,
ArrayList<MyPhotoItem> images) {
this._imageCollection = images;
}
@Override
public int getCount() {
return this._imageCollection.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((RelativeLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
inflater = (LayoutInflater) _activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View viewLayout = inflater.inflate(R.layout.layout_gallery_image,
container, false);
imgDisplay = (ImageView) viewLayout
.findViewById(R.id.layout_gallery_imgDisplay);
Picasso.with(this._activity.getApplicationContext())
.load(_imageCollection.get(position).url).into(imgDisplay);
btnLike = (ToggleButton) viewLayout
.findViewById(R.id.layout_gallery_likeButton);
btnLike.setOnCheckedChangeListener(null);
btnLike.setChecked(isChecked);
btnLike.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
isChecked = true;
} else {
isChecked = false;
}
}
});
((ViewPager) container).addView(viewLayout);
return viewLayout;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((RelativeLayout) object);
}
@Override
public int getItemPosition(Object object) {
return PagerAdapter.POSITION_NONE;
}
}
我当前的问题:当我滑动到另一个页面并返回之前的页面时,Viewpager不会保持切换按钮状态(假设在Viewpager中加载了5张照片)。我经历了几个Stackoverflow帖子,但没有一个能正确解决我的问题。
任何建议将不胜感激。提前谢谢。
答案 0 :(得分:1)
感谢您的回复。虽然他们中的大部分都不是我个案的直接答案,但他们给了我一些线索来解决我的问题。这可能不是最佳解决方案,但它解决了我的问题。
解决方案:
1 /添加一个标志以保持对象中Toggle的状态:
public class MyPhotoItem {
...
public boolean like_flag;
...
}
2 /自定义一下OnCheckedChangeListener:
class OnLikeCheckedChangeListener implements OnCheckedChangeListener {
int mPosition;
// constructor
public OnLikeCheckedChangeListener(int position) {
this.mPosition = position;
}
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked){
_imageCollection.get(_position).like_flag = true;
notifyDataSetChanged();
}
else{
_imageCollection.get(_position).like_flag = false;
notifyDataSetChanged();
}
}
}
3 /根据标志设置切换状态:
@Override
public Object instantiateItem(ViewGroup container, int position) {
...
// Init Toggle status
if (_imageCollection.get(position).like_flag) {
btnLike.setChecked(true);
} else {
btnLike.setChecked(false);
}
btnLike.setOnCheckedChangeListener(new OnLikeCheckedChangeListener(
position));
...
}
那就是它。在滑动Viewpager期间,Toggle状态会保持不变。任何人都有更好的解决方案或良好做法,请随时发表评论。
享受编码!
答案 1 :(得分:0)
如果ViewPager中只有5张图片可以尝试使用setOffscreenPageLimit (int limit)
答案 2 :(得分:0)
这是因为您在更改页面时不断回收寻呼机的视图,因此页面中的视图会不断重新创建,并且为此您丢失了ToggleButton的状态,解决方案是将其保存为状态当ToggleButton的状态改变并在instantiateItem方法上检索它时,SharedPreferences,并将状态设置为ToggleButton。