在项目里遇到一个需求:类似于QQ表情一样的 带有大分类的分组,分组里有多页小icon,效果如图:
==>
然后有个需求是 在切换大分类的时候,由于数据早已确定,简单使用
pagerAdapter.notifyDataSetChanged();
无法完成界面的刷新,然后尝试更改数据的绑定,进行起来很麻烦,于是想强制viewpager进行刷新
就在文档里发现
Called when the host view is attempting to determine if an item’s position has changed. Returns POSITION_UNCHANGED if the position of the given item has not changed orPOSITION_NONE if the item is no longer present in the adapter.
The default implementation assumes that items will never change position and always returns POSITION_UNCHANGED.
在adapter里:
@Override public int getItemPosition(Object object) { if ( mChildCount > 0) { mChildCount --; return POSITION_NONE; } return super.getItemPosition(object); }and
@Override public void notifyDataSetChanged() { mChildCount = getCount(); super.notifyDataSetChanged(); }
究其原因 是在viewpager中控制数据变更的重点方法dataSetChanged中有
if (newPos == PagerAdapter.POSITION_UNCHANGED ) {
continue;
}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, mAdapter.getCount() - 1));needPopulate = true;
}continue;
}
于是就实现了viewpager的强制刷新