矽谷社交12--群列表頁面,矽谷社交12--列表

來源:互聯網
上載者:User

矽谷社交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添加頭布局

// 擷取頭布局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() {@Overridepublic void run() {try {// 連網擷取群資訊EMClient.getInstance().groupManager().getJoinedGroupsFromServer();runOnUiThread(new Runnable() {@Overridepublic void run() {Toast.makeText(GroupListActivity.this, "載入群資訊成功", Toast.LENGTH_SHORT).show();// 重新整理顯示mGroupListAdapter.refresh(EMClient.getInstance().groupManager().getAllGroups());}});} catch (HyphenateException e) {e.printStackTrace();runOnUiThread(new Runnable() {@Overridepublic 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() {@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position, long id) {// 跳轉到聊天頁面Intent intent = new Intent(GroupListActivity.this, ChatActivity.class);// 擷取群idString 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() {@Overridepublic 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());}

  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.