Android Phonebook編寫連絡人UI載入及連絡人儲存流程(四)

來源:互聯網
上載者:User

2014-01-07 10:23:22 將百度空間裡的東西移過來。

5. KindSectionView

KindSectionView是何方神聖呢?它又是怎麼怎麼和一個DataKind,以及一個RawContactDelta綁定到一起的呢?繼續看,進入KindSectionView,其實KindSectionView就是一個普通的自訂View,他的作用還真的就是將DataKind中包含的資料變成UI顯示出來,那麼這個View是什麼呢?它有自己的xml檔案:item_kind_section.xml:

 <     xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:layout_marginBottom="@dimen/raw_contact_sect_fields_margin_bottom"     android:orientation="vertical">         <         android:id="@+id/kind_title_layout"         layout="@layout/edit_kind_title" />         <         android:id="@+id/kind_editors"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:orientation="vertical" />         <         android:id="@+id/add_text"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="start"         android:text="@string/add_new_entry_for_section"         android:drawableStart="@drawable/spb_plus_icn" />     </com.android.contacts.editor.KindSectionView>

對照這圖片我們分析:

kind_title_layout:就是中的Phone那個View;

add_text:就是中的“Add new” Button;

kind_editors:就是介於這兩者之間的部分。當然,對add_text的點擊事件處理也在KindSectionView類,如下:

        setDrawingCacheEnabled(     setAlwaysDrawnWithCacheEnabled(         mInflater =         mTitle =     mEditors =     mAddFieldFooter =     mAddFieldFooter.setOnClickListener(                     }

不過我們還是繼續沿著KindSectionView類setState()方法往下分析,rebuildFromState()-->createEditorView(),createEditorView()方法具體載入了每一個DataKind,代碼如下:

                    view = mInflater.inflate(mKind.editorLayoutResourceId, mEditors,      }                 (view          Editor editor =         editor.setDeletable(          editor.setEditorListener(        }

首先是mKind,這個值就是section.setState(kind, state, false, vig)傳過來的,它先取出editorLayoutResourceId,然後解析為View,並綁定在mEditors上,然後再操作,那麼editorLayoutResourceId是什嗎?我們回過頭去看DataKind:

      editorLayoutResourceId =     maxLinesForDisplay = 1       DataKind(String mimeType,  titleRes,  weight,               .mimeType =     .titleRes =     .weight =     .editable =     .typeOverallMax = -1     .editorLayoutResourceId =     maxLinesForDisplay = 1 }

DataKind有兩個預設的構造器,無參構造器設定了預設的text_fields_editor_view,以LocalAccountType為例:

      Context context)      DataKind kind = addKind(             R.string.nameLabelsGroup, -1,              R.layout.structured_name_editor_view));

添加Name時,使用R.layout.structured_name_editor_view:

      Context context)      DataKind kind = addKind(                 R.string.nicknameLabelsGroup, 115,                  R.layout.text_fields_editor_view));

添加NickName, Phone, Email等時都是R.layout.text_fields_editor_view。回來接著看createEditorView()方法,

editorLayoutResourceId有了,剩下的就是把這個id對應的layout顯示出來,看

editor.setValues(mKind, entry, mState, mReadOnly, mViewIdGenerator);看兩個xml檔案:

structured_name_editor_view.xml 這個是專門針對Name的,後面再說,先看text_fields_editor_view.xml:

 <     xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:orientation="vertical">         <         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:layout_weight="1"         android:orientation="horizontal"         android:gravity="center_vertical"         android:focusable="true"         android:clickable="true">             <             layout="@layout/edit_spinner"             android:layout_width="@dimen/editor_type_label_width"             android:layout_height="@dimen/editor_min_line_item_height"             android:layout_gravity="top"             android:layout_marginEnd="@dimen/raw_contact_edit_spinner_margin_end"             android:visibility="gone" />             <             layout="@layout/edit_field_list_with_anchor_view" />             <             android:layout_width="wrap_content"             android:layout_height="match_parent">                 <                 android:id="@+id/expansion_view_container"                 layout="@layout/edit_expansion_view"                 android:visibility="gone" />                 <                 android:id="@+id/delete_button_container"                 layout="@layout/edit_delete_button"                 android:visibility="gone" />             </FrameLayout>         </LinearLayout>     </com.android.contacts.editor.TextFieldsEditorView>

edit_spinner:中“Home” Spinner;

expansion_view_container:展開或摺疊Button,這個只有在Postal Address和Name中用到;

delete_button_container:中的叉;

TextFieldsEditorView類的繼承關係

我們進入TextFieldsEditorView的setValues()方法:

    setValues(DataKind kind, ValuesDelta entry, RawContactDelta state,            fieldCount =     mFieldEditTexts =       ( index = 0; index < fieldCount; index++          EditField field =          EditText fieldView =          mFieldEditTexts[index] =                 fieldView.addTextChangedListener(                                                  }

首先調用了父類的setValues()方法,在父類的setValues()方法中對一些UI賦值,如Spinner mLabel,給他設定了一個Adapter,值就是kind.typeList中包含的內容,前文中提到過。

我們繼續看TextFieldsEditorView的setValues()方法,有個for迴圈,將kind.fieldList中所有的field取出來,並建立一個對應的EditText fieldView,然後添加到mFields,mFields = (ViewGroup) findViewById(R.id.editors),來自text_fields_editor_view.xml --> edit_field_list_with_anchor_view.xml,如下:

 <     android:layout_width="0dip"     android:layout_height="wrap_content"     android:layout_weight="1"         android:orientation="vertical">     <          android:id="@+id/editors"          android:layout_width="match_parent"          android:layout_height="wrap_content"          android:orientation="vertical"          android:focusable="true"          android:focusableInTouchMode="true"          android:descendantFocusability="afterDescendants" />     <          android:id="@+id/anchor_view"          android:layout_width="match_parent"          android:layout_height="0px" /> </LinearLayout>

說白了,editors這個id對應的View就是用來添加一個EditText。

稍微總結一下:

最重要的是RawContactCommonEditorView類setState()方法,其中的for迴圈,作用就是將賬戶中的每一個kind取出來,然後產生一個KindSectionView,並顯示出來。而每一個KindSectionView裡面,又有可能包含多個field,這取決於kind中fieldlist中添加的EditField的個數,如,就包含2個EditField。

至此,Phone UI的顯示部分完成,具體的細節還得要仔細看代碼,比如Spinner的點擊事件,叉的點擊事件,文中有提到,但是具體的還得研究代碼,很簡單,至於EditText添加的TextChangedListener的作用,這個設計到Save contact,我們先記住每個EditText都有一個TextChangedListener就好,到分析Save Contact流程時再說。

6. 添加Name UI

前面提到Name UI,我們這裡仔細分析它。

在raw_contact_editor_view.xml中,有專門添加Name相關UI的布局:

 <     android:id="@+id/edit_name"     layout="@layout/structured_name_editor_view" />

進入structured_name_editor_view.xml:

 <     xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:minHeight="@dimen/editor_min_line_item_height"     android:orientation="vertical">         <         layout="@layout/edit_spinner"         android:layout_width="@dimen/editor_type_label_width"         android:layout_height="@dimen/editor_min_line_item_height"         android:layout_gravity="top"         android:visibility="gone" />         <         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:orientation="horizontal"         android:clickable="true">             <             layout="@layout/edit_field_list_with_anchor_view"/>             <             android:id="@+id/expansion_view_container"             layout="@layout/edit_expansion_view"             android:visibility="gone" />             <             android:id="@+id/delete_button_container"             layout="@layout/edit_delete_button"             android:visibility="gone" />     </LinearLayout>     </com.android.contacts.editor.StructuredNameEditorView>

發現這個檔案和text_fields_editor_view.xml很像,當然了,看StructuredNameEditorView類的繼承圖,發現StructuredNameEditorView繼承自TextFieldsEditorView,所以啊,他的邏輯其實也在TextFieldsEditorView裡面,和Phone一樣,只不過因為StructedName比較特殊,比如沒有前面的Title, 後面的“Add new”和Spinner等。我以前做這塊的UI定製的時候,需要改變頁面中所有的EditText的效果,尤其是有多個EditText,比如Name, Address展開後的效果,其實改動還是在TextFieldsEditorView的setValues()方法中,對EditText設定背景,以及其他屬性,來達到定製的需求。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.