• GridView的簡單使用


    項目GitHub地址:https://github.com/leonInShanghai/IMbobo   

    GridView XML佈局:

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

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

    <GridView
    android:id="@+id/gv_groupdetail"
    android:numColumns="4"
    android:horizontalSpacing="10dp"
    android:verticalSpacing="10dp"
    android:layout_width="match_parent"
    android:layout_weight="1"
    android:layout_height="0dp">
    </GridView>

    <Button
    android:id="@+id/bt_groupdetail_out"
    android:background="@android:color/holo_red_light"
    android:textColor="@android:color/white"
    android:text="退群"
    android:textSize="20sp"
    android:layout_width="match_parent"
    android:layout_height="60dp" />

    </LinearLayout>


    Activity 類:

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


    import android.app.Activity;
    import android.content.Intent;
    import android.support.v4.content.LocalBroadcastManager;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.GridView;
    import android.widget.Toast;
    import com.example.administrator.imbobo.controller.adapter.GroupDetailAdapte;
    import com.example.administrator.imbobo.R;
    import com.example.administrator.imbobo.model.Model;
    import com.example.administrator.imbobo.model.bean.UserInfo;
    import com.example.administrator.imbobo.utils.Constant;
    import com.hyphenate.chat.EMClient;
    import com.hyphenate.chat.EMGroup;
    import com.hyphenate.exceptions.HyphenateException;

    import java.util.ArrayList;
    import java.util.List;

    /**
    * Created by Leon on 2018/11/24
    * Functions: 群详情页面
    */
    public class GroupDetailActivity extends Activity {

    /**网格视图*/
    private GridView gv_groupdetail;

    /**退群的按钮*/
    private Button bt_groupdetail_out;

    /**群组类(环信easeUI定义的类*/
    private EMGroup mGroup;

    //群成員集合
    private List<UserInfo> mUsers;

    /**GridView 適配器*/
    private GroupDetailAdapte groupDetailAdapte;


    /**處理 增加 和刪除群成員的業務邏輯的接口*/
    private GroupDetailAdapte.OnGroupDetailListener mOnGroupDetailListener = new GroupDetailAdapte.
    OnGroupDetailListener() {
    @Override
    public void onAddMembers() {
    //添加群成員-開闢子綫程做網絡請求

    }

    @Override
    public void onDeleteMember(UserInfo user) {
    //刪除群成員-開闢子綫程做網絡請求
    }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_grour_detail);

    initView();

    /**获取传递过来的环信id*/
    getData();

    initData();
    }

    private void initData(){
    //初始化button显示
    initButtonDisplay();

    //初始化gridview
    initGridView();

    //從環信服務器獲取所有的群成員
    getMembersFromHxServer();
    }

    /**從環信服務器獲取所有的群成員*/
    private void getMembersFromHxServer(){
    //開闢子綫程做網絡請求
    Model.getInstance().getGloabalThreadPool().execute(new Runnable() {
    @Override
    public void run() {
    try {
    //從環信服務器獲取所有的群成員信息
    EMGroup emGroup = EMClient.getInstance().groupManager().getGroupFromServer(mGroup.getGroupId());
    List<String> menbers = emGroup.getMembers();

    if (menbers != null && menbers.size() >= 0){

    mUsers = new ArrayList<>();

    //轉換
    for (String menber : menbers){
    UserInfo userInfo = new UserInfo(menber);
    mUsers.add(userInfo);
    }
    }

    //更新頁面-回到主綫程
    runOnUiThread(new Runnable() {
    @Override
    public void run() {
    //刷新適配器
    groupDetailAdapte.refresh(mUsers);
    }
    });
    } catch (final HyphenateException e) {
    e.printStackTrace();
    //更新頁面-回到主綫程
    runOnUiThread(new Runnable() {
    @Override
    public void run() {
    Toast.makeText(GroupDetailActivity.this,"獲取群信息失敗"+e.toString(),
    Toast.LENGTH_SHORT).show();
    }
    });
    }
    }
    });
    }

    //初始化gridview
    private void initGridView(){

    /**
    * 群主有邀請人入群的權限
    * 另外當 群組是公開的時候群成員也有邀請人的權限
    */
    boolean isCanModify = EMClient.getInstance().getCurrentUser().equals(mGroup.getOwner()) ||
    mGroup.isPublic();

    /**
    * 第一個參數是上下文
    * 第二個參數是標記 是 群主/群員
    */
    groupDetailAdapte = new GroupDetailAdapte(this,isCanModify,
    mOnGroupDetailListener);

    //GridView 設置 adapte
    gv_groupdetail.setAdapter(groupDetailAdapte);
    }

    //初始化button显示 - 退群/解散群
    private void initButtonDisplay(){

    //判断当前用户是否为群组
    if (EMClient.getInstance().getCurrentUser().equals(mGroup.getOwner())){//群主
    bt_groupdetail_out.setText("解散群");

    bt_groupdetail_out.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

    //开辟子线程-去环信服务器解散群
    Model.getInstance().getGloabalThreadPool().execute(new Runnable() {
    @Override
    public void run() {
    try {
    //去环信服务器解散群
    EMClient.getInstance().groupManager().destroyGroup(mGroup.getGroupId());

    //发送一个退群的广播
    exitGroupBroatCast();

    //更新页面
    runOnUiThread(new Runnable() {
    @Override
    public void run() {
    Toast.makeText(GroupDetailActivity.this,"解散群成功",
    Toast.LENGTH_SHORT).show();

    //结束当前页面
    finish();
    }
    });

    } catch (final HyphenateException e) {
    e.printStackTrace();
    //更新页面
    runOnUiThread(new Runnable() {
    @Override
    public void run() {
    Toast.makeText(GroupDetailActivity.this,"解散群失败"+e.toString(),
    Toast.LENGTH_SHORT).show();
    }
    });
    }
    }
    });
    }
    });

    }else {//群成员
    bt_groupdetail_out.setText("退群");

    bt_groupdetail_out.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    //开辟子线程做服务器请求操作
    Model.getInstance().getGloabalThreadPool().execute(new Runnable() {
    @Override
    public void run() {
    try {
    //告诉环信服务器群成员要退群
    EMClient.getInstance().groupManager().leaveGroup(mGroup.getGroupId());

    //发送一个退群的广播
    exitGroupBroatCast();

    //更新页面
    runOnUiThread(new Runnable() {
    @Override
    public void run() {
    Toast.makeText(GroupDetailActivity.this,"退群成功",
    Toast.LENGTH_SHORT).show();

    //结束当前页面
    finish();
    }
    });
    } catch (final HyphenateException e) {
    e.printStackTrace();
    //更新页面
    runOnUiThread(new Runnable() {
    @Override
    public void run() {
    Toast.makeText(GroupDetailActivity.this,"退群失败"+e.toString(),
    Toast.LENGTH_SHORT).show();
    }
    });
    }
    }
    });
    }
    });
    }
    }

    /**发送退群和解散群广播*/
    private void exitGroupBroatCast(){
    LocalBroadcastManager mLBM = LocalBroadcastManager.getInstance(GroupDetailActivity.this);

    Intent intent = new Intent(Constant.EXIT_GROUP);

    intent.putExtra(Constant.GROUP_ID,mGroup.getGroupId());

    mLBM.sendBroadcast(intent);
    }

    /**获取传递过来的数据最终得到一个 mGroup*/
    private void getData(){
    String groupId = getIntent().getStringExtra(Constant.GROUP_ID);

    if (groupId == null){
    return;
    }else {
    mGroup = EMClient.getInstance().groupManager().getGroup(groupId);
    }
    }

    private void initView(){
    gv_groupdetail = (GridView)findViewById(R.id.gv_groupdetail);
    bt_groupdetail_out = (Button)findViewById(R.id.bt_groupdetail_out);
    }
    }



    適配器:

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

    import android.content.Context;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.ImageView;
    import android.widget.TextView;

    import com.example.administrator.imbobo.R;
    import com.example.administrator.imbobo.model.bean.UserInfo;

    import java.util.ArrayList;
    import java.util.List;

    /**
    * Created by Leon on 2018/11/25.
    * Functions: GroupDetailActivity 中的 GridView 的適配器
    */
    public class GroupDetailAdapte extends BaseAdapter {

    private Context mContext;

    /*是否允許添加和刪除群成員*/
    private boolean mIsCanModify;

    /**GridView 的數據源*/
    private List<UserInfo> mUsers = new ArrayList<>();

    /**刪除模式 true:表示可以刪除 false:表示不可以刪除*/
    private boolean mIsDeleteModle;

    /**自定義接口 實現添加群成員和 刪除群成員的業務邏輯*/
    private OnGroupDetailListener mOnGroupDetailListener;

    public GroupDetailAdapte(Context context,boolean isCanModify,OnGroupDetailListener onGroupDetailListener) {
    this.mContext = context;
    this.mIsCanModify = isCanModify;
    this.mOnGroupDetailListener = onGroupDetailListener;
    }

    //刷新方法-用於刷新和傳參
    public void refresh(List<UserInfo> users){
    if (users != null && users.size() >= 0){
    //1.清空原有的數據
    mUsers.clear();

    //2.添加加號和減號
    initUsers();

    //3.添加數據 - index:0 是爲了把加號 減號 都擠到後面
    mUsers.addAll(0,users);
    }

    //刷新頁面
    notifyDataSetChanged();
    }

    //添加加號和減號
    private void initUsers(){
    UserInfo add = new UserInfo("add");
    UserInfo delete = new UserInfo("delete");

    //添加 加號 和 減號讓它們在數組集合的後邊
    mUsers.add(delete);
    mUsers.add(0,add);
    }

    @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(int position, View convertView, ViewGroup parent) {

    //1.獲取或創建ViewHolder
    ViewHolder holder = null;

    if (convertView == null){
    holder = new ViewHolder();

    convertView = View.inflate(mContext, R.layout.item_groupdetail,null);

    holder.photo = (ImageView) convertView.findViewById(R.id.iv_group_detail_photo);
    holder.delete = (ImageView)convertView.findViewById(R.id.iv_group_detatil_delete);
    holder.name = (TextView)convertView.findViewById(R.id.tv_group_detail_name);

    convertView.setTag(holder);
    }else{
    holder = (ViewHolder) convertView.getTag();
    }

    //2.獲取當前item數據
    final UserInfo userInfo = mUsers.get(position);

    //3.顯示數據
    if (mIsCanModify){//群主 或 公開群權限裏的群成員
    //佈局的處理
    if (position == getCount() - 1){//減號的位置特殊處理
    //刪除模式判斷
    if (mIsDeleteModle){
    convertView.setVisibility(View.GONE);
    }else {
    convertView.setVisibility(View.VISIBLE);

    holder.photo.setImageResource(R.drawable.em_smiley_minus_btn);
    holder.delete.setVisibility(View.GONE);
    holder.name.setVisibility(View.INVISIBLE);
    }
    }else if (position == getCount() -2){//加號的位置特殊處理
    //刪除模式判斷(增刪模式)
    if (mIsDeleteModle){
    convertView.setVisibility(View.GONE);
    }else {
    convertView.setVisibility(View.VISIBLE);

    holder.photo.setImageResource(R.drawable.em_smiley_add_btn);
    holder.delete.setVisibility(View.GONE);
    holder.name.setVisibility(View.INVISIBLE);
    }
    }else {//群成員
    convertView.setVisibility(View.VISIBLE);
    holder.name.setVisibility(View.VISIBLE);

    //名稱 由於我這裏昵稱是手機號太長,所以只顯示后四位
    if (userInfo.getName() != null){
    String name = userInfo.getName().substring(userInfo.getName().length()-4,userInfo.getName().length());
    holder.name.setText(name);
    }
    holder.photo.setImageResource(R.drawable.em_default_avatar);

    if (mIsDeleteModle){//刪除(增刪)模式
    holder.delete.setVisibility(View.VISIBLE);
    }else {//非 刪除(增刪)模式
    holder.delete.setVisibility(View.GONE);
    }
    }

    //點擊事件的處理
    if (position == getCount() - 1){//減號的位置
    holder.photo.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    if (!mIsDeleteModle){
    mIsDeleteModle = true;
    notifyDataSetChanged();
    }
    }
    });
    }else if (position == getCount() - 2){//加號的位置
    holder.photo.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    mOnGroupDetailListener.onAddMembers();
    }
    });

    }else {//群成員的位置 delete 點擊事件的處理
    holder.delete.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    mOnGroupDetailListener.onDeleteMember(userInfo);
    }
    });
    }
    }else{//普通的群成員
    if (position == getCount() - 1 || position == getCount() - 2){//減號和加號的位置隱藏掉
    convertView.setVisibility(View.GONE);
    }else{
    convertView.setVisibility(View.VISIBLE);

    //名稱 由於我這裏昵稱是手機號太長,所以只顯示后四位
    if (userInfo.getName() != null){
    String name = userInfo.getName().substring(userInfo.getName().length()-4,userInfo.getName().length());
    holder.name.setText(name);
    }

    //頭像
    holder.photo.setImageResource(R.drawable.em_default_avatar);

    //刪除按鈕 - 普通成員沒有權力刪除和邀請群成員
    holder.delete.setVisibility(View.GONE);
    }
    }

    //4.返回View
    return convertView;
    }

    /**内部類 ViewHolder*/
    private class ViewHolder{
    private ImageView photo;
    private ImageView delete;
    private TextView name;
    }

    /**自定義接口 誰實現誰處理 刪除群成員 和添加群成員的業務邏輯*/
    public interface OnGroupDetailListener{
    /**添加群成員方法*/
    void onAddMembers();

    /**刪除群成員方法*/
    void onDeleteMember(UserInfo user);
    }
    }
  • 相关阅读:
    开发安全测试工具 ,推荐6款完全免费的工具
    Asp.net MVC 出现IQueryable<T> 未包含“Include”的定义
    Windows系统Git安装教程(详解Git安装过程)
    窗体进行自动适应窗口
    C# ObservableCollection两个字段排序的情况
    Advanced Installer 14.9 – WPF或winform应用程序打包成exe文件
    DotNetCore部署(IIS)踩坑记
    DotNetCore部署(IIS)文档
    windows系统中Dotnet core runtime 安装后,无法启动次程序,因为计算机中丢失api-ms-win-crt-runtime-l1-1-0.dll的解决方法
    解决Git在添加ignore文件之前就提交了项目无法再过滤问题
  • 原文地址:https://www.cnblogs.com/henanleon/p/10016201.html
Copyright © 2020-2023  润新知