Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> 硅谷社交15--群詳情,硅谷社交15--

硅谷社交15--群詳情,硅谷社交15--

編輯:關於android開發

硅谷社交15--群詳情,硅谷社交15--


1)頁面布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">

    <com.hyphenate.easeui.widget.EaseTitleBar
        app:titleBarTitle="群詳情"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </com.hyphenate.easeui.widget.EaseTitleBar>
    
    <GridView
        android:id="@+id/gv_group_detail"
        android:layout_width="match_parent"
        android:numColumns="4"
        android:horizontalSpacing="10dp"
        android:verticalSpacing="10dp"
        android:layout_weight="1"
        android:layout_height="0dp">

    </GridView>

    <Button
        android:id="@+id/bt_group_detail"
        android:text="退群"
        android:textSize="25sp"
        android:layout_margin="5dp"
        android:textColor="@android:color/white"
        android:background="@android:color/holo_red_light"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

群主群詳情.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

// 初始化gridview
private 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);
}


@Override
protected 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();
						}
					});
				}
			}
		});
	}
}

  

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved