1. 程式人生 > >Fragment 中 ListView繫結ContextMenu

Fragment 中 ListView繫結ContextMenu

package com.example.administrator.imbobo.controller.fragment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.example.administrator.imbobo.controller.activity.AddContactActivity;
import com.example.administrator.imbobo.model.Model;
import com.example.administrator.imbobo.model.bean.UserInfo;
import com.example.administrator.imbobo.utils.Constant;
import com.example.administrator.imbobo.utils.SpUtils;
import com.hyphenate.chat.EMClient;
import com.hyphenate.easeui.domain.EaseUser;
import com.hyphenate.easeui.ui.EaseContactListFragment;
import com.example.administrator.imbobo.R;
import com.example.administrator.imbobo.controller.activity.InviteActivity;
import com.hyphenate.exceptions.HyphenateException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Created by Leon on 2018/10/6
* Functions: 聯絡人列表頁面 EaseContactListFragment 環信整合的
*/
public class ContactListFragment extends EaseContactListFragment {

private ImageView iv_contact_red;
private LocalBroadcastManager mLBM;
private LinearLayout ll_contact_invite;
private String mHxid;

//接收到廣播的處理
private BroadcastReceiver contactChangeReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//重新整理頁面
refreshContact();
}
};

//接收到廣播的處理
private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {

//更新紅點顯示
iv_contact_red.setVisibility(View.VISIBLE);
SpUtils.getInstance().save(SpUtils.IS_NEW_INVITE,true);
}
};

//接收到廣播的處理
private BroadcastReceiver leReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
//不要紅點顯示剛從InviteActivity(邀請資訊列表)回來不要顯示
iv_contact_red.setVisibility(View.GONE);
SpUtils.getInstance().save(SpUtils.IS_NEW_INVITE,false);
}
};

//initView() 實現EaseContactListFragment中的抽象方法
@Override
protected void initView() {
super.initView();

//佈局顯示右上角+
titleBar.setRightImageResource(R.drawable.em_add);

//頭佈局新增
View headerView = View.inflate(getActivity(),R.layout.header_fragment_contact,null);
listView.addHeaderView(headerView);

//獲取紅點物件(imageview)
iv_contact_red = (ImageView)headerView.findViewById(R.id.iv_contact_red);

//獲取邀請資訊條目的點選事件
ll_contact_invite = headerView.findViewById(R.id.ll_contact_invite);

}

//setUpView() 實現EaseContactListFragment中的抽象方法
@Override
protected void setUpView() {
super.setUpView();

//新增按鈕的點選事件處理
titleBar.setRightLayoutClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(),AddContactActivity.class);
startActivity(intent);
}
});

//初始化紅點的顯示
boolean isNewInvite = SpUtils.getInstance().getBooleanPre(SpUtils.IS_NEW_INVITE,false);
iv_contact_red.setVisibility(isNewInvite?View.VISIBLE:View.GONE);

//邀請資訊條目的點選事件
ll_contact_invite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//紅點處理(隱藏掉)
iv_contact_red.setVisibility(View.GONE);
SpUtils.getInstance().save(SpUtils.IS_NEW_INVITE,false);

//跳轉到邀請資訊處理列表頁面
Intent intent = new Intent(getActivity(),InviteActivity.class);
startActivity(intent);
}
});

//註冊廣播
mLBM = LocalBroadcastManager.getInstance(getActivity());
//ContactInviteChangeReceiver - receiver
mLBM.registerReceiver(receiver,new IntentFilter(Constant.CONTACT_INVITE_CHANGED));
mLBM.registerReceiver(leReceiver,new IntentFilter(Constant.LEONBROADCAST));
mLBM.registerReceiver(contactChangeReceiver,new IntentFilter(Constant.CONTACT_CHANGED));

//從環信伺服器上獲取所有的聯絡人資訊
getContactFromHxServer();

//繫結listView和contextmenu ***********************************************************************************************
registerForContextMenu(listView);
}

//建立contextmenu時會呼叫這個方法**************************************************************************
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
//獲取環信id先獲取postion
int postion = ((AdapterView.AdapterContextMenuInfo)menuInfo).position;
EaseUser easeUser = (EaseUser) listView.getItemAtPosition(postion);
//username 就是 環信id
mHxid = easeUser.getUsername();
//添加布局
getActivity().getMenuInflater().inflate(R.menu.delete,menu);
}

  //當contextmenu(listview上的item被選中)被選中時呼叫這個方法************************************************************
@Override
public boolean onContextItemSelected(MenuItem item) {

//R.id.contact_delete 之前寫了R.menu.delete 產生了bug
if (item.getItemId() == R.id.contact_delete){
//刪除選中的聯絡人操作
deleteContact();

return true;
}

return super.onContextItemSelected(item);
}

//刪除選中的聯絡人操作
private void deleteContact(){

//開闢子執行緒進行網路請求
Model.getInstance().getGloabalThreadPool().execute(new Runnable() {
@Override
public void run() {
try {
//從環信伺服器中刪除聯絡人
EMClient.getInstance().contactManager().deleteContact(mHxid);

//本地資料庫的更新
Model.getInstance().getDbManager().getContactTableDao().deleteContactByHxId(mHxid);

//避免空指標異常
if ( getActivity() == null){ return; }
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
//toast提示
Toast.makeText(getActivity(),"刪除"+mHxid+"成功",Toast.LENGTH_SHORT).show();

//重新整理頁面
refreshContact();
}
});
} catch (final HyphenateException e) {
e.printStackTrace();
//避免空指標異常
if ( getActivity() == null){ return; }
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
//toast提示
Toast.makeText(getActivity(),"刪除失敗: "+e.toString(),Toast.LENGTH_SHORT).show();
}
});

}
}
});

}

//從環信伺服器上獲取所有的聯絡人資訊
private void getContactFromHxServer(){
//開闢子執行緒做聯網操作
Model.getInstance().getGloabalThreadPool().execute(new Runnable() {
@Override
public void run() {
try {
//獲取到所有的好友的環信id
List<String> hxids = EMClient.getInstance().contactManager().getAllContactsFromServer();

//校驗- 注意這裡用的是>= 0將來要注意異常
if (hxids != null && hxids.size() >= 0){

//建立一個UserInfo的集合
List<UserInfo> contacts = new ArrayList<>();

//轉換集合
for (String hxid : hxids){
UserInfo userInfo = new UserInfo(hxid);
contacts.add(userInfo);
}

//儲存好友資訊到本地資料庫
Model.getInstance().getDbManager().getContactTableDao().saveContacts(contacts,
true);

//避免下面空指標異常(重新整理頁面)
if (getActivity() == null){ return; }
//重新整理頁面-回到主執行緒操作
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
//重新整理頁面的方法
refreshContact();
}
});

}

} catch (HyphenateException e) {
e.printStackTrace();
}
}
});
}

//重新整理頁面的方法
private void refreshContact(){

//獲取資料
List<UserInfo> contacts = Model.getInstance().getDbManager().getContactTableDao().getCountacts();

//校驗獲取到的資料 注意這個地方用的是>= 0 可能會有異常
if (contacts != null && contacts.size() >= 0){

// 設定資料 Map<String, EaseUser> contactsMap
Map<String,EaseUser> contactsMap = new HashMap<>();

//資料轉換
for (UserInfo contact : contacts){
EaseUser easeUser = new EaseUser(contact.getHxid());
contactsMap.put(contact.getHxid(),easeUser);
}

//設定聯絡人Map,鍵是hyphenate id。
setContactsMap(contactsMap);

//重新整理頁面
refresh();

}
}

@Override
public void onDestroy() {

//註冊的廣播一定要關閉掉
mLBM.unregisterReceiver(receiver);
mLBM.unregisterReceiver(leReceiver);
mLBM.unregisterReceiver(contactChangeReceiver);
super.onDestroy();
}
}