FileDocCategorySizeDatePackage
ViewStubCompat.javaAPI DocAndroid 5.1 API8767Thu Mar 12 22:22:56 GMT 2015android.support.v7.internal.widget

ViewStubCompat

public final class ViewStubCompat extends android.view.View
Backport of {@link android.view.ViewStub} so that we can set the {@link android.view.LayoutInflater} on devices before Jelly Bean.
hide

Fields Summary
private int
mLayoutResource
private int
mInflatedId
private WeakReference
mInflatedViewRef
private android.view.LayoutInflater
mInflater
private OnInflateListener
mInflateListener
Constructors Summary
public ViewStubCompat(android.content.Context context, android.util.AttributeSet attrs)


         
        this(context, attrs, 0);
    
public ViewStubCompat(android.content.Context context, android.util.AttributeSet attrs, int defStyle)

        super(context, attrs, defStyle);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ViewStubCompat,
                defStyle, 0);

        mInflatedId = a.getResourceId(R.styleable.ViewStubCompat_android_inflatedId, NO_ID);
        mLayoutResource = a.getResourceId(R.styleable.ViewStubCompat_android_layout, 0);

        setId(a.getResourceId(R.styleable.ViewStubCompat_android_id, NO_ID));
        a.recycle();

        setVisibility(GONE);
        setWillNotDraw(true);
    
Methods Summary
protected voiddispatchDraw(android.graphics.Canvas canvas)

    
public voiddraw(android.graphics.Canvas canvas)

    
public intgetInflatedId()
Returns the id taken by the inflated view. If the inflated id is {@link View#NO_ID}, the inflated view keeps its original id.

return
A positive integer used to identify the inflated view or {@link #NO_ID} if the inflated view should keep its id.
see
#setInflatedId(int)
attr
ref android.R.styleable#ViewStub_inflatedId

        return mInflatedId;
    
public android.view.LayoutInflatergetLayoutInflater()
Get current {@link LayoutInflater} used in {@link #inflate()}.

        return mInflater;
    
public intgetLayoutResource()
Returns the layout resource that will be used by {@link #setVisibility(int)} or {@link #inflate()} to replace this StubbedView in its parent by another view.

return
The layout resource identifier used to inflate the new View.
see
#setLayoutResource(int)
see
#setVisibility(int)
see
#inflate()
attr
ref android.R.styleable#ViewStub_layout

        return mLayoutResource;
    
public android.view.Viewinflate()
Inflates the layout resource identified by {@link #getLayoutResource()} and replaces this StubbedView in its parent by the inflated layout resource.

return
The inflated layout resource.

        final ViewParent viewParent = getParent();

        if (viewParent != null && viewParent instanceof ViewGroup) {
            if (mLayoutResource != 0) {
                final ViewGroup parent = (ViewGroup) viewParent;
                final LayoutInflater factory;
                if (mInflater != null) {
                    factory = mInflater;
                } else {
                    factory = LayoutInflater.from(getContext());
                }
                final View view = factory.inflate(mLayoutResource, parent,
                        false);

                if (mInflatedId != NO_ID) {
                    view.setId(mInflatedId);
                }

                final int index = parent.indexOfChild(this);
                parent.removeViewInLayout(this);

                final ViewGroup.LayoutParams layoutParams = getLayoutParams();
                if (layoutParams != null) {
                    parent.addView(view, index, layoutParams);
                } else {
                    parent.addView(view, index);
                }

                mInflatedViewRef = new WeakReference<View>(view);

                if (mInflateListener != null) {
                    mInflateListener.onInflate(this, view);
                }

                return view;
            } else {
                throw new IllegalArgumentException("ViewStub must have a valid layoutResource");
            }
        } else {
            throw new IllegalStateException("ViewStub must have a non-null ViewGroup viewParent");
        }
    
protected voidonMeasure(int widthMeasureSpec, int heightMeasureSpec)

        setMeasuredDimension(0, 0);
    
public voidsetInflatedId(int inflatedId)
Defines the id taken by the inflated view. If the inflated id is {@link View#NO_ID}, the inflated view keeps its original id.

param
inflatedId A positive integer used to identify the inflated view or {@link #NO_ID} if the inflated view should keep its id.
see
#getInflatedId()
attr
ref android.R.styleable#ViewStub_inflatedId

        mInflatedId = inflatedId;
    
public voidsetLayoutInflater(android.view.LayoutInflater inflater)
Set {@link LayoutInflater} to use in {@link #inflate()}, or {@code null} to use the default.

        mInflater = inflater;
    
public voidsetLayoutResource(int layoutResource)
Specifies the layout resource to inflate when this StubbedView becomes visible or invisible or when {@link #inflate()} is invoked. The View created by inflating the layout resource is used to replace this StubbedView in its parent.

param
layoutResource A valid layout resource identifier (different from 0.)
see
#getLayoutResource()
see
#setVisibility(int)
see
#inflate()
attr
ref android.R.styleable#ViewStub_layout

        mLayoutResource = layoutResource;
    
public voidsetOnInflateListener(android.support.v7.internal.widget.ViewStubCompat$OnInflateListener inflateListener)
Specifies the inflate listener to be notified after this ViewStub successfully inflated its layout resource.

param
inflateListener The OnInflateListener to notify of successful inflation.
see
android.view.ViewStub.OnInflateListener

        mInflateListener = inflateListener;
    
public voidsetVisibility(int visibility)
When visibility is set to {@link #VISIBLE} or {@link #INVISIBLE}, {@link #inflate()} is invoked and this StubbedView is replaced in its parent by the inflated layout resource. After that calls to this function are passed through to the inflated view.

param
visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
see
#inflate()

        if (mInflatedViewRef != null) {
            View view = mInflatedViewRef.get();
            if (view != null) {
                view.setVisibility(visibility);
            } else {
                throw new IllegalStateException("setVisibility called on un-referenced view");
            }
        } else {
            super.setVisibility(visibility);
            if (visibility == VISIBLE || visibility == INVISIBLE) {
                inflate();
            }
        }