关闭

android:duplicateParentState

标签: android
169人阅读 评论(0) 收藏 举报

子控件 childView ,以View为例;

父控件 parantView 以ViewGroup为例;

childView设置了android:duplicateParentState 或 setDuplicateParentStateEnabled 为true

实际上是调用

setFlags(enabled ?DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);把标志位保存到mViewFlags中.

当 parantView状态改变,比如setSelected 或 setPressed,会调用refreshDrawableState();

    public void refreshDrawableState() {

        mPrivateFlags |= PFLAG_DRAWABLE_STATE_DIRTY;

        drawableStateChanged();


        ViewParent parent = mParent;

        if (parent !=null) {

            parent.childDrawableStateChanged(this);

        }

    }

可以看到,它里面其实调用了drawableStateChanged();

    protected void drawableStateChanged() {

        super.drawableStateChanged();


        if ((mGroupFlags &FLAG_NOTIFY_CHILDREN_ON_DRAWABLE_STATE_CHANGE) != 0) {

            if ((mGroupFlags &FLAG_ADD_STATES_FROM_CHILDREN) != 0) {

                thrownew IllegalStateException("addStateFromChildren cannot be enabled if a"

                        + " child has duplicateParentState set to true");

            }


            final View[] children =mChildren;

            final intcount = mChildrenCount;


            for (inti = 0; i < count;i++) {

                final View child = children[i];

                if ((child.mViewFlags & DUPLICATE_PARENT_STATE) != 0) {

   //在这里调用了childView的refreshDrawableState();

                    child.refreshDrawableState();

                }

            }

        }

    }

parantView是通过标志位FLAG_NOTIFY_CHILDREN_ON_DRAWABLE_STATE_CHANGE来判断执行refreshDrawableState();

而标志位是在addViewInner里面设置的,子view如果设置了DUPLICATE_PARENT_STATE,父view则会设置FLAG_NOTIFY_CHILDREN_ON_DRAWABLE_STATE_CHANGE.

private void addViewInner(Viewchild, int index, LayoutParamsparams,boolean preventRequestLayout) {

...

        if ((child.mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE) {

            mGroupFlags |=FLAG_NOTIFY_CHILDREN_ON_DRAWABLE_STATE_CHANGE;

        }


...

}

childView的refreshDrawableState()和parantView一样,也调用了drawableStateChanged()方法

    protected void drawableStateChanged() {

        Drawable d = mBackground;

        if (d != null && d.isStateful()) {

            d.setState(getDrawableState());

        }

    }

setState会调用onStateChange,以StateListDrawable为例,onStateChange会调用selectDrawable()改变当前的mCurrDrawable 和调用invalidateSelf();

    public void invalidateSelf() {

        final Callback callback = getCallback();

        if (callback !=null) {

            callback.invalidateDrawable(this);

        }

    }

callback回到view的invalidateDrawable()方法

    public void invalidateDrawable(Drawabledrawable) {

        if (verifyDrawable(drawable)) {

            final Rect dirty =drawable.getBounds();

            final intscrollX = mScrollX;

            final intscrollY = mScrollY;


            invalidate(dirty.left +scrollX, dirty.top +scrollY,

                    dirty.right +scrollX, dirty.bottom +scrollY);

        }

    }

请求view重新绘制.
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:311次
    • 积分:22
    • 等级:
    • 排名:千里之外
    • 原创:2篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章存档