Android Phonebook编写联系人UI加载及联系人保存流程(四)
2014-01-07 10:23:22 将百度空间里的东西移过来。
5. KindSectionView
KindSectionView是何方神圣呢?它又是怎么怎么和一个DataKind,以及一个RawContactDelta绑定到一起的呢?继续看,进入KindSectionView,其实KindSectionView就是一个普通的自定义View,他的作用还真的就是将DataKind中包含的数据变成UI显示出来,那么这个View是什么呢?它有自己的xml文件:item_kind_section.xml:
<com.android.contacts.editor.KindSectionView
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"> <include
android:id="@+id/kind_title_layout"
layout="@layout/edit_kind_title" /> <LinearLayout
android:id="@+id/kind_editors"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" /> <Button
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类,如下:
@Override
protected void onFinishInflate() {
setDrawingCacheEnabled(true);
setAlwaysDrawnWithCacheEnabled(true); mInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); mTitle = (TextView) findViewById(R.id.kind_title);
mEditors = (ViewGroup) findViewById(R.id.kind_editors);
mAddFieldFooter = findViewById(R.id.add_text);
mAddFieldFooter.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mAddFieldFooter.setVisibility(View.GONE);
addItem();
}
});
super.onFinishInflate();
}
不过我们还是继续沿着KindSectionView类setState()方法往下分析,rebuildFromState()-->createEditorView(),createEditorView()方法具体加载了每一个DataKind,代码如下:
private View createEditorView(ValuesDelta entry) {
final View view;
try {
view = mInflater.inflate(mKind.editorLayoutResourceId, mEditors, false);
} catch (Exception e) {
}
view.setEnabled(isEnabled());
if (view instanceof Editor) {
Editor editor = (Editor) view;
editor.setDeletable(true);
editor.setValues(mKind, entry, mState, mReadOnly, mViewIdGenerator);
editor.setEditorListener(this);
}
mEditors.addView(view);
return view;
}
首先是mKind,这个值就是section.setState(kind, state, false, vig)传过来的,它先取出editorLayoutResourceId,然后解析为View,并绑定在mEditors上,然后再操作,那么editorLayoutResourceId是什么?我们回过头去看DataKind:
public DataKind() {
editorLayoutResourceId = R.layout.text_fields_editor_view;
maxLinesForDisplay = 1;
}
public DataKind(String mimeType, int titleRes, int weight, boolean editable,
int editorLayoutResourceId) {
this.mimeType = mimeType;
this.titleRes = titleRes;
this.weight = weight;
this.editable = editable;
this.typeOverallMax = -1;
this.editorLayoutResourceId = editorLayoutResourceId;
maxLinesForDisplay = 1;
}
DataKind有两个默认的构造器,无参构造器设置了默认的text_fields_editor_view,以LocalAccountType为例:
protected DataKind addDataKindStructuredName(
Context context) throws DefinitionException {
DataKind kind = addKind(new DataKind(StructuredName.CONTENT_ITEM_TYPE,
R.string.nameLabelsGroup, -1, true,
R.layout.structured_name_editor_view));
添加Name时,使用R.layout.structured_name_editor_view:
protected DataKind addDataKindNickname(
Context context) throws DefinitionException {
DataKind kind = addKind(new DataKind(Nickname.CONTENT_ITEM_TYPE,
R.string.nicknameLabelsGroup, 115, true,
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:
<com.android.contacts.editor.TextFieldsEditorView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"> <LinearLayout
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"> <include
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" /> <include
layout="@layout/edit_field_list_with_anchor_view" /> <FrameLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"> <include
android:id="@+id/expansion_view_container"
layout="@layout/edit_expansion_view"
android:visibility="gone" /> <include
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()方法:
@Override
public void setValues(DataKind kind, ValuesDelta entry, RawContactDelta state, boolean readOnly, ViewIdGenerator vig) {
super.setValues(kind, entry, state, readOnly, vig);
int fieldCount = kind.fieldList.size();
mFieldEditTexts = new EditText[fieldCount];
for (int index = 0; index < fieldCount; index++) {
final EditField field = kind.fieldList.get(index);
final EditText fieldView = createFieldView(field.column);
fieldView.setTextAppearance(getContext(), android.R.style.TextAppearance_Medium);
mFieldEditTexts[index] = fieldView; fieldView.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
// Trigger event for newly changed value
onFieldChanged(column, s.toString());
} }); mFields.addView(fieldView);
} }
首先调用了父类的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,如下:
<LinearLayout
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1" android:orientation="vertical">
<LinearLayout
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" />
<View
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的布局:
<include
android:id="@+id/edit_name"
layout="@layout/structured_name_editor_view" />
进入structured_name_editor_view.xml:
<com.android.contacts.editor.StructuredNameEditorView
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"> <include
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" /> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:clickable="true"> <include
layout="@layout/edit_field_list_with_anchor_view"/> <include
android:id="@+id/expansion_view_container"
layout="@layout/edit_expansion_view"
android:visibility="gone" /> <include
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设置背景,以及其他属性,来达到定制的需求。
Android Phonebook编写联系人UI加载及联系人保存流程(四)的更多相关文章
- Android Phonebook编写联系人UI加载及联系人保存流程(一)
2014-01-06 17:05:11 将百度空间里的东西移过来. 本文适合ROM定制做Phonebook的童鞋看,其他人飘过即可- Phonebook添加/编辑联系人UI加载及保存联系人流程,是一系 ...
- Android Phonebook编写联系人UI加载及联系人保存流程(三)
2014-01-07 09:54:13 将百度空间里的东西移过来. 本文从点击“添加联系人”Button开始,分析新建联系人页面UI是如何加载,以及新的联系人信息是如何保存的,借此,我们一探Phon ...
- Android Phonebook编写联系人UI加载及联系人保存流程(五)
2014-01-07 10:46:30 将百度空间里的东西移过来. 在前面的文章中我们分析了UI的加载,其中提到了一个重要的对象:RawContactDeltaList mState,我前面说过这个对 ...
- Android Phonebook编写联系人UI加载及联系人保存流程(二)
2014-01-06 17:18:29 1. Phonebook中新建/编辑联系人的UI不是用xml文件写的,它是随着帐号类型的改变来加载不同的UI,比如SIM联系人,只有Name.Phone Num ...
- Android Phonebook编写联系人UI加载及联系人保存流程(六)
2014-01-07 11:18:08 将百度空间里的东西移过来. 1. Save contact 我们前面已经写了四篇文章,做了大量的铺垫,总算到了这一步,见证奇迹的时刻终于到了. 用户添加了所有需 ...
- Android ViewPager Fragment使用懒加载提升性能
Android ViewPager Fragment使用懒加载提升性能 Fragment在如今的Android开发中越来越普遍,但是当ViewPager结合Fragment时候,由于Androi ...
- Android三种基本的加载网络图片方式(转)
Android三种基本的加载网络图片方式,包括普通加载网络方式.用ImageLoader加载图片.用Volley加载图片. 1. [代码]普通加载网络方式 ? 1 2 3 4 5 6 7 8 9 10 ...
- Android开发中如何解决加载大图片时内存溢出的问题
Android开发中如何解决加载大图片时内存溢出的问题 在Android开发过程中,我们经常会遇到加载的图片过大导致内存溢出的问题,其实类似这样的问题已经屡见不鲜了,下面将一些好的解决方案分享给 ...
- Android引入高速缓存的异步加载全分辨率
Android引进高速缓存的异步加载全分辨率 为什么要缓存 通过图像缩放,我们这样做是对的异步加载优化的大图,但现在的App这不仅是一款高清大图.图.动不动就是图文混排.以图代文,假设这些图片都载入到 ...
随机推荐
- 关于jQuery的小知识点
jQuery 简介 jQuery 库可以通过一行简单的标记被添加到网页中. jQuery 库 - 特性 jQuery 是一个 JavaScript 函数库. jQuery 库包含以下特性: HTML ...
- android 回调函数的使用
// activity 之间方法调用的桥梁 public class ActivityCallBridge { static ActivityCallBridge mBridge; private O ...
- hdu 1058 Humble Numbers
这题应该是用dp来做的吧,但一时不想思考了,写了个很暴力的,类似模拟打表,然后排序即可,要注意的是输出的格式,在这里wa了一发,看了别人的代码才知道哪些情况没考虑到. #include<cstd ...
- 扩展spring data jpa的数据更新方法时注意事项
//此处必须加@Transactional,否则不能运行,报错 @Transactional @Modifying @Query("update ExamItem a set a.versi ...
- Java集合的Stack、Queue、Map的遍历
Java集合的Stack.Queue.Map的遍历 在集合操作中,常常离不开对集合的遍历,对集合遍历一般来说一个foreach就搞定了,但是,对于Stack.Queue.Map类型的遍历,还是有一 ...
- 基于 php-redis 的redis操作
基于 php-redis 的redis操作 林涛 发表于:2016-5-13 12:12 分类:PHP 标签:php,php-redis,redis 203次 redis的操作很多的,下面的例子都是基 ...
- oracel数据库基本知识和基本查询方法
Oracel数据库学习1.小型数据库 access,foxbase 中型数据库 mysql,sqlserver,informix 大型数据库 sybase,Oracle,db2 Oracle数据库需求 ...
- ajax正确的简单封装“姿势”
window.meng = window.meng || {}; (function ($) { function getAjaxDate(url, apikey) { var datas; $.aj ...
- 发布完ArcGIS地图服务后,服务未启动成功
今天下午更新地图服务后,服务未启动成功.出来的弹出框警告问题目前应该是ArcGIS Server出了问题,打开ArcCatalog目录,查看GIS服务器下localhost下的服务,只要是今天发布的服 ...
- cf(#div1 B. Dreamoon and Sets)(数论)
B. Dreamoon and Sets time limit per test 1 second memory limit per test 256 megabytes input standard ...