android baseadapter 移除某个item,Android开发 ViewPager删除Item后,不会更新数据和View

问题描述:

在使用ViewPager的适配器删除适配器里一个Item后依然会,而删除的这个item依然会保留缓存,适配器不会重新加载更新数据.如下代码:

public classTReleaseCircleZoomablePagerAdapter extends PagerAdapter {private List mImageUrlList = new ArrayList<>();public void removeItem(intposition){

mImageUrlList.remove(position);

notifyDataSetChanged();

}//....略

在这个代码里removeItem(intposition)是希望清理适配器List里的一项item,并且重新刷新.但是其实真实情况是并不会重新刷新...... 在滑动超过3个页面后才会让你那个删除item消失.

解决办法:

依然在这个适配器里重写public int getItemPosition(@NonNull Object object)方法

public classTReleaseCircleZoomablePagerAdapter extends PagerAdapter {private List mImageUrlList = new ArrayList<>();public void removeItem(intposition){

mImageUrlList.remove(position);

notifyDataSetChanged();

}

@Overridepublic int getItemPosition(@NonNull Object object) {//return super.getItemPosition(object);

returnPOSITION_NONE;

}//略......

注意关键点是返回 POSITION_NONE

为什么呢?

为什么我改了这个之后就可以在删除item后更新view了呢?好奇心害死猫,我们进一步探究一下.

首先我们看看 return super.getItemPosition(object); 父类里写的返回值到底里面有啥

/*** 在主视图尝试确定项目的位置时调用

*已经改变。 如果给定的位置,则返回{@link #POSITION_UNCHANGED}

*如果适配器中不再存在该项,则item未更改或{@link #POSITION_NONE}。

*

*

The default implementation assumes that items will never

* change position and always returns {@link#POSITION_UNCHANGED}.

*

*@paramobject Object representing an item, previously returned by a call to

* {@link#instantiateItem(View, int)}.

*@returnobject'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 intgetItemPosition(@NonNull Object object) {returnPOSITION_UNCHANGED;

}

很好上面的意思是

如果视图item已经是加载过的,就使用 POSITION_UNCHANGED 来标识,表示这个item不需要重复更新

如果item在适配器里不存在,就说item未加载过,就使用 POSITION_NONE 来标识,标识这个item需要更新一次

接着继续探究下ViewPager是怎么调用getItemPosition()的

voiddataSetChanged() {//This method only gets called if our observer is attached, so mAdapter is non-null.

final int adapterCount =mAdapter.getCount();

mExpectedAdapterCount=adapterCount;boolean needPopulate = mItems.size() < mOffscreenPageLimit * 2 + 1

&& mItems.size()

}if (newPos ==PagerAdapter.POSITION_NONE) {

mItems.remove(i);

i--;if (!isUpdating) {

mAdapter.startUpdate(this);

isUpdating= true;

}

mAdapter.destroyItem(this, ii.position, ii.object);

needPopulate= true;if (mCurItem ==ii.position) {//Keep the current item in the valid range

newCurrItem = Math.max(0, Math.min(mCurItem, adapterCount - 1));

needPopulate= true;

}continue;

}

上面的if (newPos ==PagerAdapter.POSITION_UNCHANGED) 和 if (newPos ==PagerAdapter.POSITION_NONE) 就已经破案了...,只能说google真会写方法名称...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值