@Override
 protected void initView() {
   super.initView();
   View headerView =
       LayoutInflater.from(getActivity())
           .inflate(com.xuhai.telescopes.R.layout.em_contacts_header, null);
   HeaderItemClickListener clickListener = new HeaderItemClickListener();
   applicationItem =
       (ContactItemView) headerView.findViewById(com.xuhai.telescopes.R.id.application_item);
   applicationItem.setOnClickListener(clickListener);
   headerView.findViewById(com.xuhai.telescopes.R.id.group_item).setOnClickListener(clickListener);
   headerView
       .findViewById(com.xuhai.telescopes.R.id.chat_room_item)
       .setOnClickListener(clickListener);
   //
   // headerView.findViewById(com.xuhai.telescopes.R.id.robot_item).setOnClickListener(clickListener);
   // 添加headerview
   //        listView.addHeaderView(headerView);
   // 添加正在加载数据提示的loading view
   loadingView =
       LayoutInflater.from(getActivity())
           .inflate(com.xuhai.telescopes.R.layout.em_layout_loading_data, null);
   contentContainer.addView(loadingView);
   // 注册上下文菜单
   registerForContextMenu(listView);
 }
  @Override
  protected void setUpView() {
    titleBar.setRightImageResource(com.xuhai.telescopes.R.drawable.em_add);
    titleBar.setRightLayoutClickListener(
        new OnClickListener() {

          @Override
          public void onClick(View v) {
            startActivity(new Intent(getActivity(), NewAndCreateActivity.class));
          }
        });

    // 设置联系人数据
    setContactsMap(MyHelper.getInstance().getContactList());
    super.setUpView();
    listView.setOnItemClickListener(
        new OnItemClickListener() {

          @Override
          public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String username = ((EaseUser) listView.getItemAtPosition(position)).getUsername();
            // demo中直接进入聊天页面,实际一般是进入用户详情页
            startActivity(
                new Intent(getActivity(), ChatActivity.class).putExtra("userId", username));
          }
        });

    // 进入添加好友页
    titleBar
        .getRightLayout()
        .setOnClickListener(
            new OnClickListener() {

              @Override
              public void onClick(View v) {
                startActivity(new Intent(getActivity(), AddContactActivity.class));
              }
            });

    contactSyncListener = new ContactSyncListener();
    MyHelper.getInstance().addSyncContactListener(contactSyncListener);

    blackListSyncListener = new BlackListSyncListener();
    MyHelper.getInstance().addSyncBlackListListener(blackListSyncListener);

    contactInfoSyncListener = new ContactInfoSyncListener();
    MyHelper.getInstance()
        .getUserProfileManager()
        .addSyncContactInfoListener(contactInfoSyncListener);

    if (!MyHelper.getInstance().isContactsSyncedWithServer()) {
      loadingView.setVisibility(View.VISIBLE);
    } else {
      loadingView.setVisibility(View.GONE);
    }
  }
 @Override
 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
   super.onCreateContextMenu(menu, v, menuInfo);
   toBeProcessUser =
       (EaseUser) listView.getItemAtPosition(((AdapterContextMenuInfo) menuInfo).position);
   toBeProcessUsername = toBeProcessUser.getUsername();
   getActivity()
       .getMenuInflater()
       .inflate(com.xuhai.telescopes.R.menu.em_context_contact_list, menu);
 }
 @Override
 public void refresh() {
   super.refresh();
   if (inviteMessgeDao == null) {
     inviteMessgeDao = new InviteMessgeDao(getActivity());
   }
   if (inviteMessgeDao.getUnreadMessagesCount() > 0) {
     applicationItem.showUnreadMsgView();
   } else {
     applicationItem.hideUnreadMsgView();
   }
 }
  @Override
  public void onDestroy() {
    super.onDestroy();
    if (contactSyncListener != null) {
      MyHelper.getInstance().removeSyncContactListener(contactSyncListener);
      contactSyncListener = null;
    }

    if (blackListSyncListener != null) {
      MyHelper.getInstance().removeSyncBlackListListener(blackListSyncListener);
    }

    if (contactInfoSyncListener != null) {
      MyHelper.getInstance()
          .getUserProfileManager()
          .removeSyncContactInfoListener(contactInfoSyncListener);
    }
  }