-1
我想知道如何更新ViewPager視圖的任何元素而無需重新啓用適配器。我正在使用PagerAdapter而不是FragmentStatePagerAdapter。如何更新ViewPager的元素(如按鈕)?
尋呼機適配器的代碼是這樣的:
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);
}
}
現在,當我點擊了最愛的ImageButton,什麼都不會發生在任一頁面,但在最後一頁的最愛按鈕變化圖片。我認爲尋呼機無法給出被點擊項目的當前位置。
任何猜測?