zoukankan      html  css  js  c++  java
  • ViewStub的使用及原理

    最近项目中有需求,需要添加功能引导,如果用户是第一次使用,那么就显示功能引导,之后则不再显示。感觉这样的需求正好可以利用ViewStub来实现,更节省资源。

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        ..............
        
        <ViewStub 
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/lay_guide"
            android:layout="@layout/guide_main_tab"/>
        
    </RelativeLayout>
    if(!GuidePreference.isGuide(getApplicationContext(), GuidePreference.MAIN_TAB_V4)){
      ViewStub stub = (ViewStub) findViewById(R.id.lay_guide);
      if(stub==null){
        return ;
      }
      mFilpper = (ViewFlipper) stub.inflate() ;
      mFilpper.setOnClickListener(new View.OnClickListener() {
                    
        @Override
        public void onClick(View v) {
          guideSwitch() ;
        }
      });
    }

    查看ViewStub的源码,inflate()部分如下:

    public View inflate() {
            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(mContext);
                    }
                    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");
            }
        }

    从以上代码可以看出,ViewStub其实只相当于一个占位View而已,在未inflate之前,高宽都是0,只保存了在parent中的index和layoutParmas而已,在inflate时,用mLayoutResource来替换掉自己。这比自己平时动态往ViewGroup里面添加view的确要少一些操作,方便多了。

  • 相关阅读:
    win10 下安装mysql8.0 zip
    CentOS7 安装mysql8
    linux 下 oracle 安装
    springboot restful jsonp 跨域请求
    Springboot mybatis generate 自动生成实体类和Mapper
    Spring MVC — @RequestMapping原理讲解二
    Spring MVC — @RequestMapping原理讲解一
    Android--LinearLayout中的属性baselineAligned的使用[转]
    Android--CountDownTimer倒计时工具类
    Android UI--SwipeDismissListView_直接删除
  • 原文地址:https://www.cnblogs.com/alexthecoder/p/4191370.html
Copyright © 2011-2022 走看看