android 群成员界面,硅谷社交12-群列表页面

硅谷社交12--群列表页面

231319457.png

1)页面布局

xmlns:tools="http://schemas.android.com/tools"

xmlns:app="http://schemas.android.com/apk/res-auto"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

app:titleBarTitle="群组列表"

android:layout_width="match_parent"

android:layout_height="wrap_content">

android:id="@+id/lv_grouplist"

android:layout_width="match_parent"

android:layout_height="match_parent">

2)Listview添加头布局

// 获取头布局view

View headerView = View.inflate(GroupListActivity.this, R.layout.header_group_list, null);

// 添加头布局view

lv_group_list.addHeaderView(headerView);

3)Listview初始化

// 创建适配器

mGroupListAdapter = new GroupListAdapter(GroupListActivity.this);

// 将适配器添加到listview中

lv_group_list.setAdapter(mGroupListAdapter);

4)Listview适配器

public class GroupListAdapter extends BaseAdapter {

private Context mContext;

private List mEMGroups = new ArrayList<>();

public GroupListAdapter(Context context) {

mContext = context;

}

// 刷新方法

public void refresh(List eMGroups){

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

// 加载数据

mEMGroups.clear();

mEMGroups.addAll(eMGroups);

// 通知刷新页面

notifyDataSetChanged();

}

}

@Override

public int getCount() {

return mEMGroups == null? 0:mEMGroups.size();

}

@Override

public Object getItem(int position) {

return mEMGroups.get(position);

}

@Override

public long getItemId(int position) {

return position;

}

@Override

public View getView(int position, View convertView, ViewGroup parent) {

// 创建或获取viewhoder

ViewHolder holder = null;

if(convertView == null) {

holder = new ViewHolder();

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

holder.tv_name = (TextView) convertView.findViewById(R.id.tv_group_list_name);

convertView.setTag(holder);

}else {

holder = (ViewHolder) convertView.getTag();

}

// 获取当前item数据

EMGroup emGroup = mEMGroups.get(position);

// 显示数据

holder.tv_name.setText(emGroup.getGroupName());

// 返回convertview

return convertView;

}

static class ViewHolder{

TextView tv_name;

}

}

5)从环信服务器获取群组联系人信息

// 联网获取群信息

private void getGroupFromHxServier() {

Model.getInstace().getGolbalThreadPool().execute(new Runnable() {

@Override

public void run() {

try {

// 联网获取群信息

EMClient.getInstance().groupManager().getJoinedGroupsFromServer();

runOnUiThread(new Runnable() {

@Override

public void run() {

Toast.makeText(GroupListActivity.this, "加载群信息成功", Toast.LENGTH_SHORT).show();

// 刷新显示

mGroupListAdapter.refresh(EMClient.getInstance().groupManager().getAllGroups());

}

});

} catch (HyphenateException e) {

e.printStackTrace();

runOnUiThread(new Runnable() {

@Override

public void run() {

Toast.makeText(GroupListActivity.this, "加载群信息失败", Toast.LENGTH_SHORT).show();

}

});

}

}

});

}

6)Listview刷新页面

// 刷新显示

mGroupListAdapter.refresh(EMClient.getInstance().groupManager().getAllGroups());

7)群列表条目的点击事件

// 群列表的点击事件

lv_group_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override

public void onItemClick(AdapterView> parent, View view, int position, long id) {

// 跳转到聊天页面

Intent intent = new Intent(GroupListActivity.this, ChatActivity.class);

// 获取群id

String groupId = EMClient.getInstance().groupManager().getAllGroups().get(position - 1).getGroupId();

intent.putExtra(EaseConstant.EXTRA_USER_ID, groupId);

// 保存群聊天类型

intent.putExtra(EaseConstant.EXTRA_CHAT_TYPE, EaseConstant.CHATTYPE_GROUP);

startActivity(intent);

}

});

8)创建新群的点击监听

// 群组条目的点击事件

ll_group_list.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

// 跳转到新建群页面

Intent intent = new Intent(GroupListActivity.this, NewGroupActivity.class);

startActivity(intent);

}

});

9)页面可见时刷新页面

@Override

protected void onResume() {

super.onResume();

// 刷新页面

mGroupListAdapter.refresh(EMClient.getInstance().groupManager().getAllGroups());

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值