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

原文:


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是:

  1. GroupBrowseListFragment.java(群组)  
  2. DefaultContactBrowseListFragment.java(所有联系人)  
  3. ContactTileListFragment.java(常用、收藏联系人)。  


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

  1. FragmentManager fragmentManager = getFragmentManager();   
  2. FragmentTransaction transaction = fragmentManager.beginTransaction()  
    FragmentManager fragmentManager = getFragmentManager(); 
    FragmentTransaction transaction = fragmentManager.beginTransaction()

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

  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);  
    transaction.add(R.id.tab_pager, mFavoritesFragment, FAVORITE_TAG);
    transaction.add(R.id.tab_pager, mAllFragment, ALL_TAG);
    transaction.add(R.id.tab_pager, mGroupsFragment, GROUPS_TAG);


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

  1. transaction.commitAllowingStateLoss();  
    transaction.commitAllowingStateLoss();

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

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

 

all_contacts_inheritance

 

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

  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.     }  
public View getView(int position, View convertView, ViewGroup parent) {
        ensureCacheValid();
        int start = 0;
        for (int i = 0; i < mSize; i++) {
            int end = start + mPartitions[i].count;
            if (position >= start && position < end) {
                int offset = position - start;
                if (mPartitions[i].hasHeader) {
                    offset--;
                }
                View view;
                if (offset == -1) {
                    view = getHeaderView(i, mPartitions[i].cursor, convertView, parent);
                } else {
                    if (!mPartitions[i].cursor.moveToPosition(offset)) {
                        throw new IllegalStateException("Couldn't move cursor to position "
                                + offset);
                    }
                    view = getView(i, mPartitions[i].cursor, offset, convertView, parent);
                }
                if (view == null) {
                    throw new NullPointerException("View should not be null, partition: " + i
                            + " position: " + offset);
                }
                return view;
            }
            start = end;
        }

        throw new ArrayIndexOutOfBoundsException(position);
    }


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

  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.     }  
protected View getView(int partition, Cursor cursor, int position, View convertView,
            ViewGroup parent) {
        View view;
        if (convertView != null) {
            view = convertView;
        } else {
            view = newView(mContext, partition, cursor, position, parent);
        }
        bindView(view, partition, cursor, position);
        return view;
    }


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

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

 

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

  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.     }  
protected View newView(Context context, int partition, Cursor cursor, int position,
            ViewGroup parent) {
    	
        ContactListItemView view = new ContactListItemView(context, null);
        view.setUnknownNameText(mUnknownNameText);
        view.setQuickContactEnabled(isQuickContactEnabled());
        view.setActivatedStateSupported(isSelectionVisible());
        return view;
    }


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

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

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

  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.     }  
protected void bindView(View itemView, int partition, Cursor cursor, int position) {
        final ContactListItemView view = (ContactListItemView)itemView;

        view.setHighlightedPrefix(isSearchMode() ? getUpperCaseQueryString() : null);

        if (isSelectionVisible()) {
            view.setActivated(isSelectedContact(partition, cursor));
        }

        bindSectionHeaderAndDivider(view, position, cursor);

        if (isQuickContactEnabled()) {
            bindQuickContact(view, partition, cursor, ContactQuery.CONTACT_PHOTO_ID,
                    ContactQuery.CONTACT_ID, ContactQuery.CONTACT_LOOKUP_KEY);
        } else {
            bindPhoto(view, partition, cursor);
        }

        bindName(view, cursor);
        bindPresenceAndStatusMessage(view, cursor);

        if (isSearchMode()) {
            bindSearchSnippet(view, cursor);
        } else {
            view.setSnippet(null);
        }
    }

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

 

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

  1. mView = inflateView(inflater, container);  
mView = inflateView(inflater, container);

inflateView的实现代码:

  1. protected View inflateView(LayoutInflater inflater, ViewGroup container) {  
  2.         return inflater.inflate(R.layout.contact_list_content, null);  
  3.     }  
protected View inflateView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.contact_list_content, null);
    }


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




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值