RecyclerView控件列表項佈局match_parent屬性失效的根本原理

之前用RecyclerView爲了達到自己想要的結果,把item的根佈局(最外層Layout)的大小設爲match_parent,一開始卻發現一個很大的問題!咦?爲什麼我的item一加載就成了wrap_content的效果?我的match_parent爲什麼效果顯示不出來…在嘗試了很多很多方法覺得應該不是我寫錯了之後,我才意識到我根本不知道LayoutInflater的inflate這個函數的參數的意義,查了api還是不解,這個第三個參數attachToRoot到底是啥意思?爲了弄懂這個問題,看了很多博客,覺得這個是個好問題!弄懂了它,你再也不會錯誤的用inflate了!

我們最常用的便是LayoutInflater的inflate方法,這個方法重載了四種調用方式,分別爲:

1. public View inflate(int resource, ViewGroup root)

2. public View inflate(int resource, ViewGroup root, boolean attachToRoot)

3.public View inflate(XmlPullParser parser, ViewGroup root)

4.public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot)

而我們最常用的用法就是這樣(RecyclerView的Adapter中):

[java] view plain copy
  1. @Override  
  2.     public AgendaDetailHolder onCreateViewHolder(ViewGroup parent, int viewType) {  
  3.         View view = mInflater.inflate(R.layout.item_agenda_detail, null);  
  4.         AgendaDetailHolder holder = new AgendaDetailHolder(view);  
  5.         return holder;  
  6.     }  

看似很簡單的一個調用,原來有四個重載,而我們最簡單的用法就是上面這段用法,也是我一開始的用法(複製粘貼就是這樣,你也不會去看細節)

可當你運行測試的時候,你驚訝的發現,說好的效果呢?

下面是我item的佈局文件,清清楚楚的寫着match_parent

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent">  
  6.   
  7.     <View  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="50dp">  
  10.     </View>  
  11.     <LinearLayout  
  12.         android:orientation="vertical"  
  13.         android:id="@+id/id_agenda_detail_layout"  
  14.         android:background="#afbfff"  
  15.         android:layout_width="match_parent"  
  16.         android:layout_height="0dp"  
  17.         android:layout_weight="1">  
  18.         <LinearLayout  
  19.             android:background="#FFFFFF"  
  20.             android:orientation="vertical"  
  21.             android:layout_marginLeft="2dp"  
  22.             android:layout_marginRight="2dp"  
  23.             android:layout_marginTop="2dp"  
  24.             android:layout_marginBottom="2dp"  
  25.             android:layout_width="match_parent"  
  26.             android:layout_height="match_parent">  
  27.   
  28.             <TextView  
  29.                 android:id="@+id/id_text_view_agenda_title"  
  30.                 android:gravity="center"  
  31.                 android:textSize="30sp"  
  32.                 android:text="議程標題"  
  33.                 android:layout_width="match_parent"  
  34.                 android:layout_height="0dp"  
  35.                 android:layout_weight="1"/>  
  36.             <TextView  
  37.                 android:id="@+id/id_text_view_agenda_speaker"  
  38.                 android:gravity="center"  
  39.                 android:textSize="20sp"  
  40.                 android:text="主講人:"  
  41.                 android:layout_width="match_parent"  
  42.                 android:layout_height="0dp"  
  43.                 android:layout_weight="1"/>  
  44.             <TextView  
  45.                 android:id="@+id/id_text_view_agenda_time"  
  46.                 android:gravity="center"  
  47.                 android:textSize="20sp"  
  48.                 android:text="議程時長:5:00"  
  49.                 android:layout_width="match_parent"  
  50.                 android:layout_height="0dp"  
  51.                 android:layout_weight="1"/>  
  52.             <RelativeLayout  
  53.                 android:layout_width="match_parent"  
  54.                 android:layout_height="0dp"  
  55.                 android:layout_weight="1">  
  56.   
  57.                 <Button  
  58.                     android:id="@+id/id_btn_show_agenda_file"  
  59.                     android:text="查看文件"  
  60.                     android:layout_centerInParent="true"  
  61.                     android:layout_width="100dp"  
  62.                     android:layout_height="50dp" />  
  63.   
  64.             </RelativeLayout>  
  65.   
  66.         </LinearLayout>  
  67.     </LinearLayout>  
  68.     <View  
  69.         android:layout_width="match_parent"  
  70.         android:layout_height="50dp">  
  71.     </View>  
  72.   
  73. </LinearLayout>  
運行的效果我就不說了…擠在一起的item,醜陋不堪,完全和我預想的效果不一樣!然後我試了一下wrap_content,我驚訝的發現,真的一模一樣……所以說我的match_parent竟然被改成了wrap_content!

網上查了一圈,我發現我的adapter代碼和別人的不一樣!於是,我機智地改成了這樣:

[java] view plain copy
  1. @Override  
  2.     public AgendaDetailHolder onCreateViewHolder(ViewGroup parent, int viewType) {  
  3.         View view = mInflater.inflate(R.layout.item_agenda_detail, parent, false);  
  4.         AgendaDetailHolder holder = new AgendaDetailHolder(view);  
  5.         return holder;  
  6.     }  
運行了一遍之後,我發現竟然可以了!我的效果又設置成功了,match_parent又有了效果!

但我強烈地想知道,到底爲什麼有了效果…所以我開始了查找資料的過程,源碼中是這麼寫的

[java] view plain copy
  1. public View inflate(int resource, ViewGroup root) {    
  2.     return inflate(resource, root, root != null);    
  3. }    
[java] view plain copy
  1. public View inflate(int resource, ViewGroup root, boolean attachToRoot) {    
  2.     if (DEBUG) System.out.println("INFLATING from resource: " + resource);    
  3.     XmlResourceParser parser = getContext().getResources().getLayout(resource);    
  4.     try {    
  5.         return inflate(parser, root, attachToRoot);    
  6.     } finally {    
  7.         parser.close();    
  8.     }    
  9. }    

這是前兩種的調用,它們最終都會調用第四個重載,所以我們關注這個即可

[java] view plain copy
  1. public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot) {  
  2.         synchronized (mConstructorArgs) {  
  3.             Trace.traceBegin(Trace.TRACE_TAG_VIEW, "inflate");  
  4.    
  5.             final AttributeSet attrs = Xml.asAttributeSet(parser);  
  6.             Context lastContext = (Context)mConstructorArgs[0];  
  7.             mConstructorArgs[0] = mContext;  
  8.             View result = root;  
  9.    
  10.             try {  
  11.                 // Look for the root node.  
  12.                 int type;  
  13.                 while ((type = parser.next()) != XmlPullParser.START_TAG &&  
  14.                         type != XmlPullParser.END_DOCUMENT) {  
  15.                     // Empty  
  16.                 }  
  17.    
  18.                 if (type != XmlPullParser.START_TAG) {  
  19.                     throw new InflateException(parser.getPositionDescription()  
  20.                             + ": No start tag found!");  
  21.                 }  
  22.    
  23.                 final String name = parser.getName();  
  24.                    
  25.                 if (DEBUG) {  
  26.                     System.out.println("**************************");  
  27.                     System.out.println("Creating root view: "  
  28.                             + name);  
  29.                     System.out.println("**************************");  
  30.                 }  
  31.    
  32.                 if (TAG_MERGE.equals(name)) {  
  33.                     if (root == null || !attachToRoot) {  
  34.                         throw new InflateException("<merge> can be used only with a valid "  
  35.                                 + "ViewGroup root and attachToRoot=true");  
  36.                     }  
  37.    
  38.                     rInflate(parser, root, attrs, false);  
  39.                 } else {  
  40.                     // Temp is the root view that was found in the xml  
  41.                     View temp;  
  42.                     if (TAG_1995.equals(name)) {  
  43.                         temp = new BlinkLayout(mContext, attrs);  
  44.                     } else {  
  45.                         temp = createViewFromTag(root, name, attrs);  
  46.                     }  
  47.    
  48.                     ViewGroup.LayoutParams params = null;  
  49.    
  50.                     if (root != null) {  
  51.                         if (DEBUG) {  
  52.                             System.out.println("Creating params from root: " +  
  53.                                     root);  
  54.                         }  
  55.                         // Create layout params that match root, if supplied  
  56.                         params = root.generateLayoutParams(attrs);  
  57.                         if (!attachToRoot) {  
  58.                             // Set the layout params for temp if we are not  
  59.                             // attaching. (If we are, we use addView, below)  
  60.                             temp.setLayoutParams(params);  
  61.                         }  
  62.                     }  
  63.    
  64.                     if (DEBUG) {  
  65.                         System.out.println("-----> start inflating children");  
  66.                     }  
  67.                     // Inflate all children under temp  
  68.                     rInflate(parser, temp, attrs, true);  
  69.                     if (DEBUG) {  
  70.                         System.out.println("-----> done inflating children");  
  71.                     }  
  72.    
  73.                     // We are supposed to attach all the views we found (int temp)  
  74.                     // to root. Do that now.  
  75.                     if (root != null && attachToRoot) {  
  76.                         root.addView(temp, params);  
  77.                     }  
  78.    
  79.                     // Decide whether to return the root that was passed in or the  
  80.                     // top view found in xml.  
  81.                     if (root == null || !attachToRoot) {  
  82.                         result = temp;  
  83.                     }  
  84.                 }  
  85.    
  86.             } catch (XmlPullParserException e) {  
  87.                 InflateException ex = new InflateException(e.getMessage());  
  88.                 ex.initCause(e);  
  89.                 throw ex;  
  90.             } catch (IOException e) {  
  91.                 InflateException ex = new InflateException(  
  92.                         parser.getPositionDescription()  
  93.                         + ": " + e.getMessage());  
  94.                 ex.initCause(e);  
  95.                 throw ex;  
  96.             } finally {  
  97.                 // Don't retain static reference on context.  
  98.                 mConstructorArgs[0] = lastContext;  
  99.                 mConstructorArgs[1] = null;  
  100.             }  
  101.    
  102.             Trace.traceEnd(Trace.TRACE_TAG_VIEW);  
  103.    
  104.             return result;  
  105.         }  
  106.     }  

代碼很長,關鍵是這一段
[java] view plain copy
  1. ViewGroup.LayoutParams params = null;    
  2.     
  3. if (root != null) {    
  4.     if (DEBUG) {    
  5.         System.out.println("Creating params from root: " +    
  6.                 root);    
  7.     }    
  8.     // Create layout params that match root, if supplied    
  9.     params = root.generateLayoutParams(attrs);    
  10.     if (!attachToRoot) {    
  11.         // Set the layout params for temp if we are not    
  12.         // attaching. (If we are, we use addView, below)    
  13.         temp.setLayoutParams(params);    
  14.     }    
  15. }    

我們的root終於出現了,意思就是當root不是空的時候,並且你的attachToRoot位false,就把root的參數設置給temp,那root的實參parent是啥呢?在這個

[java] view plain copy
  1. <pre name="code" class="java">public AgendaDetailHolder onCreateViewHolder(ViewGroup parent, int viewType)  


函數中,parent就是RecyclerView,即你item的父試圖,你創建的佈局的即我們設置的layout的參數的計算都要依賴於這個父視圖,而沒有這個父視圖(null)等於告訴框架你不需要父視圖去添加你的view

那這個temp是幹嘛的,我們接着看代碼

[java] view plain copy
  1. // Decide whether to return the root that was passed in or the  
  2.                     // top view found in xml.  
  3.                     if (root == null || !attachToRoot) {  
  4.                         result = temp;  
  5.                     }  
而這個result最終就是我們的返回結果,那我們的參數就成功的得到了計算(因爲有了父視圖,match_parent被計算了大小,就是你父視圖,即你的RecyclerView的大小),那我想要的效果就得到設置!
通過這一行行的解釋,我們終於弄明白了!root的作用就是來計算我們item的設置參數,沒有root自然也沒辦法計算參數,那我們設置什麼都不會有效果!可能我的理解會有一點偏差,不過起碼也算知道了我們的設置爲什麼沒有用的原因,後幾篇文章我想對LayoutInflater做一個根本的解析,真正理解這個大師的原理。
發佈了456 篇原創文章 · 獲贊 19 · 訪問量 42萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章