Android 4.0.3 联系人(通讯录)应用源码学习

来源:互联网 发布:ubuntu获取arm gcc 编辑:程序博客网 时间:2024/05/16 03:49
原文:


http://blog.csdn.net/zhu_apollo/article/details/8763055


Contacts应用入口类有2个:PeopleActivity.java和DialtactsActivity.java。PeopleActivity是联系人入口类,DialtactsActivity是拨号入口类,Contacts集成了联系人和拨号功能。Contacts主界面如图1所示:

all

每个Tab都和一个Fragment对应,当选择某个标签时,对应的Fragment就会被调用显示。三个Fragment是:

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. GroupBrowseListFragment.java(群组)  
  2. DefaultContactBrowseListFragment.java(所有联系人)  
  3. ContactTileListFragment.java(常用、收藏联系人)。  


在入口类PeopleActivity.java中,采用getFragmentManager()方法获得FragmentManager实例便于管理Fragment,比如将Fragment从后台堆栈中弹出等。FragmentTransaction可以用来隐藏、添加、移除Fragment等操作,使用FragmentManager的beginTransaction方法,代码: 

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. FragmentManager fragmentManager = getFragmentManager();   
  2. FragmentTransaction transaction = fragmentManager.beginTransaction()  

再用add方法将fragment生成的view添加到容器中,该容器嵌入在activity的布局中:

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. transaction.add(R.id.tab_pager, mFavoritesFragment, FAVORITE_TAG);  
  2. transaction.add(R.id.tab_pager, mAllFragment, ALL_TAG);  
  3. transaction.add(R.id.tab_pager, mGroupsFragment, GROUPS_TAG);  


要使得add、删除等方法生效,必须使用commit方法提交这些事务:

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. transaction.commitAllowingStateLoss();  

commitAllowingStateLoss和commit的区别是当退出activity时,防止提交后的状态丢失。

 mAllFragment是DefaultContactBrowseListFragment的实例,是“所有联系人”的fragment,他是多继承类,其结构如下图所示:

 

all_contacts_inheritance

 

从图看出,DefaultContactBrowseListFragment最终继承于Fragment,ContactListAdapter是与listview对应的自定义适配器,最终继承于CompositeCursorAdapter,而CompositeCursorAdapter实际上继承于BaseAdapter,系统采用了CompositeCursorAdapter方法的好处在于它已经实现了getView方法,并且采用了缓存技术,代码如下:

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. public View getView(int position, View convertView, ViewGroup parent) {  
  2.         ensureCacheValid();  
  3.         int start = 0;  
  4.         for (int i = 0; i < mSize; i++) {  
  5.             int end = start + mPartitions[i].count;  
  6.             if (position >= start && position < end) {  
  7.                 int offset = position - start;  
  8.                 if (mPartitions[i].hasHeader) {  
  9.                     offset--;  
  10.                 }  
  11.                 View view;  
  12.                 if (offset == -1) {  
  13.                     view = getHeaderView(i, mPartitions[i].cursor, convertView, parent);  
  14.                 } else {  
  15.                     if (!mPartitions[i].cursor.moveToPosition(offset)) {  
  16.                         throw new IllegalStateException("Couldn't move cursor to position "  
  17.                                 + offset);  
  18.                     }  
  19.                     view = getView(i, mPartitions[i].cursor, offset, convertView, parent);  
  20.                 }  
  21.                 if (view == null) {  
  22.                     throw new NullPointerException("View should not be null, partition: " + i  
  23.                             + " position: " + offset);  
  24.                 }  
  25.                 return view;  
  26.             }  
  27.             start = end;  
  28.         }  
  29.   
  30.         throw new ArrayIndexOutOfBoundsException(position);  
  31.     }  


它包含一个getView,具体实现是:  

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. protected View getView(int partition, Cursor cursor, int position, View convertView,  
  2.             ViewGroup parent) {  
  3.         View view;  
  4.         if (convertView != null) {  
  5.             view = convertView;  
  6.         } else {  
  7.             view = newView(mContext, partition, cursor, position, parent);  
  8.         }  
  9.         bindView(view, partition, cursor, position);  
  10.         return view;  
  11.     }  


它判断convertView是否为空,如果是,就用newView方法创建一个,如果不是,就采用已有的,这种方法无需每次都创建View对象,提高了效率。需要我们实现的只有2个:

bindView方法和newView方法。newView方法在ContactListAdapter中实现,bindView在DefaultContactListAdapter中实现。

 

newView是用来创建ListView的item布局的:

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. protected View newView(Context context, int partition, Cursor cursor, int position,  
  2.             ViewGroup parent) {  
  3.           
  4.         ContactListItemView view = new ContactListItemView(context, null);  
  5.         view.setUnknownNameText(mUnknownNameText);  
  6.         view.setQuickContactEnabled(isQuickContactEnabled());  
  7.         view.setActivatedStateSupported(isSelectionVisible());  
  8.         return view;  
  9.     }  


ContactListItemView继承ViewGroup,是每个item的布局,在这个布局中添加TextView显示联系人姓名,添加ImageView显示头像,再使用一个TextView显示姓名大写首字母。在ContactListItemView中,主要的实现方法有三个:

onMeasure, onLayout, dispatchDraw. onMeasure用来测量视图的大小尺寸,onLayout给视图布局,确定其位置,dispatchDraw起到分发的作用,正在画图的方法还是draw。

 如果说newView是用来创建每个Item的布局,那么bindView是用来创建布局上每个子视图。bindVIew的代码如下:

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. protected void bindView(View itemView, int partition, Cursor cursor, int position) {  
  2.         final ContactListItemView view = (ContactListItemView)itemView;  
  3.   
  4.         view.setHighlightedPrefix(isSearchMode() ? getUpperCaseQueryString() : null);  
  5.   
  6.         if (isSelectionVisible()) {  
  7.             view.setActivated(isSelectedContact(partition, cursor));  
  8.         }  
  9.   
  10.         bindSectionHeaderAndDivider(view, position, cursor);  
  11.   
  12.         if (isQuickContactEnabled()) {  
  13.             bindQuickContact(view, partition, cursor, ContactQuery.CONTACT_PHOTO_ID,  
  14.                     ContactQuery.CONTACT_ID, ContactQuery.CONTACT_LOOKUP_KEY);  
  15.         } else {  
  16.             bindPhoto(view, partition, cursor);  
  17.         }  
  18.   
  19.         bindName(view, cursor);  
  20.         bindPresenceAndStatusMessage(view, cursor);  
  21.   
  22.         if (isSearchMode()) {  
  23.             bindSearchSnippet(view, cursor);  
  24.         } else {  
  25.             view.setSnippet(null);  
  26.         }  
  27.     }  

其中:bindSectionHeaderAndDivider是创建第一行首字母和首字母下面蓝色下划线以及首字母右边的计数器;bindName创建姓名TextView;

 

ContactEntryListFragment中实现了Fragment的onCreateView方法,该方法创建Fragment对应的视图,创建代码是:

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. mView = inflateView(inflater, container);  

inflateView的实现代码:

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. protected View inflateView(LayoutInflater inflater, ViewGroup container) {  
  2.         return inflater.inflate(R.layout.contact_list_content, null);  
  3.     }  


布局文件contact_list_content.xml即是所有联系人的根布局。创建完视图后,采用setAdapter将适配器添加到ListView中即可。




0 0