亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android中使用LayoutInflater要注意的一些坑

 更新時間:2017年04月25日 16:38:55   作者:Android雜貨鋪  
LayoutInflater類在我們日常開發(fā)中經(jīng)常會用到,最近在使用中就遇到了一些問題,所有下面這篇文章主要給大家總結了關于Android中使用LayoutInflater要注意的一些坑,希望通過這篇能讓大家避免走一些彎路,需要的朋友可以參考學習,下面來一起看吧。

前言

在平時的開發(fā)過程中,我們經(jīng)常會用LayoutInflater這個類,比如說在Fragment$onCreateViewRecyclerView.Adapter$onCreateViewHolder中都會用到。它的用法也無非就是LayoutInflater.inflate(resourceId, root, attachToRoot),第一個參數(shù)沒什么好說的,但第二個和第三個參數(shù)結合起來會帶來一定的迷惑性。之前有時候會發(fā)現(xiàn)界面布局上出了一些問題,查了很久之后偶然的改動了這兩個參數(shù),發(fā)現(xiàn)問題解決了,然后也就過去了,并沒有去思考這是為什么,然后下次可能又重復這種困境了。

所以想在這里總結一下,避免以后繼續(xù)掉坑。

先來看看inflate方法的注釋:

/**
 * Inflate a new view hierarchy from the specified xml resource. Throws
 * {@link InflateException} if there is an error.
 * 
 * @param resource ID for an XML layout resource to load (e.g.,
 *  <code>R.layout.main_page</code>)
 * @param root Optional view to be the parent of the generated hierarchy (if
 *  <em>attachToRoot</em> is true), or else simply an object that
 *  provides a set of LayoutParams values for root of the returned
 *  hierarchy (if <em>attachToRoot</em> is false.)
 * @param attachToRoot Whether the inflated hierarchy should be attached to
 *  the root parameter? If false, root is only used to create the
 *  correct subclass of LayoutParams for the root view in the XML.
 * @return The root View of the inflated hierarchy. If root was supplied and
 *   attachToRoot is true, this is root; otherwise it is the root of
 *   the inflated XML file.
 */
public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot)

首先需要了解的一點是,一個View的測量結果并不只是由它自己的layout_width和layout_height(即LayoutParams)所決定的,而是由父容器給它的約束(MeasureSpec)和它自身的LayoutParams共同決定的。

達成這個共識之后,我們再來看看它的參數(shù)。

  • root:給布局文件提供一個父容器。布局文件里面總有一個元素是沒有父容器的(沒錯,就是根元素),所以需要給它提供一個父容器來幫助它完成測量工作。如果root為空的話,就會導致根元素中的layout_xxx全部失效,從而影響到整個布局。同時,如果root為空的話,那么attachToRoot也就沒有意義了。
  • attachToRoot: 如果為true,創(chuàng)建出來的布局系統(tǒng)會幫我們添加到父容器中去。為false的話,就只是給它提供約束,好讓這個布局順利完成測量等工作而已,將布局添加到父容器中去需要我們后續(xù)根據(jù)需要去手動調用addView方法。
  • 返回值:如果root != null && attachToRoot,返回的View就是傳進來的root,否則返回由布局文件所創(chuàng)建的View對象。

用幾個例子來說明一下會比較好理解。Activity的布局是一個LinearLayout,要添加的布局如下:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/item_text"
  android:layout_width="200dp"
  android:layout_height="50dp"
  android:layout_marginLeft="16dp"
  android:layout_marginTop="16dp"
  android:background="@color/colorAccent"
  android:gravity="center"
  android:text="item: text"/>

正常的情況

// 第一種方法
View inflatedView = LayoutInflater.from(this).inflate(R.layout.item_text, mLinearLayout, true);
Log.d(TAG, "inflated view is " + inflatedView);

// 第二種方法
View inflatedView = LayoutInflater.from(this).inflate(R.layout.item_text, mLinearLayout, false);
Log.d(TAG, "inflated view is " + inflatedView);
mLinearLayout.addView(inflatedView);

視覺上的結果都是一樣的


但是Log就有一點不一樣了,這就是attachToRoot不同的值所導致的。

第一種方法的Log

D/MainActivity: inflated view is android.widget.LinearLayout{36e9aac V.E...... ......I. 0,0-0,0 #7f0c0051 app:id/linear}

第二種方法的Log

D/MainActivity: inflated view is android.support.v7.widget.AppCompatTextView{3c9d37b V.ED..... ......ID 0,0-0,0 #7f0c0054 app:id/item_text}

還有一個需要注意的地方是:如果在第一種方法的基礎上再加上mLinearLayout.addView(inflatedView)就會造成報錯
IllegalStateException: The specified child already has a parent....  。

而如果第二種方法沒有這句話,界面上是看不到任何東西的。

root為null的情況

mLinearLayout = (LinearLayout) findViewById(R.id.linear);
View inflatedView = LayoutInflater.from(this).inflate(R.layout.item_text, null);
Log.d(TAG, "inflated view is " + inflatedView);
mLinearLayout.addView(inflatedView);

此時再看看它的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/item_text"
  android:layout_width="200dp"
  android:layout_height="50dp"
  android:layout_marginLeft="16dp"
  android:layout_marginTop="16dp"
  android:background="@color/colorAccent"
  android:gravity="center"
  android:text="item: text"/>

不難發(fā)現(xiàn),所有l(wèi)ayout_xxx的屬性全都失效了。

RecyclerView中的Inflater

上面說了,在創(chuàng)建布局的時候,要把布局添加到root中去,并且有兩種方法,但是我們在onCreateViewHolder中添加布局的時候卻是這樣寫的:

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
 View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_text, parent, false);
 return new MyViewHolder(view);
}

如果第三個參數(shù)傳了true還會報錯,這又是為什么呢?

java.lang.IllegalStateException: The specified child already has a parent.

直觀上來解釋就是,子View的添加與刪除是由RecyclerView來管理的,不需要我們來添加。但我們還是從RecyclerView的代碼來理解一下會好一些。

以LinearLayoutManager為例,RecyclerView在創(chuàng)建子View的時候會調用到LinearLayoutManager$layoutChunk方法:

void layoutChunk(RecyclerView.Recycler recycler, RecyclerView.State state,
  LayoutState layoutState, LayoutChunkResult result) {
 // 在這里會調用到Adapter$onCreateViewHolder
 View view = layoutState.next(recycler);
 if (view == null) {
  if (DEBUG && layoutState.mScrapList == null) {
   throw new RuntimeException("received null view when unexpected");
  }
  // if we are laying out views in scrap, this may return null which means there is
  // no more items to layout.
  result.mFinished = true;
  return;
 }
 LayoutParams params = (LayoutParams) view.getLayoutParams();
 if (layoutState.mScrapList == null) {
  if (mShouldReverseLayout == (layoutState.mLayoutDirection
    == LayoutState.LAYOUT_START)) {
   addView(view);
  } else {
   addView(view, 0);
  }
 } else {
  if (mShouldReverseLayout == (layoutState.mLayoutDirection
    == LayoutState.LAYOUT_START)) {
   addDisappearingView(view);
  } else {
   addDisappearingView(view, 0);
  }
 }

 // 省略其它大部分代碼
}

在初始化的時候,View view = layoutState.next(recycler)里面會調用到我們熟悉的onCreateViewHolder方法,然后我們在里面inflate的過程中第三個參數(shù)傳了true,將子View添加到了RecyclerView中去了。然而,獲得View之后,調用到了addView(因為是初始化,不可能調用addDisappearingView) ,這里又會去添加一次,所以報出了上面的IllegalStateException異常。

總結

以上就是這篇文章的全部內容了,希望本文的內容對各位Android開發(fā)者們能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關文章

  • Android入門教程之Picasso框架

    Android入門教程之Picasso框架

    本文會先介紹Picasso的基本使用方法,讓您快速上手Picasso。后面我們分享實現(xiàn)ListView顯示網(wǎng)絡圖片的實例,從源碼角度詳細分析它的實現(xiàn),有需要的可以參考借鑒。
    2016-08-08
  • android上一個可追蹤代碼具體到函數(shù)某行的日志類

    android上一個可追蹤代碼具體到函數(shù)某行的日志類

    追蹤代碼到函數(shù)具體某行,這樣的功能,是每一個程序員都希望會有的,因為它可以幫助我們追蹤到某行代碼的錯誤,接下來介紹下android上一個可追蹤代碼到函數(shù)具體某行的日志類,希望對開發(fā)者有所幫助
    2012-12-12
  • Android 7.0 運行時權限彈窗問題的解決

    Android 7.0 運行時權限彈窗問題的解決

    這篇文章主要介紹了Android 7.0 運行時權限彈窗問題的解決,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-03-03
  • Android WiFi熱點開發(fā)的示例代碼

    Android WiFi熱點開發(fā)的示例代碼

    這篇文章主要介紹了Android WiFi熱點開發(fā)的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-09-09
  • Android程序開發(fā)ListView+Json+異步網(wǎng)絡圖片加載+滾動翻頁的例子(圖片能緩存,圖片不錯亂)

    Android程序開發(fā)ListView+Json+異步網(wǎng)絡圖片加載+滾動翻頁的例子(圖片能緩存,圖片不錯亂)

    這篇文章主要介紹了Android程序開發(fā)ListView+Json+異步網(wǎng)絡圖片加載+滾動翻頁的例子(圖片能緩存,圖片不錯亂) 的相關資料,需要的朋友可以參考下
    2016-01-01
  • Android中volley封裝實踐記錄(二)

    Android中volley封裝實踐記錄(二)

    這篇文章主要給大家介紹了關于Android中volley封裝的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-02-02
  • Android使用SlidingPaneLayout 實現(xiàn)仿微信的滑動返回

    Android使用SlidingPaneLayout 實現(xiàn)仿微信的滑動返回

    這篇文章主要介紹了Android使用SlidingPaneLayout 實現(xiàn)仿微信的滑動返回,需要的朋友可以參考下
    2018-04-04
  • Android Eclipse 注釋模板的使用(圖文說明)

    Android Eclipse 注釋模板的使用(圖文說明)

    為提高代碼的可讀性以及后期的可維護性,為我們的代碼加上規(guī)范化的注釋是很有必要,不僅有利于提高自己的專業(yè)素養(yǎng),也能方便他人
    2013-12-12
  • Android顯式Intent與隱式Intent的使用詳解

    Android顯式Intent與隱式Intent的使用詳解

    Intent的中文意思是“意圖,意向”, Intent對Android的核心和靈魂,是各組件之間的橋梁。四大組件分別為Activity 、Service、BroadcastReceiver、ContentProvider。而這四種組件是獨立的,它們之間可以互相調用,協(xié)調工作,最終組成一個真正的Android應用
    2022-09-09
  • 簡單談談我的Android屏幕適配之路

    簡單談談我的Android屏幕適配之路

    我相信Android碎片化問題是讓所有的Android開發(fā)者都比較頭疼的問題.尤其是屏幕適配這一塊兒.想要自己的app在不同的設備上面都有一個比較好的顯示效果.就必須做好相應的屏幕適配.
    2017-11-11

最新評論