美文网首页
IM社交App04

IM社交App04

作者: GeekGray | 来源:发表于2018-10-03 19:48 被阅读1次

阅读原文

3.11 功能十一:新建群页面

页面布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.hyphenate.easeui.widget.EaseTitleBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:titleBarTitle="新建群组"/>
<EditText
android:id="@+id/et_group_name"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:singleLine="true"
android:hint="群聊名称"/>
<EditText
android:id="@+id/et_group_desc"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_width="match_parent"
android:singleLine="false"
android:layout_height="150dp"
android:textSize="20sp"
android:hint="群聊简介"
android:gravity="top"/>
<LinearLayout
android:layout_marginRight="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="是否公开"
android:layout_gravity="center"
android:textSize="20sp"/>
<CheckBox
android:id="@+id/cb_is_public_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_marginTop="30dp"
android:layout_marginRight="10dp"
android:layout_width="match_parent
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="是否开发群邀请"
android:layout_gravity="center"
android:textSize="20sp"/>
<CheckBox
android:id="@+id/cb_open_invitation"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<Button
android:id="@+id/btn_group_create"
android:layout_marginTop="30dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_green_light"
android:textColor="@android:color/white"
android:text="创建"
android:textSize="25dp"/>
</LinearLayout>

创建群按钮的监听

/**
 * 新建群页面
 */
public class NewGroupActivity extends Activity
{
    private EditText et_newgroup_name;//群名称
    private EditText et_newgroup_desc;//群简介
    private CheckBox cb_newgroup_public;//是否公开
    private CheckBox cb_newgroup_invite;//是否开放群邀请
    private Button bt_newgroup_create;//创建


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_group);

        initView();

        initListener();
    }

    /**
     * 带参数启动 回调
     * 监听
     */
    private void initListener()
    {
        bt_newgroup_create.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Intent intent = new Intent(NewGroupActivity.this, PickContactActivity.class);
                startActivityForResult(intent, 1);
            }
        });

    }

    /**
     * 接收参数
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);

        // 成功获取到联系人
        if(resultCode==RESULT_OK)
        {
            // 创建群,key可以随意,与选择联系人中的key保持一致即可
            createGroup(data.getStringArrayExtra("members"));
        }

    }

    /**
     * 创建群
     */
    private void createGroup(final String[] memberses)
    {
        // 群名称
       final String groupName = et_newgroup_name.getText().toString();
        // 群描述
       final String groupDesc= et_newgroup_desc.getText().toString();

        //联网操作
        Model.getInstance().getGlobalThreadPool().execute(new Runnable()
        {
            @Override
            public void run()
            {
                // 去环信服务器创建群
                // 参数一:群名称;参数二:群描述;参数三:群成员;参数四:原因;参数五:参数设置
                EMGroupManager.EMGroupOptions options=new EMGroupManager.EMGroupOptions();

                options.maxUsers=200;//群最多容纳多少人
                EMGroupManager.EMGroupStyle groupStyle=null;

                if(cb_newgroup_public.isChecked())//公开
                {
                    if(cb_newgroup_invite.isChecked())// 开放群邀请
                    {
                        groupStyle=EMGroupManager.EMGroupStyle.EMGroupStylePublicOpenJoin;
                    }
                    else
                    {
                        groupStyle = EMGroupManager.EMGroupStyle.EMGroupStylePublicJoinNeedApproval;
                    }
                }
                else
                {
                    if(cb_newgroup_invite.isChecked())// 开放群邀请
                    {
                        groupStyle = EMGroupManager.EMGroupStyle.EMGroupStylePrivateMemberCanInvite;
                    }
                    else
                    {
                        groupStyle = EMGroupManager.EMGroupStyle.EMGroupStylePrivateOnlyOwnerInvite;
                    }
                }

                options.style=groupStyle;// 创建群的类型
                try
                {
                    //创建群
                    EMClient.getInstance().groupManager().createGroup(groupName,groupDesc,memberses,"申请加入群",options);
                    //创建成功更新UI
                    runOnUiThread(new Runnable()
                    {
                        @Override
                        public void run()
                        {
                            Toast.makeText(NewGroupActivity.this, "创建群成功", Toast.LENGTH_SHORT).show();
                            // 结束当前页面
                            finish();
                        }
                    });
                }
                catch (HyphenateException e)
                {
                    runOnUiThread(new Runnable()
                    {
                        @Override
                        public void run()
                        {
                            Toast.makeText(NewGroupActivity.this, "创建群失败", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }
        });
    }

    /**
     * 初始化View
     */
    private void initView()
    {
        et_newgroup_name = (EditText)findViewById(R.id.et_newgroup_name);
        et_newgroup_desc = (EditText)findViewById(R.id.et_newgroup_desc);
        cb_newgroup_public = (CheckBox)findViewById(R.id.cb_newgroup_public);
        cb_newgroup_invite = (CheckBox)findViewById(R.id.cb_newgroup_invite);
        bt_newgroup_create = (Button)findViewById(R.id.bt_newgroup_create);
    }
}

3.12 功能十二:群联系人选择页面

页面布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#eee"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="@dimen/height_top_bar"
android:background="@color/top_bar_normal_bg"
android:gravity="center_vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/Select_the_contact"
android:textColor="#ffffff"
android:textSize="20sp" />
<Button
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="@drawable/ease_common_tab_bg"
android:onClick="save"
android:text="@string/button_save"
android:textColor="#fff"
android:textSize="18sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="#00000000"
android:background="@android:color/transparent"
android:divider="#D4D4D4"
android:descendantFocusability="afterDescendants"
android:dividerHeight="1px"
android:fastScrollEnabled="true" />
<com.hyphenate.easeui.widget.EaseSidebar
android:id="@+id/sidebar"
android:layout_width="25dp"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:background="@android:color/transparent"
android:clickable="true" />
<TextView
android:id="@+id/floating_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@android:color/holo_green_dark"
android:gravity="center"
android:paddingLeft="25dp"
android:paddingRight="25dp"
android:textColor="@android:color/white"
android:textSize="40sp"
android:visibility="invisible" />
</RelativeLayout>
</LinearLayout>

选择联系人页面

/**
 * 新建群选择联系人页面
 */
public class PickContactActivity extends Activity
{

    private TextView tv_pick_save;
    private ListView lv_pick;
    private List<PickContactInfo>mPicks;
    private PickContactAdapter pickContactAdapter;
    private List<String>mExistMembers;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pick_contact);

        // 获取传递过来的数据
        getData();

        initView();

        initData();

        initListener();
    }

    /**
     * 获取传递过来的数据
     */
    private void getData()
    {
        String groupId = getIntent().getStringExtra(Constant.GROUP_ID);

        if (groupId != null)
        {
            EMGroup group = EMClient.getInstance().groupManager().getGroup(groupId);
            // 获取群众已经存在的所有群成员
            mExistMembers = group.getMembers();
        }

        if (mExistMembers == null)
        {
            mExistMembers = new ArrayList<>();
        }
    }

    /**
     * 点击监听
     */
    private void initListener()
    {
        // listview条目点击事件
        lv_pick.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                // checkbox的切换
                CheckBox cb_pick=(CheckBox)view.findViewById(R.id.cb_pick);
                cb_pick.setChecked(!cb_pick.isChecked());

                // 修改数据
                PickContactInfo pickContactInfo=mPicks.get(position);
                pickContactInfo.setIsChecked(cb_pick.isChecked());

                // 刷新页面
                pickContactAdapter.notifyDataSetChanged();
            }
        });

        // 保存按钮的点击事件
        tv_pick_save.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                // 获取到已经选择的联系人
                List<String>names=pickContactAdapter.getPickContacts();

                // 给启动页面返回数据
                Intent intent=new Intent();
                intent.putExtra("members",names.toArray(new String[0]));//集合转String数组

                // 设置返回的结果码
                setResult(RESULT_OK,intent);

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

    /**
     * 数据处理
     */
    private void initData()
    {
        // 从本地数据库中获取所有的联系人信息
        List<UserInfo>contacts= Model.getInstance().getDbManager().getContactTableDao().getContacts();

        mPicks=new ArrayList<>();

        if(contacts!=null && contacts.size()>=0)
        {
            // 数据转换
            for(UserInfo contact:contacts)
            {
                PickContactInfo pickContactInfo=new PickContactInfo(contact,false);
                mPicks.add(pickContactInfo);
            }
        }
        // 初始化listview
        pickContactAdapter = new PickContactAdapter(this, mPicks, mExistMembers);

        lv_pick.setAdapter(pickContactAdapter);
    }

    /**
     * 初始化View
     */
    private void initView()
    {
        tv_pick_save = (TextView)findViewById(R.id.tv_pick_save);
        lv_pick = (ListView)findViewById(R.id.lv_pick);
    }
}

3.13 功能十三:群详情

页面布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.hyphenate.easeui.widget.EaseTitleBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:titleBarTitle="群详情"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.hyphenate.easeui.widget.EaseExpandGridView
android:id="@+id/gv_member_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="13dp"
android:gravity="start"
android:horizontalSpacing="6dp"
android:listSelector="@android:color/transparent"
android:numColumns="4"
android:verticalSpacing="10dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="群 ID"
android:textSize="25sp"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
<Button
android:id="@+id/btn_exit_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="退群"
android:textSize="25sp"
android:textColor="@android:color/white"
android:background="@android:color/holo_red_dark"/>
</LinearLayout>

群详情页面

/**
 * 群详情页面
 */
public class GroupDetailActivity extends Activity
{
    private GridView gv_groupdetail;
    private Button bt_groupdetail_out;
    private EMGroup mGroup;
    private GroupDetailAdapter groupDetailAdapter;
    private List<UserInfo> mUsers;
    private GroupDetailAdapter.OnGroupDetailListener mOnGroupDetailListener= new GroupDetailAdapter.OnGroupDetailListener()
    {
        /**
         * 添加群成员
         */
        @Override
        public void onAddMembers()
        {
            // 跳转到选择联系人页面
            Intent intent=new Intent(GroupDetailActivity.this,PickContactActivity.class);

            // 传递群id;
            intent.putExtra(Constant.GROUP_ID,mGroup.getGroupId());
            startActivityForResult(intent,2);
        }

        /**
         *  删除群成员方法
         * @param user
         */
        @Override
        public void onDeleteMember(final UserInfo user)
        {
            Model.getInstance().getGlobalThreadPool().execute(new Runnable()
            {
                @Override
                public void run()
                {
                    try
                    {
                        // 从环信服务器中删除此人
                        EMClient.getInstance().groupManager().removeUserFromGroup(mGroup.getGroupId(),user.getHxid());

                        // 更新页面
                        getMembersFromHxServer();

                        runOnUiThread(new Runnable()
                        {
                            @Override
                            public void run()
                            {
                                Toast.makeText(GroupDetailActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                    catch (final HyphenateException e)
                    {
                        runOnUiThread(new Runnable()
                        {
                            @Override
                            public void run()
                            {
                                Toast.makeText(GroupDetailActivity.this, "删除失败" + e.toString(), Toast.LENGTH_SHORT).show();
                            }
                        });

                    }
                }
            });
        }
    };

    /**
     * 接收参数
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @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.getInstance().getGlobalThreadPool().execute(new Runnable()
            {
                @Override
                public void run()
                {
                    try
                    {
                        // 去环信服务器,发送邀请信息
                        EMClient.getInstance().groupManager().addUsersToGroup(mGroup.getGroupId(),memberses);
                        runOnUiThread(new Runnable()
                        {
                            @Override
                            public void run()
                            {
                                Toast.makeText(GroupDetailActivity.this, "发送邀请成功", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                    catch (final HyphenateException e)
                    {
                        runOnUiThread(new Runnable()
                        {
                            @Override
                            public void run()
                            {
                                Toast.makeText(GroupDetailActivity.this, "发送邀请失败" + e.toString(), Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                }
            });
        }
    }

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

        initView();

        getData();

        initData();

        initListener();

    }

    /**
     * 监听
     */
    private void initListener()
    {
        gv_groupdetail.setOnTouchListener(new View.OnTouchListener()
        {
            @Override
            public boolean onTouch(View v, MotionEvent event)
            {
                switch (event.getAction())
                {
                    case MotionEvent.ACTION_DOWN:
                        // 判断当前是否是删除模式,如果是删除模式
                        if(groupDetailAdapter.ismIsDeleteModel())
                        {
                            // 切换为非删除模式
                            groupDetailAdapter.setmIsDeleteModel(false);
                            // 刷新页面
                            groupDetailAdapter.notifyDataSetChanged();
                        }
                        break;
                }
                return false;
            }
        });
    }

    /**
     * 数据处理
     */
    private void initData()
    {
        // 初始化button显示
        initButtonDisplay();
        // 初始化gridview
        initGridview();
        // 从环信服务器获取所有的群成员
        getMembersFromHxServer();
    }

    /**
     * 从环信服务器获取所有的群成员
     */
    private void getMembersFromHxServer()
    {
        Model.getInstance().getGlobalThreadPool().execute(new Runnable()
        {
            @Override
            public void run()
            {
                try
                {
                    // 从环信服务器获取所有的群成员信息
                    EMGroup emGroup=EMClient.getInstance().groupManager().getGroupFromServer(mGroup.getGroupId());
                    List<String>members=emGroup.getMembers();

                    if(members!=null && members.size()>=0)
                    {
                        mUsers=new ArrayList<UserInfo>();

                        // 数据转换
                        for(String member:members)
                        {
                            UserInfo userInfo=new UserInfo(member);
                            mUsers.add(userInfo);
                        }
                    }

                    // 更新页面
                    runOnUiThread(new Runnable()
                    {
                        @Override
                        public void run()
                        {
                            // 刷新适配器
                            groupDetailAdapter.refresh(mUsers);
                        }
                    });
                }
                catch (final HyphenateException e)
                {
                    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();

        groupDetailAdapter=new GroupDetailAdapter(this,isCanModify,mOnGroupDetailListener);

        gv_groupdetail.setAdapter(groupDetailAdapter);


    }

    /**
     * 初始化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().getGlobalThreadPool().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().getGlobalThreadPool().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)
                            {
                                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);
    }

    /**
     * 初始化视图
     */
    private void initView()
    {
        gv_groupdetail = (GridView)findViewById(R.id.gv_groupdetail);
        bt_groupdetail_out = (Button)findViewById(R.id.bt_groupdetail_out);
    }

    /**
     * 获取传递过来的数据
     * @return
     */
    public void getData()
    {
        Intent intent=getIntent();
        String groupId=intent.getStringExtra(Constant.GROUP_ID);

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

GridView 适配器

public class GroupDetailAdapter extends BaseAdapter
{
    private Context mContext;
    private boolean mIsCanModify;//是否允许添加和删除群成员
    private List<UserInfo>mUsers=new ArrayList<>();
    private boolean mIsDeleteModel;// 删除模式  true:表示可以删除; false:表示不可以删除
    private OnGroupDetailListener mOnGroupDetailListener;

    public GroupDetailAdapter(Context mContext, boolean mIsCanModify,OnGroupDetailListener onGroupDetailListener)
    {
        this.mContext = mContext;
        this.mIsCanModify = mIsCanModify;

        mOnGroupDetailListener=onGroupDetailListener;
    }

    /**
     * 获取当前的删除模式
     * @return
     */
    public boolean ismIsDeleteModel()
    {
        return mIsDeleteModel;
    }

    /**
     * 设置当前的删除模式
     * @param mIsDeleteModel
     */
    public void setmIsDeleteModel(boolean mIsDeleteModel)
    {
        this.mIsDeleteModel=mIsDeleteModel;
    }

    /**
     * 刷新数据
     * @param users
     */
    public void refresh(List<UserInfo>users)
    {
        if(users!=null && users.size()>=0)
        {
            mUsers.clear();

            // 添加加号和减号
            initUsers();

            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)
    {
        // 获取或创建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_detail_delete);
            holder.name=(TextView)convertView.findViewById(R.id.tv_group_detail_name);

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

        // 获取当前item数据
        final UserInfo userInfo=mUsers.get(position);

        // 显示数据
        if(mIsCanModify)// 群主或开放了群权限
        {
            // 布局的处理
            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// 群成员
            {
                convertView.setVisibility(View.VISIBLE);
                holder.name.setVisibility(View.VISIBLE);

                holder.name.setText(userInfo.getName());
                holder.photo.setImageResource(R.drawable.em_default_avatar);

                if(mIsDeleteModel)
                {
                    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(!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(userInfo);
                    }
                });
            }
        }
        else// 普通的群成员
        {
            if(position==getCount()-1 || position==getCount()-2)
            {
                convertView.setVisibility(View.GONE);
            }
            else
            {
                convertView.setVisibility(View.VISIBLE);

                // 名称
                holder.name.setText(userInfo.getName());
                // 头像
                holder.photo.setImageResource(R.drawable.em_default_avatar);
                // 删除
                holder.delete.setVisibility(View.GONE);
            }
        }
        // 返回view
        return convertView;
    }

    private class ViewHolder
    {
        private ImageView photo;
        private ImageView delete;
        private TextView name;
    }

    public interface OnGroupDetailListener
    {
        // 添加群成员方法
        void onAddMembers();

        // 删除群成员方法
        void onDeleteMember(UserInfo user);
    }
}

相关文章

网友评论

      本文标题:IM社交App04

      本文链接:https://www.haomeiwen.com/subject/idhtaftx.html