标签:android pageadapter 适配器无法刷新
如果item的位置如果没有发生变化,则返回POSITION_UNCHANGED。如果返回了POSITION_NONE,表示该位置的item已经不存在了。默认的实现是假设item的位置永远不会发生变化,而返回POSITION_UNCHANGED
所以我们可以尝试着修改适配器的写法,覆盖getItemPosition()方法,当调用notifyDataSetChanged时,让getItemPosition方法人为的返回POSITION_NONE,从而达到强迫viewpager重绘所有item的目的。
private class MyPageAdapter extends PagerAdapter {
private List<View> list;
private int mChildCount = 0;
private MyPageAdapter(List<View> list) {
this.list = list;
}
@Override
public void destroyItem(View view, int position, Object ob) {
ViewPager pViewPager = ((ViewPager) view);
pViewPager.removeView(list.get(position));
}
@Override
public void finishUpdate(View v) {
}
@Override
public int getCount() {
return list.size();
}
public void setsize(List<View> mlist)
{
list=mlist;
}
@Override
public Object instantiateItem(View view, int position) {
ViewPager pViewPager = ((ViewPager) view);
pViewPager.addView(list.get(position));
return list.get(position);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
}
@Override
public Parcelable saveState() {
return null;
}
@Override
public void startUpdate(View v) {
}
// 重点是加上下面的代码就可以了。
@Override
public void notifyDataSetChanged() {
mChildCount = getCount();
super.notifyDataSetChanged();
}
@Override
public int getItemPosition(Object object) {
if ( mChildCount > 0) {
mChildCount --;
return POSITION_NONE;
}
return super.getItemPosition(object);
}
}
标签:android pageadapter 适配器无法刷新
原文地址:http://blog.csdn.net/huningjun/article/details/46446223