ViewPager强制刷新UI

来源:互联网 发布:2g3g4g网络结构图 编辑:程序博客网 时间:2024/05/17 08:07

ViewPager强制刷新UIViewPager不能动态刷新UI的原因主要是因为PagerAdapter中调用notifyDataSetChanged是会失效的。
源码分析
<pre name="code" class="java">1.首先看一下getItemPosition方法默认返回的是-1也就是POSITION_UNCHANGED标志
public abstract class PagerAdapter {    private DataSetObservable mObservable = new DataSetObservable();    public static final int POSITION_UNCHANGED = -1;    public static final int POSITION_NONE = -2;    public PagerAdapter() {    }    public abstract int getCount();    public Object instantiateItem(ViewGroup container, int position) {        return this.instantiateItem((View)container, position);    }    public void destroyItem(ViewGroup container, int position, Object object) {        this.destroyItem((View)container, position, object);    }  public int getItemPosition(Object object) {        return -1;    }    public void notifyDataSetChanged() {        this.mObservable.notifyChanged();    }  }
<pre name="code" class="java">2然后我们看一下viewpager的dataSetChanged()在调用这个方法之前系统会首先调用一下adapter.getItemPosition(),然后我们看一下判断条件if(child!=-1)才能进行更新,adapter.getItemPosition()系统默认给的是-1所以调用时没法更新

 void dataSetChanged() {        int adapterCount = this.mAdapter.getCount();        this.mExpectedAdapterCount = adapterCount;        boolean needPopulate = this.mItems.size() < this.mOffscreenPageLimit * 2 + 1 && this.mItems.size() < adapterCount;        int newCurrItem = this.mCurItem;        boolean isUpdating = false;        int childCount;        for(childCount = 0; childCount < this.mItems.size(); ++childCount) {           ViewPager.ItemInfo i = (ViewPager.ItemInfo)this.mItems.get(childCount);           int child = this.mAdapter.getItemPosition(i.object);           if(child != -1) {                if(child == -2) {                    this.mItems.remove(childCount);                    --childCount;                    if(!isUpdating) {                        this.mAdapter.startUpdate(this);                        isUpdating = true;                    }                    this.mAdapter.destroyItem(this, i.position, i.object);                    needPopulate = true;                    if(this.mCurItem == i.position) {                        newCurrItem = Math.max(0, Math.min(this.mCurItem, adapterCount - 1));                        needPopulate = true;                    }                } else if(i.position != child) {                    if(i.position == this.mCurItem) {                        newCurrItem = child;                    }                    i.position = child;                    needPopulate = true;                }            }        }        if(isUpdating) {            this.mAdapter.finishUpdate(this);        }        Collections.sort(this.mItems, COMPARATOR);        if(needPopulate) {            childCount = this.getChildCount();            for(int var9 = 0; var9 < childCount; ++var9) {                View var10 = this.getChildAt(var9);                ViewPager.LayoutParams lp = (ViewPager.LayoutParams)var10.getLayoutParams();                if(!lp.isDecor) {                    lp.widthFactor = 0.0F;                }            }            this.setCurrentItemInternal(newCurrItem, false, true);            this.requestLayout();        }    }
通用解决方法当ViewPager绘制完Item之后,ViewPager会把child标记为POSITION_UNCHANGED,这样就不会在notifyDataSetChanged后更新这个View了。所以,要解决这个问题,我们只需要在:    @Override    public int getItemPosition(Object object) {        return POSITION_NONE;    }当我们调用PagerAdapter的notifyDataSetChanged方法之后,系统会去Adapter的getItemPosition方法中遍历所有的child,我们在上面的方法中改写了返回值,全部返回为POSITION_NONE,表示child都没有绘制过,这样ViewPager就会去重绘了。更加优化一点的代码如下:    @Override    public void notifyDataSetChanged() {        mChildCount = getCount();        super.notifyDataSetChanged();    }    @Override    public int getItemPosition(Object object) {        // 重写getItemPosition,保证每次获取时都强制重绘UI        if (mChildCount > 0) {            mChildCount--;            return POSITION_NONE;        }        return super.getItemPosition(object);    }我们增加一个mChildCount来记录子类的数量,在一定程度上减少重绘的次数。因为重绘的时候,ViewPager会的Destory Item,增加了系统开销。

http://blog.csdn.net/eclipsexys/article/details/49758155

0 0
原创粉丝点击