最近使用viewpager的时候遇到一个问题,viewpager设置过pagerAdapter之后,当需要修改viewpager的数据时,我这里是删除item,使用pagerAdapter.notifyDataSetChanged方法似乎并没有完全生效。
我的Adapter如下
public class ViewPagerAdapter extends PagerAdapter {
private Context context;
private List<String> imgList; //图片的数据源
public ViewPagerAdapter(Context context, List<String> imgList) {
this.context = context;
this.imgList = imgList;
}
@Override
public int getCount() {
return imgList.size();
}
//判断当前的View 和 我们想要的Object(值为View) 是否一样;返回 true/false
@Override
public boolean isViewFromObject(View view, Object object) {
return (view == object);
}
//instantiateItem():将当前view添加到ViewGroup中,并返回当前View
@Override
public Object instantiateItem(ViewGroup container, int position) {
View itemView = getItemView(R.layout.view_pager_img);
PhotoView imageView = (PhotoView) itemView.findViewById(R.id.img_iv);
Glide.with(context).load(imgList.get(position)).into(imageView);
container.addView(itemView);
return itemView;
}
//destroyItem():删除当前的View;
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
private View getItemView(int layoutId) {
View itemView = LayoutInflater.from(this.context).inflate(layoutId, null, false);
return itemView;
}
}
后来在stackoverflow找到答案
http://stackoverflow.com/questions/8060904/add-delete-pages-to-viewpager-dynamically
原来是因为这个PagerAdapter中有个默认方法getItemPosition();我们在Adapter中重写这个方法,如下
/**
* Called when the host view is attempting to determine if an item's position
* has changed. Returns {@link #POSITION_UNCHANGED} if the position of the given
* item has not changed or {@link #POSITION_NONE} if the item is no longer present
* in the adapter.
*
* <p>The default implementation assumes that items will never
* change position and always returns {@link #POSITION_UNCHANGED}.
*
* @param object Object representing an item, previously returned by a call to
* {@link #instantiateItem(View, int)}.
* @return object's new position index from [0, {@link #getCount()}),
* {@link #POSITION_UNCHANGED} if the object's position has not changed,
* or {@link #POSITION_NONE} if the item is no longer present.
*/
public int getItemPosition(Object object) {
return POSITION_UNCHANGED;
}
大概意思是这样的:该方法默认的返回值是POSITION_UNCHANGED,也就是说即使你删了list中的一个item,positon任然没有改变。如果把返回值改成#POSITION_NONE之后会更新数据源了,所以解决办法如下。
public class ViewPagerAdapter extends PagerAdapter {
private Context context;
private List<String> imgList; //图片的数据源
public ViewPagerAdapter(Context context, List<String> imgList) {
this.context = context;
this.imgList = imgList;
}
//解决ViewPager数据源改变时,刷新无效的解决办法
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
@Override
public int getCount() {
return imgList.size();
}
//判断当前的View 和 我们想要的Object(值为View) 是否一样;返回 true/false
@Override
public boolean isViewFromObject(View view, Object object) {
return (view == object);
}
//instantiateItem():将当前view添加到ViewGroup中,并返回当前View
@Override
public Object instantiateItem(ViewGroup container, int position) {
View itemView = getItemView(R.layout.view_pager_img);
PhotoView imageView = (PhotoView) itemView.findViewById(R.id.img_iv);
Glide.with(context).load(imgList.get(position)).into(imageView);
container.addView(itemView);
return itemView;
}
//destroyItem():删除当前的View;
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
private View getItemView(int layoutId) {
View itemView = LayoutInflater.from(this.context).inflate(layoutId, null, false);
return itemView;
}
}