你的位置:首页 > 操作系统

[操作系统]硅谷社交12


1)页面布局

<?

2)Listview添加头布局

// 获取头布局viewView headerView = View.inflate(GroupListActivity.this, R.layout.header_group_list, null);// 添加头布局viewlv_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)页面可见时刷新页面

@Overrideprotected void onResume() {	super.onResume();	// 刷新页面	mGroupListAdapter.refresh(EMClient.getInstance().groupManager().getAllGroups());}