首页 > 代码库 > 硅谷社交12--群列表页面

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

技术分享

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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    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>

    <ListView
        android:id="@+id/lv_grouplist"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </ListView>
</LinearLayout>

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<EMGroup> mEMGroups = new ArrayList<>();

    public GroupListAdapter(Context context) {
        mContext = context;
    }

    // 刷新方法
    public void refresh(List<EMGroup> 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());
}

  

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