viewPager 里Adapter强制刷新

         


                     在项目里遇到一个需求:类似于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的强制刷新





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值