根据读源码可知,所有显示联系人页面是在DefaultContactBrowseListFragment类,在其类中我们可一发现 private void addEmptyUserProfileHeader(LayoutInflater inflater)这样一个方法,其方法的作用就是动态的添加ListView(显示联系人)以上的控件,所以我们想要在联系人以上添加模块就要了解,其方法里动态添加模块的流程,
方法的里
ListView list = getListView();
// Put a header with the "ME" name and a view for the number of contacts
// The view is embedded in a frame view since you cannot change the visibility of a
// view in a ListView without having a parent view.
mProfileHeaderContainer = new FrameLayout(inflater.getContext());
mProfileHeaderContainerCall = new FrameLayout(inflater.getContext());
mProfileHeader = inflater.inflate(R.layout.user_profile_header, null, false);
mProfileHeaderCall = inflater.inflate(R.layout.user_profile_header_call, null, false);
mCounterHeaderView = (TextView) mProfileHeader.findViewById(R.id.contacts_count);
mProfileTitle = (TextView) mProfileHeader.findViewById(R.id.profile_title);
mProfileHeaderContainer.addView(mProfileHeader);
mProfileHeaderContainerCall.addView(mProfileHeaderCall);
list.addHeaderView(mProfileHeaderContainer, null, false);
// Put a header with the "ME" name and a view for the number of contacts
// The view is embedded in a frame view since you cannot change the visibility of a
// view in a ListView without having a parent view.
mProfileHeaderContainer = new FrameLayout(inflater.getContext());
mProfileHeaderContainerCall = new FrameLayout(inflater.getContext());
mProfileHeader = inflater.inflate(R.layout.user_profile_header, null, false);
mProfileHeaderCall = inflater.inflate(R.layout.user_profile_header_call, null, false);
mCounterHeaderView = (TextView) mProfileHeader.findViewById(R.id.contacts_count);
mProfileTitle = (TextView) mProfileHeader.findViewById(R.id.profile_title);
mProfileHeaderContainer.addView(mProfileHeader);
mProfileHeaderContainerCall.addView(mProfileHeaderCall);
list.addHeaderView(mProfileHeaderContainer, null, false);
是添加头控件的标题所以我们可以在这个地方添加我们自己想要的模块
mMessageContainer = new FrameLayout(inflater.getContext());
mMessageContainerCall = new FrameLayout(inflater.getContext());
mMessageContainerFire = new FrameLayout(inflater.getContext());
mProfileMessage = (Button)inflater.inflate(R.layout.user_profile_button, null, false);
mProfileMessageCall = (Button)inflater.inflate(R.layout.user_profile_button_call, null, false);
mProfileMessageFire = (Button)inflater.inflate(R.layout.user_profile_button_fire, null, false);
mMessageContainer.addView(mProfileMessage);
mMessageContainerCall.addView(mProfileMessageCall);
mMessageContainerFire.addView(mProfileMessageFire);
list.addHeaderView(mMessageContainer, null, true);
list.addHeaderView(mProfileHeaderContainerCall, null, false);
list.addHeaderView(mMessageContainerCall, null, true);
list.addHeaderView(mMessageContainerFire, null, true);
mProfileMessage.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI);
intent.putExtra(ContactEditorFragment.INTENT_EXTRA_NEW_LOCAL_PROFILE, true);
startActivity(intent);
}
});
mProfileMessageCall.setOnClickListener(new View.OnClickListener(){
public void onClick(View arg0) {
Uri uri = Uri.parse("tel:110");
Intent intent = new Intent(Intent.ACTION_DIAL,uri);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
再有就是可以添加其他的控件还有事件监听,来完成自己的需求。
mMessageContainerCall = new FrameLayout(inflater.getContext());
mMessageContainerFire = new FrameLayout(inflater.getContext());
mProfileMessage = (Button)inflater.inflate(R.layout.user_profile_button, null, false);
mProfileMessageCall = (Button)inflater.inflate(R.layout.user_profile_button_call, null, false);
mProfileMessageFire = (Button)inflater.inflate(R.layout.user_profile_button_fire, null, false);
mMessageContainer.addView(mProfileMessage);
mMessageContainerCall.addView(mProfileMessageCall);
mMessageContainerFire.addView(mProfileMessageFire);
list.addHeaderView(mMessageContainer, null, true);
list.addHeaderView(mProfileHeaderContainerCall, null, false);
list.addHeaderView(mMessageContainerCall, null, true);
list.addHeaderView(mMessageContainerFire, null, true);
mProfileMessage.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI);
intent.putExtra(ContactEditorFragment.INTENT_EXTRA_NEW_LOCAL_PROFILE, true);
startActivity(intent);
}
});
mProfileMessageCall.setOnClickListener(new View.OnClickListener(){
public void onClick(View arg0) {
Uri uri = Uri.parse("tel:110");
Intent intent = new Intent(Intent.ACTION_DIAL,uri);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});