国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

Android LayoutInflater.inflate源碼分析

2019-12-12 04:10:17
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

LayoutInflater.inflate源碼詳解

LayoutInflater的inflate方法相信大家都不陌生,在Fragment的onCreateView中或者在BaseAdapter的getView方法中我們都會(huì)經(jīng)常用這個(gè)方法來(lái)實(shí)例化出我們需要的View.

假設(shè)我們有一個(gè)需要實(shí)例化的布局文件menu_item.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="match_parent"  android:layout_height="wrap_content"  android:orientation="vertical">  <TextView    android:id="@+id/id_menu_title_tv"    android:layout_width="match_parent"    android:layout_height="300dp"    android:gravity="center_vertical"    android:textColor="@android:color/black"    android:textSize="16sp"    android:text="@string/menu_item"/></LinearLayout>

我們想在BaseAdapter的getView()方法中對(duì)其進(jìn)行實(shí)例化,其實(shí)例化的方法有三種,分別是:

2個(gè)參數(shù)的方法:

convertView = mInflater.inflate(R.layout.menu_item, null);

3個(gè)參數(shù)的方法(attachToRoot=false):

convertView = mInflater.inflate(R.layout.menu_item, parent, false);

3個(gè)參數(shù)的方法(attachToRoot=true):

convertView = mInflater.inflate(R.layout.menu_item, parent, true);

究竟我們應(yīng)該用哪個(gè)方法進(jìn)行實(shí)例化View,這3個(gè)方法又有什么區(qū)別呢?如果有同學(xué)對(duì)三個(gè)方法的區(qū)別還不是特別清楚,那么就和我一起從源碼的角度來(lái)分析一下這個(gè)問題吧.

源碼

inflate

我們先來(lái)看一下兩個(gè)參數(shù)的inflate方法,源碼如下:

public View inflate(@LayoutRes int resource, @Nullable ViewGroup root) {  return inflate(resource, root, root != null);}

從代碼我們看出,其實(shí)兩個(gè)參數(shù)的inflate方法根據(jù)父布局parent是否為null作為第三個(gè)參數(shù)來(lái)調(diào)用三個(gè)參數(shù)的inflate方法,三個(gè)參數(shù)的inflate方法源碼如下:

public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot) {  // 獲取當(dāng)前應(yīng)用的資源集合  final Resources res = getContext().getResources();  // 獲取指定資源的xml解析器  final XmlResourceParser parser = res.getLayout(resource);  try {    return inflate(parser, root, attachToRoot);  } finally {    // 返回View之前關(guān)閉parser資源    parser.close();  }}

這里需要解釋一下,我們傳入的資源布局id是無(wú)法直接實(shí)例化的,需要借助XmlResourceParser.

而XmlResourceParser是借助Android的pull解析方法是解析布局文件的.繼續(xù)跟蹤inflate方法源碼:

public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {  synchronized (mConstructorArgs) {    // 獲取上下文對(duì)象,即LayoutInflater.from傳入的Context.    final Context inflaterContext = mContext;    // 根據(jù)parser構(gòu)建XmlPullAttributes.    final AttributeSet attrs = Xml.asAttributeSet(parser);    // 保存之前的Context對(duì)象.    Context lastContext = (Context) mConstructorArgs[0];    // 賦值為傳入的Context對(duì)象.    mConstructorArgs[0] = inflaterContext;    // 注意,默認(rèn)返回的是父布局root.    View result = root;    try {      // 查找xml的開始標(biāo)簽.      int type;      while ((type = parser.next()) != XmlPullParser.START_TAG &&          type != XmlPullParser.END_DOCUMENT) {        // Empty      }      // 如果沒有找到有效的開始標(biāo)簽,則拋出InflateException異常.      if (type != XmlPullParser.START_TAG) {        throw new InflateException(parser.getPositionDescription()            + ": No start tag found!");      }      // 獲取控件名稱.      final String name = parser.getName();      // 特殊處理merge標(biāo)簽      if (TAG_MERGE.equals(name)) {        if (root == null || !attachToRoot) {          throw new InflateException("<merge /> can be used only with a valid "              + "ViewGroup root and attachToRoot=true");        }        rInflate(parser, root, inflaterContext, attrs, false);      } else {        // 實(shí)例化我們傳入的資源布局的view        final View temp = createViewFromTag(root, name, inflaterContext, attrs);        ViewGroup.LayoutParams params = null;        // 如果傳入的parent不為空.        if (root != null) {          if (DEBUG) {            System.out.println("Creating params from root: " +                root);          }          // 創(chuàng)建父類型的LayoutParams參數(shù).          params = root.generateLayoutParams(attrs);          if (!attachToRoot) {            // 如果實(shí)例化的View不需要添加到父布局上,則直接將根據(jù)父布局生成的params參數(shù)設(shè)置            // 給它即可.            temp.setLayoutParams(params);          }        }        // 遞歸的創(chuàng)建當(dāng)前布局的所有控件        rInflateChildren(parser, temp, attrs, true);        // 如果傳入的父布局不為null,且attachToRoot為true,則將實(shí)例化的View加入到父布局root中        if (root != null && attachToRoot) {          root.addView(temp, params);        }        // 如果父布局為null或者attachToRoot為false,則將返回值設(shè)置成我們實(shí)例化的View        if (root == null || !attachToRoot) {          result = temp;        }      }    } catch (XmlPullParserException e) {      InflateException ex = new InflateException(e.getMessage());      ex.initCause(e);      throw ex;    } catch (Exception e) {      InflateException ex = new InflateException(          parser.getPositionDescription()              + ": " + e.getMessage());      ex.initCause(e);      throw ex;    } finally {      // Don't retain static reference on context.      mConstructorArgs[0] = lastContext;      mConstructorArgs[1] = null;    }    Trace.traceEnd(Trace.TRACE_TAG_VIEW);    return result;  }}

上述代碼中的關(guān)鍵部分我已經(jīng)加入了中文注釋.從上述代碼中我們還可以發(fā)現(xiàn),我們傳入的布局文件是通過createViewFromTag來(lái)實(shí)例化每一個(gè)子節(jié)點(diǎn)的.

createViewFromTag

函數(shù)源碼如下:

/** * 方便調(diào)用5個(gè)參數(shù)的方法,ignoreThemeAttr的值為false. */private View createViewFromTag(View parent, String name, Context context, AttributeSet attrs) {  return createViewFromTag(parent, name, context, attrs, false);}View createViewFromTag(View parent, String name, Context context, AttributeSet attrs,    boolean ignoreThemeAttr) {  if (name.equals("view")) {    name = attrs.getAttributeValue(null, "class");  }  // Apply a theme wrapper, if allowed and one is specified.  if (!ignoreThemeAttr) {    final TypedArray ta = context.obtainStyledAttributes(attrs, ATTRS_THEME);    final int themeResId = ta.getResourceId(0, 0);    if (themeResId != 0) {      context = new ContextThemeWrapper(context, themeResId);    }    ta.recycle();  }  // 特殊處理“1995”這個(gè)標(biāo)簽(ps: 平時(shí)我們寫xml布局文件時(shí)基本沒有使用過).  if (name.equals(TAG_1995)) {    // Let's party like it's 1995!    return new BlinkLayout(context, attrs);  }  try {    View view;    if (mFactory2 != null) {      view = mFactory2.onCreateView(parent, name, context, attrs);    } else if (mFactory != null) {      view = mFactory.onCreateView(name, context, attrs);    } else {      view = null;    }    if (view == null && mPrivateFactory != null) {      view = mPrivateFactory.onCreateView(parent, name, context, attrs);    }    if (view == null) {      final Object lastContext = mConstructorArgs[0];      mConstructorArgs[0] = context;      try {        if (-1 == name.indexOf('.')) {          view = onCreateView(parent, name, attrs);        } else {          view = createView(name, null, attrs);        }      } finally {        mConstructorArgs[0] = lastContext;      }    }    return view;  } catch (InflateException e) {    throw e;  } catch (ClassNotFoundException e) {    final InflateException ie = new InflateException(attrs.getPositionDescription()        + ": Error inflating class " + name);    ie.initCause(e);    throw ie;  } catch (Exception e) {    final InflateException ie = new InflateException(attrs.getPositionDescription()        + ": Error inflating class " + name);    ie.initCause(e);    throw ie;  }}

在createViewFromTag方法中,最終是通過createView方法利用反射來(lái)實(shí)例化view控件的.

createView

public final View createView(String name, String prefix, AttributeSet attrs)  throws ClassNotFoundException, InflateException {  // 以View的name為key, 查詢構(gòu)造函數(shù)的緩存map中是否已經(jīng)存在該View的構(gòu)造函數(shù).  Constructor<? extends View> constructor = sConstructorMap.get(name);  Class<? extends View> clazz = null;  try {    // 構(gòu)造函數(shù)在緩存中未命中    if (constructor == null) {      // 通過類名去加載控件的字節(jié)碼      clazz = mContext.getClassLoader().loadClass(prefix != null ? (prefix + name) : name).asSubClass(View.class);      // 如果有自定義的過濾器并且加載到字節(jié)碼,則通過過濾器判斷是否允許加載該View      if (mFilter != null && clazz != null) {        boolean allowed = mFilter.onLoadClass(clazz);        if (!allowed) {          failNotAllowed(name, prefix, attrs);        }      }      // 得到構(gòu)造函數(shù)      constructor = clazz.getConstructor(mConstructorSignature);      constructor.setAccessible(true);      // 緩存構(gòu)造函數(shù)      sConstructorMap.put(name, constructor);    } else {      if (mFilter != null) {        // 過濾的map是否包含了此類名        Boolean allowedState = mFilterMap.get(name);        if (allowedState == null) {          // 重新加載類的字節(jié)碼          clazz = mContext.getClassLoader().loadClass(prefix != null ? (prefix + name) : name).asSubclass(View.class);          boolean allowed = clazz != null && mFilter.onLoadClass(clazz);          mFilterMap.put(name, allowed);          if (!allowed) {            failNotAllowed(name, prefix, attrs);          }        } else if (allowedState.equals(Boolean.FALSE)) {          failNotAllowed(name, prefix, attrs);        }      }    }    // 實(shí)例化類的參數(shù)數(shù)組(mConstructorArgs[0]為Context, [1]為View的屬性)    Object[] args = mConstructorArgs;    args[1] = attrs;    // 通過構(gòu)造函數(shù)實(shí)例化View    final View view = constructor.newInstance(args);    if (View instanceof ViewStub) {      final ViewStub viewStub = (ViewStub) view;      viewStub.setLayoutInflater(cloneInContext((Context)args[0]))    }    return view;  } catch (NoSunchMethodException e) {    // ......  } catch (ClassNotFoundException e) {    // ......  } catch (Exception e) {    // ......  } finally {    // ......  }}

總結(jié)

通過學(xué)習(xí)了inflate函數(shù)源碼,我們?cè)倩剡^頭去看BaseAdapter的那三種方法,我們可以得出的結(jié)論是:

第一種方法使用不夠規(guī)范, 且會(huì)導(dǎo)致實(shí)例化View的LayoutParams屬性失效.(ps: 即layout_width和layout_height等參數(shù)失效, 因?yàn)樵创a中這種情況的LayoutParams為null).

第二種是最正確,也是最標(biāo)準(zhǔn)的寫法.

第三種由于attachToRoot為true,所以返回的View其實(shí)是父布局ListView,這顯然不是我們想要實(shí)例化的View.因此,第三種寫法是錯(cuò)誤的.

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 沙湾县| 宝坻区| 霍山县| 聂荣县| 石渠县| 屯门区| 民县| 孟村| 霍州市| 德令哈市| 洪雅县| 南充市| 沙田区| 博湖县| 克东县| 香格里拉县| 灵石县| 宁乡县| 天津市| 安达市| 苏尼特左旗| 株洲市| 汤原县| 博野县| 罗城| 麻城市| 孝昌县| 齐齐哈尔市| 锡林郭勒盟| 峨眉山市| 慈溪市| 榕江县| 漯河市| 永靖县| 黄山市| 邹城市| 西青区| 都匀市| 蒙阴县| 满城县| 琼结县|