我想知道如何在不恢复适配器的情况下更新ViewPager视图的任何元素。我使用的是PagerAdapter而不是FragmentStatePagerAdapter。
寻呼机适配器的代码如下:
public class PagerData extends PagerAdapter {
private Context context;
private List<ParseUser> list;
private boolean[] likes = {true, false, true, false, false};
private static OnDataChangedListener odl;
public PagerData(Context context, List<ParseUser> list) {
this.context = context;
this.list = list;
}
@Override
public int getCount() {
if (list == null)
return 0;
return list.size();
}
private TextView reason, amount;
private ImageButton fav, bookmark;
@Override
public Object instantiateItem(final ViewGroup container, final int position) {
View v = LayoutInflater.from(context).inflate(R.layout.pager_data, null, false);
fav = (ImageButton) v.findViewById(R.id.fav);
startLikes(position);
final ParseRelation<ParseUser> relation = ParseUser.getCurrentUser().getRelation("favorites");
fav.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setLike(position, container);
//relation.add(ParseUser.getCurrentUser());
//ParseUser.getCurrentUser().saveEventually();
}
});
container.addView(v, 0);
return v;
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
private void setLike(int position, ViewGroup cont) {
if (!likes[position]) {
fav.startAnimation(AnimationUtils.loadAnimation(context, R.anim.fav_rotate));
fav.setImageResource(R.mipmap.ic_favorite);
likes[position] = true;
} else {
fav.setImageResource(R.mipmap.ic_favorite_border);
likes[position] = false;
}
}
private void startLikes(int position) {
if (!likes[position]) {
fav.setImageResource(R.mipmap.ic_favorite_border);
} else {
fav.setImageResource(R.mipmap.ic_favorite);
}
}
public interface OnDataChangedListener {
void onDataChanged();
}
public static void setOnDataChangedListener(OnDataChangedListener od) {
odl = od;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return (view == object);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
现在当我点击fav ImageButton时,任何页面上都没有任何反应,但在最后一页上,fav按钮会改变图像。我认为传呼机无法给出点击项目的当前位置。
任何猜测?
答案 0 :(得分:0)
致电fav.invalidate()
后尝试使用setLike(position, container)
。另外,据我所知,您应该将图像资源放在drawable文件夹中。 Mipmap用于启动器图标。