项目中有用到联系人,就研究了下,参考了别人的demo,并且加上了自己的一些想法跟ui效果.有这方面需求的朋友可以拿去参考一下.
联系人字母排序参考文章,我也是参考他的:http://blog.csdn.net/xiaanming/article/details/12684155
我的项目实现了联系人排序,侧滑可以弹出联系人分组,以及添加联系人,删掉了搜索功能.增加了头像跟电话号码展示.
首先看下效果图片:
1.主MainActivity用来展示SlidingMenu SlidingMenu加载左侧fragment(GroupFragment) 跟 中间的fragment (ContactFragment)
注意: 把ContactFragment对象注入到GroupFragment中,因为点击群组的时候要更新ContactFragment界面.
- public class MainActivity extends FragmentActivity {
- private SlidingMenu mSlidingMenu;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.contact_main);
- initViews();
- ImageView showGroup=(ImageView) findViewById(R.id.show_group);
- showGroup.setOnClickListener(listener);
- ImageView addContact=(ImageView) findViewById(R.id.add_contact);
- addContact.setOnClickListener(listener);
- }
- private void initViews() {
- mSlidingMenu = (SlidingMenu) findViewById(R.id.slidingmenu);
- mSlidingMenu.setContent(R.layout.contact_fragment);//中间布局
- mSlidingMenu.setMenu(R.layout.group_fragment);//左边布局
- ContactFragment contactFragment=new ContactFragment();
- FragmentTransaction ft=getSupportFragmentManager().beginTransaction();
- ft.replace(R.id.contacts_fragment,contactFragment);
- ft.replace(R.id.group_fragment,new GroupFragment(contactFragment));
- ft.commit();
- }
- private OnClickListener listener=new OnClickListener() {
- @Override
- public void onClick(View v){
- switch (v.getId()) {
- case R.id.show_group:
- mSlidingMenu.toggle();//动态判断自动关闭或开启SlidingMenu
- break;
- case R.id.add_contact:
- Intent intent = new Intent(Intent.ACTION_INSERT,Contacts.CONTENT_URI);
- startActivityForResult(intent,108);
- break;
- }
- }
- };
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- if (108 == requestCode){
- Toast.makeText(MainActivity.this, "如果操作数据,请重新打开联系人...",Toast.LENGTH_LONG).show();
- }
- super.onActivityResult(requestCode, resultCode, data);
- }
- @Override
- public boolean onKeyUp(int keyCode, KeyEvent event) {
- if (keyCode == KeyEvent.KEYCODE_BACK && mSlidingMenu.isMenuShowing()) {
- mSlidingMenu.showContent();
- return true;
- }
- return super.onKeyUp(keyCode, event);
- }
- }
2. ContactFragment (中间内容显示的fragment) 初始化显示所有联系人.
给SideBar添加监听事件,dialog的注入,ListView注入adapter,很简单的,相信大家都能看懂
- public class ContactFragment extends Fragment implements GroupItemClick{
- private List<SWWContact> contactList;
- private Activity activity;
- private ListView sortListView;
- private ContactsAdapter adapter;
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- Log.i("fragment","onCreateView:"+getClass().getSimpleName());
- activity=getActivity();
- View mView = inflater.inflate(R.layout.contact_list, null);
- SideBar sideBar = (SideBar) mView.findViewById(R.id.sidrbar);
- TextView dialog = (TextView) mView.findViewById(R.id.dialog);
- sideBar.setTextView(dialog);
- //为ListView设置adapter
- sortListView = (ListView) mView.findViewById(R.id.country_lvcountry);
- contactList=SWWContactDB.findAllContacts(activity.getContentResolver());
- adapter = new ContactsAdapter(activity,contactList);
- sortListView.setAdapter(adapter);
- //设置右侧触摸监听
- sideBar.setOnTouchingLetterChangedListener(new OnTouchingLetterChangedListener() {
- @Override
- public void onTouchingLetterChanged(String s) {
- //该字母首次出现的位置
- int position = adapter.getPositionForSection(s.charAt(0));
- if(position != -1){
- sortListView.setSelection(position);
- }
- }
- });
- return mView;
- }
- @Override
- public void itemClick(List<SWWContact> contacts) {
- adapter.updateListView(contacts);
- }
- }
3.GroupFragment 展示左侧群组数据,加载所有群组,以及群组点击事件,刷新右边联系人.
- @SuppressLint("ValidFragment")
- public class GroupFragment extends Fragment {
- private Activity activity;
- private List<SWWContactsGroup> groupList=new ArrayList<SWWContactsGroup>();
- private GroupItemClick groupClick;
- public GroupFragment(){
- }
- public GroupFragment(GroupItemClick groupClick){
- this.groupClick=groupClick;
- }
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
- Log.i("fragment", "onCreateView:"+getClass().getSimpleName());
- activity=getActivity();
- this.groupList = SWWContactDB.queryGroup(activity);//得到所有组数据
- View view = inflater.inflate(R.layout.group_list, null);
- ListView groupListView=(ListView)view.findViewById(R.id.group_list);
- groupListView.setAdapter(new MyAdapter());
- return view;
- }
- /**
- * 左侧联系人分组 点击回调接口
- */
- public interface GroupItemClick{
- public void itemClick(List<SWWContact> contacts);
- }
- private class MyAdapter extends BaseAdapter{
- private boolean isPressed[];
- public MyAdapter(){
- if(groupList.size()>0){
- isPressed = new boolean[groupList.size()];
- isPressed[0]=true;
- }
- }
- @Override
- public int getCount() {
- return groupList.size();
- }
- @Override
- public Object getItem(int position) {
- return groupList.get(position);
- }
- @Override
- public long getItemId(int position) {
- return groupList.get(position).getGroupId();
- }
- @Override
- public View getView(final int position, View convertView, ViewGroup parent) {
- ViewHolder holder;
- if(null==convertView){
- holder=new ViewHolder();
- convertView=View.inflate(activity,R.layout.group_list_item, null);
- holder.groupName=(TextView) convertView.findViewById(R.id.group_name);
- convertView.setTag(holder);
- }else{
- holder=(ViewHolder) convertView.getTag();
- }
- holder.groupName.setText(groupList.get(position).getGroupName());
- holder.groupName.setBackgroundResource(isPressed[position]==true?R.color.light_blue:R.color.group_list);
- holder.groupName.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- changeState(position);
- notifyDataSetChanged();
- if(position==0){//点击全部按钮
- groupClick.itemClick(SWWContactDB.findAllContacts(activity.getContentResolver()));
- }else{
- int groupId=groupList.get(position).getGroupId();
- groupClick.itemClick(SWWContactDB.queryGroupMember(groupId,activity));
- }
- }
- });
- return convertView;
- }
- private void changeState(int position){
- isPressed=new boolean[isPressed.length];
- isPressed[position] = true;
- }
- private class ViewHolder{
- private TextView groupName;
- }
- }
- }
还有一些访问数据库, 以及一些工具类,我就不一一贴出来了,需要的要自己去下载源码......如果大家有什么疑问,请在下面留言.