你的位置:首页 > 操作系统

[操作系统]硅谷社交15


1)页面布局

<?

群主群详情.png
删除模式群详情.png
群成员群详情.png


2)获取传递过来的数据

// 获取传递过来的参数private void getData() {	// 获取传递过来的群id	String groupId = getIntent().getStringExtra(Constant.GROUP_ID);	// 校验	if (groupId == null) {		finish();		return;	} else {		// 获取群信息		mGroup = EMClient.getInstance().groupManager().getGroup(groupId);	}}


3)初始化button按钮的显示

// 初始化按钮的显示private void initButtonDisplay() {	//显示	// 判断是否是群主	if (EMClient.getInstance().getCurrentUser().equals(mGroup.getOwner())) {		// 更新显示		btn_exit_group.setText("解散群");		// 解散群的点击事件		btn_exit_group.setOnClickListener(new View.OnClickListener() {			@Override			public void onClick(View v) {				Model.getInstace().getGolbalThreadPool().execute(new Runnable() {					@Override					public void run() {						// 联网						try {							// 联网成功							EMClient.getInstance().groupManager().destroyGroup(mGroup.getGroupId());							// 发送解散群广播							exitGroupBroadCast();							// 提示							runOnUiThread(new Runnable() {								@Override								public void run() {									Toast.makeText(GroupDetailActivity.this, "解散群成功", Toast.LENGTH_SHORT).show();									// 销毁当前页面									finish();								}							});						} catch (HyphenateException e) {							e.printStackTrace();							// 提示							runOnUiThread(new Runnable() {								@Override								public void run() {									Toast.makeText(GroupDetailActivity.this, "解散群失败", Toast.LENGTH_SHORT).show();								}							});						}					}				});			}		});	} else {// 群成员		btn_exit_group.setText("退群");		// 退群按钮点击事件		btn_exit_group.setOnClickListener(new View.OnClickListener() {			@Override			public void onClick(View v) {				// 联网				Model.getInstace().getGolbalThreadPool().execute(new Runnable() {					@Override					public void run() {						try {							EMClient.getInstance().groupManager().leaveGroup(mGroup.getGroupId());							// 发送广播							exitGroupBroadCast();							// 提示							runOnUiThread(new Runnable() {								@Override								public void run() {									Toast.makeText(GroupDetailActivity.this, "退群成功", Toast.LENGTH_SHORT).show();									// 销毁当前页面									finish();								}							});						} catch (HyphenateException e) {							e.printStackTrace();							runOnUiThread(new Runnable() {								@Override								public void run() {									Toast.makeText(GroupDetailActivity.this, "退群失败", Toast.LENGTH_SHORT).show();								}							});						}					}				});			}		});	}}

  

4)退群和解散群
发送退群广播

// 解散群和退群广播private void exitGroupBroadCast() {	// 发送广播	Intent intent = new Intent(Constant.EXIT_GROUP);	// 传递群id参数	intent.putExtra(Constant.GROUP_ID, mGroup.getGroupId());	// 发送	mLBM.sendBroadcast(intent);}


5)初始化Gridview

// 初始化gridviewprivate void initGridview() {	// 你是群主 或者 你这个群是公开 你就可以添加和删除群成员	boolean mIsCanModify = EMClient.getInstance().getCurrentUser().equals(mGroup.getOwner()) || mGroup.isPublic();	// 创建适配器	mGroupDetailAdapter = new GroupDetailAdapter(GroupDetailActivity.this, mIsCanModify, mOnGroupDetailListener);	// 添加到gridview	gv_member_list.setAdapter(mGroupDetailAdapter);}


6)GridView适配器

public class GroupDetailAdapter extends BaseAdapter {  private Context mContext;  private boolean mIsCanModify;    // 表示可以添加和删除好友  private boolean mIsDeleteModel;   // 是否是删除模式 true:删除模式 false:非删除模式  private List<UserInfo> mUsers = new ArrayList<>();  private OnGroupDetailListener mOnGroupDetailListener;  private final String mCurrentUser;  public GroupDetailAdapter(Context context, boolean isCanModify, OnGroupDetailListener onGroupDetailListener) {    mContext = context;    mIsCanModify = isCanModify;    mOnGroupDetailListener = onGroupDetailListener;    mCurrentUser = EMClient.getInstance().getCurrentUser();  }  private void initUsers() {    UserInfo add = new UserInfo("add");    UserInfo delete = new UserInfo("delete");    mUsers.add(delete);    mUsers.add(0, add);  }  // 刷新的方法  public void refresh(List<UserInfo> users) {    // 校验    if (users != null && users.size() >= 0) {      mUsers.clear();      initUsers();      mUsers.addAll(0, users);    }    // 刷新页面    notifyDataSetChanged();  }  // 获取当前的模式  public boolean ismIsDeleteModel() {    return mIsDeleteModel;  }  // 设置当前的模式  public void setmIsDeleteModel(boolean mIsDeleteModel) {    this.mIsDeleteModel = mIsDeleteModel;  }  @Override  public int getCount() {    return mUsers == null ? 0 : mUsers.size();  }  @Override  public Object getItem(int position) {    return mUsers.get(position);  }  @Override  public long getItemId(int position) {    return position;  }  @Override  public View getView(final int position, View convertView, ViewGroup parent) {    // 创建或获取viewHolder    ViewHolder holder = null;    if (convertView == null) {      holder = new ViewHolder();      convertView = View.inflate(mContext, R.layout.item_group_detail, null);      holder.photo = (ImageView) convertView.findViewById(R.id.iv_member_photo);      holder.name = (TextView) convertView.findViewById(R.id.tv_member_name);      holder.delete = (ImageView) convertView.findViewById(R.id.iv_member_delete);      convertView.setTag(holder);    } else {      holder = (ViewHolder) convertView.getTag();      if (position != getCount() - 1 && position != getCount() - 2) {        if (holder.name.getVisibility() == View.INVISIBLE) {          holder = new ViewHolder();          convertView = View.inflate(mContext, R.layout.item_group_detail, null);          holder.photo = (ImageView) convertView.findViewById(R.id.iv_member_photo);          holder.name = (TextView) convertView.findViewById(R.id.tv_member_name);          holder.delete = (ImageView) convertView.findViewById(R.id.iv_member_delete);          convertView.setTag(holder);        }      }    }    // 显示数据    // 判断是否有修改的权限    if (mIsCanModify) {// 修改模式      // 1 监听事件      if (position == getCount() - 1) {// 减号位置        holder.photo.setOnClickListener(new View.OnClickListener() {          @Override          public void onClick(View v) {            // 如果当前是非删除模式            if (!mIsDeleteModel) {              // 修改为删除模式              mIsDeleteModel = true;              // 更新显示              notifyDataSetChanged();            }          }        });      } else if (position == getCount() - 2) {// 加号位置        holder.photo.setOnClickListener(new View.OnClickListener() {          @Override          public void onClick(View v) {            // 逻辑复杂 不再这操作            mOnGroupDetailListener.onAddMembers();          }        });      } else {// 群成员的点击事件        holder.delete.setOnClickListener(new View.OnClickListener() {          @Override          public void onClick(View v) {            // 不再这操作            mOnGroupDetailListener.onDeleteMember(mUsers.get(position));          }        });      }      // 2 显示布局      if (position == getCount() - 1) {// 减号        if (mIsDeleteModel) {          convertView.setVisibility(View.INVISIBLE);        } else {          convertView.setVisibility(View.VISIBLE);          // 设置数据          holder.photo.setImageResource(R.drawable.em_smiley_minus_btn_pressed);          holder.delete.setVisibility(View.GONE);          holder.name.setVisibility(View.INVISIBLE);        }      } else if (position == getCount() - 2) {// 加号        if (mIsDeleteModel) {          convertView.setVisibility(View.INVISIBLE);        } else {          convertView.setVisibility(View.VISIBLE);          holder.photo.setImageResource(R.drawable.em_smiley_add_btn_pressed);          holder.delete.setVisibility(View.GONE);          holder.name.setVisibility(View.INVISIBLE);        }      } else {        // 获取当前item数据        UserInfo user = mUsers.get(position);        convertView.setVisibility(View.VISIBLE);        holder.name.setVisibility(View.VISIBLE);        holder.photo.setVisibility(View.VISIBLE);        holder.name.setText(user.getName());        holder.photo.setImageResource(R.drawable.em_default_avatar);        if (mIsDeleteModel && !mCurrentUser.equals(user.getHxId())) {          // 布局设置          holder.delete.setVisibility(View.VISIBLE);        } else {          holder.delete.setVisibility(View.GONE);        }      }    } else {      // 展示数据      if (position == getCount() - 1 || position == getCount() - 2) {        convertView.setVisibility(View.INVISIBLE);      } else {        convertView.setVisibility(View.VISIBLE);        // 获取当前item数据        UserInfo userInfo = mUsers.get(position);        holder.name.setText(userInfo.getName());        holder.photo.setImageResource(R.drawable.em_default_avatar);        holder.delete.setVisibility(View.GONE);      }    }    // 返回view    return convertView;  }  static class ViewHolder {    TextView name;    ImageView photo;    ImageView delete;  }  public interface OnGroupDetailListener {    // 添加成员的方法    void onAddMembers();    // 删除群成员方法    void onDeleteMember(UserInfo user);  }}


7)从环信服务器获取所有群成员

private void getMembersFromHxServer() {    Modle.getInstance().getGlobalThreadPool().execute(new Runnable() {      @Override      public void run() {        try {          EMGroup groupFromServer = EMClient.getInstance().groupManager().getGroupFromServer(mGroup.getGroupId());          List<String> members = groupFromServer.getMembers();                   if(members != null && members.size() >=0) {            mUserss = new ArrayList<UserInfo>();            for (String member:members){              UserInfo userInfo = new UserInfo(member);              mUserss.add(userInfo);            }          }          runOnUiThread(new Runnable() {            @Override            public void run() {              mGroupDetailAdapter.refresh(mUserss);//              Toast.makeText(GroupDetailAcitivity.this, "加载成功", Toast.LENGTH_SHORT).show();            }          });        } catch (HyphenateException e) {          e.printStackTrace();        }      }    });  }


8)GridView适配器回调接口
(1)删除群成员

 Model.getInstace().getGolbalThreadPool().execute(new Runnable() {	@Override	public void run() {		try {			// 从环信服务器中删除该成员			EMClient.getInstance().groupManager().removeUserFromGroup(group.getGroupId(),user.getHxId() );			// 从环信服务器再次获取群成员,并刷新页面			getUsersFromHxServer();			runOnUiThread(new Runnable() {				@Override				public void run() {					Toast.makeText(GroupDetailActivity.this, "删除该成员成功", Toast.LENGTH_SHORT).show();				}			});		} catch (HyphenateException e) {			e.printStackTrace();			runOnUiThread(new Runnable() {				@Override				public void run() {					Toast.makeText(GroupDetailActivity.this, "删除该成员失败", Toast.LENGTH_SHORT).show();				}			});		}	}});

  

(2)添加群成员

public void onAddMembers() {	// 跳转到选择联系人页面	Intent intent = new Intent(GroupDetailActivity.this, PickContactsActivity.class);	intent = intent.putExtra(Constant.GROUP_ID, group.getGroupId());	startActivityForResult(intent, 0);}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {	super.onActivityResult(requestCode, resultCode, data);		if(resultCode == RESULT_OK) {		// 获取返回的联系人信息		final String[] memberses = data.getStringArrayExtra("members");		Model.getInstace().getGolbalThreadPool().execute(new Runnable() {			@Override			public void run() {				try {					// 添加群成员到该群组					EMClient.getInstance().groupManager().addUsersToGroup(group.getGroupId(), memberses);					// 访问服务器刷新页面//            getUsersFromHxServer();					runOnUiThread(new Runnable() {						@Override						public void run() {							Toast.makeText(GroupDetailActivity.this, "发送加入该群邀请成功", Toast.LENGTH_SHORT).show();						}					});				} catch (HyphenateException e) {					e.printStackTrace();					runOnUiThread(new Runnable() {						@Override						public void run() {							Toast.makeText(GroupDetailActivity.this, "发送加入该群邀请失败", Toast.LENGTH_SHORT).show();						}					});				}			}		});	}}