ViewStub详解

ViewStub详解首先上源码:packageandroid.view;importandroid.annotation.IdRes;importandroid.annotation.LayoutRes;importandroid.content.Context;importandroid.content.res.TypedArray;importandroid.graphics.Canvas;importandroid.util.AttributeSet;importandroid.widg

大家好,又见面了,我是你们的朋友全栈君。

源码:

package android.view;

import android.annotation.IdRes;
import android.annotation.LayoutRes;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.widget.RemoteViews.RemoteView;

import com.android.internal.R;

import java.lang.ref.WeakReference;

/** * ViewStub是一个用户不可见的,大小为0的view,用于在app运行时懒加载一些布局。 *当ViewStub设置用户可见(setVisibility)时,或者当调用inflate()方法时,布局会就被初始化。ViewStub会用初始化后的布局文件替换自己放入其父布局中(参考inflate中的replaceSelfWithView方法)。 * * 因此ViewStub会在view布局中存在一直到setVisibility或inflate被调用了。 * * 当被初始化的View添加到ViewStub的父布局时会使用ViewStub的布局属性,因此你可以使用ViewStub的inflatedId属性定义/重写View的id。例如: * * * <ViewStub android:id="@+id/stub" * android:inflatedId="@+id/subTree" * android:layout="@layout/mySubTree" * android:layout_width="120dip" * android:layout_height="40dip" /> * * * 上面例子里边定义了一个id为stub的ViewStub。在初始化布局subTree之后,这个ViewStub就会从它父布局中移除。新创建的mySubTree布局的View的id就会使用布局中定义的subTree,最后这个新创建的view的宽为120dip,高为40dip. * * * 如下方式是最标准的初始化布局View的方式: * * * ViewStub stub = findViewById(R.id.stub); * View inflated = stub.inflate(); * * * 当inflate()方法被调用后,ViewStub会被替换为初始化后的布局View,并且View会被返回。从而让app获取到view的引用而不用在调用一次findViewById(). * * @attr ref android.R.styleable#ViewStub_inflatedId * @attr ref android.R.styleable#ViewStub_layout */
@RemoteView
public final class ViewStub extends View { 
   
    private int mInflatedId;
    private int mLayoutResource;

    private WeakReference<View> mInflatedViewRef;

    private LayoutInflater mInflater;
    private OnInflateListener mInflateListener;

    public ViewStub(Context context) { 
   
        this(context, 0);
    }

    /** * 通过传入布局id创建ViewStub(猜测是用于代码中动态初始化时调用的)。 * * @param context The application's environment. * @param layoutResource The reference to a layout resource that will be inflated. */
    public ViewStub(Context context, @LayoutRes int layoutResource) { 
   
        this(context, null);

        mLayoutResource = layoutResource;
    }

    public ViewStub(Context context, AttributeSet attrs) { 
   
        this(context, attrs, 0);
    }

    public ViewStub(Context context, AttributeSet attrs, int defStyleAttr) { 
   
        this(context, attrs, defStyleAttr, 0);
    }

	/** * 解析布局文件创建ViewStub,并获取mInflatedId及mLayoutResource等属性。最后设置不可见及不绘制自身。 * */
    public ViewStub(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 
   
        super(context);

        final TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.ViewStub, defStyleAttr, defStyleRes);
        saveAttributeDataForStyleable(context, R.styleable.ViewStub, attrs, a, defStyleAttr,
                defStyleRes);

        mInflatedId = a.getResourceId(R.styleable.ViewStub_inflatedId, NO_ID);
        mLayoutResource = a.getResourceId(R.styleable.ViewStub_layout, 0);
        mID = a.getResourceId(R.styleable.ViewStub_id, NO_ID);
        a.recycle();

        setVisibility(GONE);
        setWillNotDraw(true);
    }

    /** * 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 */
    @IdRes
    public int getInflatedId() { 
   
        return mInflatedId;
    }

    /** * 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 */
    @android.view.RemotableViewMethod(asyncImpl = "setInflatedIdAsync")
    public void setInflatedId(@IdRes int inflatedId) { 
   
        mInflatedId = inflatedId;
    }

    /** @hide **/
    public Runnable setInflatedIdAsync(@IdRes int inflatedId) { 
   
        mInflatedId = inflatedId;
        return null;
    }

    /** * 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 */
    @LayoutRes
    public int getLayoutResource() { 
   
        return mLayoutResource;
    }

    /** * 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 */
    @android.view.RemotableViewMethod(asyncImpl = "setLayoutResourceAsync")
    public void setLayoutResource(@LayoutRes int layoutResource) { 
   
        mLayoutResource = layoutResource;
    }

    /** @hide **/
    public Runnable setLayoutResourceAsync(@LayoutRes int layoutResource) { 
   
        mLayoutResource = layoutResource;
        return null;
    }

    /** * Set {@link LayoutInflater} to use in {@link #inflate()}, or {@code null} * to use the default. */
    public void setLayoutInflater(LayoutInflater inflater) { 
   
        mInflater = inflater;
    }

    /** * Get current {@link LayoutInflater} used in {@link #inflate()}. */
    public LayoutInflater getLayoutInflater() { 
   
        return mInflater;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
   
        setMeasuredDimension(0, 0);
    }

	/** * 不做任何绘制,所以比普通view节省了时间与内存。 */
    @Override
    public void draw(Canvas canvas) { 
   
    }

    @Override
    protected void dispatchDraw(Canvas canvas) { 
   
    }

    /** * 当设置visibility为VISIBLE或INVISIBLE时,inflate方法就会被调用 * ,且初始化出来的view对象会替换StubView在其父布局中的位置。 * * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}. * * @see #inflate() */
    @Override
    @android.view.RemotableViewMethod(asyncImpl = "setVisibilityAsync")
    public void setVisibility(int visibility) { 
   
        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();
            }
        }
    }

    /** @hide **/
    public Runnable setVisibilityAsync(int visibility) { 
   
        if (visibility == VISIBLE || visibility == INVISIBLE) { 
   
            ViewGroup parent = (ViewGroup) getParent();
            return new ViewReplaceRunnable(inflateViewNoAdd(parent));
        } else { 
   
            return null;
        }
    }

    private View inflateViewNoAdd(ViewGroup parent) { 
   
        final LayoutInflater factory;
        if (mInflater != null) { 
   
            factory = mInflater;
        } else { 
   
            factory = LayoutInflater.from(mContext);
        }
        final View view = factory.inflate(mLayoutResource, parent, false);

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

    private void replaceSelfWithView(View view, ViewGroup parent) { 
   
        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);
        }
    }

    /** * 初始化布局view,替换stub在父布局中的位置(注:stub被替换之后getparent会为空,因此会抛出IllegalArgumentException异常)。 * * * @return The inflated layout resource. * */
    public View inflate() { 
   
        final ViewParent viewParent = getParent();

        if (viewParent != null && viewParent instanceof ViewGroup) { 
   
            if (mLayoutResource != 0) { 
   
                final ViewGroup parent = (ViewGroup) viewParent;
                final View view = inflateViewNoAdd(parent);
                replaceSelfWithView(view, parent);

                mInflatedViewRef = new WeakReference<>(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");
        }
    }

    /** * 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 */
    public void setOnInflateListener(OnInflateListener inflateListener) { 
   
        mInflateListener = inflateListener;
    }

    /** * 接收布局view被创建的观察者,如果设置了会在布局view被初始化之后回调 listener的onInflate方法。 * * * @see android.view.ViewStub#setOnInflateListener(android.view.ViewStub.OnInflateListener) */
    public static interface OnInflateListener { 
   
        /** * Invoked after a ViewStub successfully inflated its layout resource. * This method is invoked after the inflated view was added to the * hierarchy but before the layout pass. * * @param stub The ViewStub that initiated the inflation. * @param inflated The inflated View. */
        void onInflate(ViewStub stub, View inflated);
    }

    /** @hide **/
    public class ViewReplaceRunnable implements Runnable { 
   
        public final View view;

        ViewReplaceRunnable(View view) { 
   
            this.view = view;
        }

        @Override
        public void run() { 
   
            replaceSelfWithView(view, (ViewGroup) getParent());
        }
    }
}

使用方法:
1.在布局中定义ViewStub:

<ViewStub android:id="@+id/stub" android:inflatedId="@+id/subTree" android:layout="@layout/mySubTree" android:layout_width="120dip" android:layout_height="40dip" />

2.布局中使用:

	 ViewStub stub = findViewById(R.id.stub);
	 //inflated就是初始化后的布局view对象
     View inflated = stub.inflate();
     //对inflated操作,设置文字,图片等。
     

抛砖引玉,不当之处敬请之处,万分感谢?。

注:其实也可以也可以用动态添加的方法添加View:在java/kotlin代码中动态初始化View,然后添加到对应的viewgroup中。

1.在需要的时候通过layoutInflater.inflate(mLayoutResource, parent, false);

2.方法初始化要添加的view,然后调用对应viewGroup.addView(View child/View child, int index/View child, LayoutParams params);方法添加到布局中;

这样的话在布局中就不用多了个ViewStub的view了,性能也更好些。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/153965.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • plt.subplot()使用方法以及参数介绍

    plt.subplot()使用方法以及参数介绍plt.subplot()plt.subplot(nrows,ncols,index,**kwargs)第一个参数:*args(官网文档描述)Eithera3-digitintegerorthreeseparateintegersdescribingthepositionofthesubplot.Ifthethreeintegersarenr…

    2022年6月18日
    94
  • kprobe分析内核kworker占用CPU 100%问题总结

    kprobe分析内核kworker占用CPU 100%问题总结kprobe分析内核kworker占用CPU100%问题总结CreatebyBillow.Jen,2020.3.8前言[引用]有的工程师在线上出问题的时候,非常慌乱,会去胡乱猜测可能的原因,但又缺乏任何证据去支持或者否证他的猜测与假设。他甚至会在线上反复地试错,反复地折腾,搞得一团乱麻,毫无头绪,让自己和身边的同事都很痛苦,白白浪费了宝贵的排错时间。但是当我们有了动态追踪技术之后,排…

    2022年9月24日
    2
  • 产品需求分析与市场分析方法汇总(SWOT+PDCA+波士顿矩阵BCG+5W2H分析法+STAR关键事件分析法+目标管理SMART+时间管理紧急重要矩阵+WBS任务分解法)

    产品需求分析与市场分析方法汇总(SWOT+PDCA+波士顿矩阵BCG+5W2H分析法+STAR关键事件分析法+目标管理SMART+时间管理紧急重要矩阵+WBS任务分解法)产品需求分析与市场分析方法汇总(SWOT+PDCA+波士顿矩阵BCG+5W2H分析法+STAR关键事件分析法+目标管理SMART+时间管理紧急重要矩阵+WBS任务分解法)产品需求分析与市场分析方法汇总http://www.chanpin100.com/article/55744一、KANO模型KANO模型分为:基本型需求、期望型需求、兴奋型需求。1.基本型需求,这类需求是应…

    2022年5月18日
    78
  • 机器学习发展历史回顾

    机器学习发展历史回顾其它机器学习、深度学习算法的全面系统讲解可以阅读《机器学习-原理、算法与应用》,清华大学出版社,雷明著,由SIGAI公众号作者倾力打造。书的购买链接 书的勘误,优化,源代码资源机器学习是现阶段解决很多人工智能问题的主流方法,作为一个独立的方向,正处于高速发展之中。最早的机器学习算法可以追溯到20世纪初,到今天为止,已经过去了100多年。从1980年机器学习称为一个独立的方向开始算起,到现在…

    2022年5月29日
    45
  • springboot springcloud项目实战(阿里云如何搭建服务器)

    https://blog.csdn.net/u010938610/article/details/79282624

    2022年4月15日
    50
  • 国内机床企业名录

    国内机床企业名录珠江机床有限公司北京第三机床厂北京市机电研究院北京市华德液压泵厂机床厂北京市仪表机床厂北京市电加工机床厂北京市电加工研究所北京阿奇工业电子有限公司中国航天工业总公司二八四厂(北京长峰机械动力厂)北京机床研究所北京良乡锻压机床厂北京市京良机械制造有限公司北京长空机械公司北京北方车辆制造厂十五分厂北京机电研究所北京工业大学科技开发管理部(数控机床配件研究所)北京光电量仪研究中心北京工大光电机械厂北京液

    2022年6月30日
    81

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号