viewPage notifyDataSetChanged无刷新解决方案之一

来源:互联网 发布:选择网络项目的标准 编辑:程序博客网 时间:2024/05/22 15:20

尊重出处: http://www.67tgb.com/?p=624

以下是文章部分内容,由于不全做了稍微的修改,全文请点击上方连接。

 ViewPager中继承下来的getItemPosition()方法,官方对解释是:

  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 returnsPOSITION_UNCHANGED.

  意思是如果item的位置如果没有发生变化,则返回POSITION_UNCHANGED。如果返回了POSITION_NONE,表示该位置的item已经不存在了。默认的实现是假设item的位置永远不会发生变化,而返回POSITION_UNCHANGED

解决方案

修改适配器的写法,覆盖getItemPosition()方法,当调用notifyDataSetChanged时,让getItemPosition方法人为的返回POSITION_NONE,从而达到强迫viewpager重绘所有item的目的。

具体代码如下:

class SearchAdapter extends PagerAdapter {         private int mChildCount = 0;      @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);     } }




0 0
原创粉丝点击