矽谷社交14--選擇連絡人頁面,矽谷社交14--連絡人

來源:互聯網
上載者:User

矽谷社交14--選擇連絡人頁面,矽谷社交14--連絡人

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"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <RelativeLayout        android:background="@android:color/holo_blue_light"        android:layout_width="match_parent"        android:layout_height="60dp">        <TextView            android:text="選擇連絡人"            android:textSize="25sp"            android:textColor="@android:color/white"            android:layout_centerInParent="true"            android:layout_width="wrap_content"            android:layout_height="wrap_content" />        <TextView            android:id="@+id/tv_pick_save"            android:text="儲存"            android:textSize="25sp"            android:textColor="@android:color/white"            android:layout_alignParentRight="true"            android:gravity="center"            android:layout_marginRight="5dp"            android:layout_width="wrap_content"            android:layout_height="match_parent" />    </RelativeLayout>    <ListView        android:id="@+id/lv_pick"        android:layout_width="match_parent"        android:layout_height="match_parent">    </ListView></LinearLayout>

2)擷取好友名單

// 擷取所有連絡人的資料List<UserInfo> contacts = Model.getInstace().getDbManager().getContactTableDao().getContacts();mPicks = new ArrayList<>();// 校正if(contacts != null && contacts.size() >= 0) {// 將連絡人資訊轉換為選擇連絡人bean資訊PickContactInfo pickContactInfo = null;for (UserInfo contact: contacts){pickContactInfo = new PickContactInfo(contact, false);mPicks.add(pickContactInfo);}}

3)初始化listview

// 建立適配器mPickContactsAdapter = new PickContactsAdapter(PickContactsActivity.this, mPicks, mExistingMembers);// 添加適配器lv_pick_contacts.setAdapter(mPickContactsAdapter);

4)Listview適配器

public class PickContactsAdapter extends BaseAdapter {    private Context mContext;    private List<PickContactInfo> mPicks = new ArrayList<>();    private List<String> mExistingMembers  = new ArrayList<>();    public PickContactsAdapter(Context context , List<PickContactInfo> picks,List<String> existingMembers) {        mContext = context;        if(picks != null && picks.size() >= 0) {            mPicks.clear();            mPicks.addAll(picks);        }        // 接受群中已經存在的群成員的環信id        if(existingMembers != null && existingMembers.size() >=0 ) {            mExistingMembers.clear();            mExistingMembers.addAll(existingMembers);        }    }    // 擷取選中的連絡人    public List<String> getAddMembers(){        // 準備一個要返回的資料集合        List<String> names = new ArrayList<>();        // 遍曆集合 選擇出選中狀態的連絡人        for (PickContactInfo pick: mPicks){            if(pick.isChecked()) {                names.add(pick.getUser().getName());            }        }        return names;    }    @Override    public int getCount() {        return mPicks == null? 0:mPicks.size();    }    @Override    public Object getItem(int position) {        return mPicks.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_pick_contacts, null);            holder.tv_name = (TextView) convertView.findViewById(R.id.tv_item_pick_contacts_name);            holder.cb_checked = (CheckBox) convertView.findViewById(R.id.cb_item_pick_contacts);            convertView.setTag(holder);        }else {            holder = (ViewHolder) convertView.getTag();        }        // 擷取當前item資料        PickContactInfo pickContactInfo = mPicks.get(position);        // 顯示資料        holder.tv_name.setText(pickContactInfo.getUser().getName());        holder.cb_checked.setChecked(pickContactInfo.isChecked());        if(mExistingMembers.contains(pickContactInfo.getUser().getHxId())) {            holder.cb_checked.setChecked(true);            pickContactInfo.setIsChecked(true);        }        // 返回view        return convertView;    }    static class ViewHolder{        TextView tv_name;        CheckBox cb_checked;    }}


5)Listview條目的點擊事件

// listView團隊item點擊事件處理lv_pick_contacts.setOnItemClickListener(new AdapterView.OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position, long id) {//  擷取當前item的checkbox對象CheckBox cb_item_pick_contacts = (CheckBox) view.findViewById(R.id.cb_item_pick_contacts);// 設定狀態cb_item_pick_contacts.setChecked(!cb_item_pick_contacts.isChecked());//                cb_item_pick_contacts.toggle();// 更新資料PickContactInfo pickContactInfo = mPicks.get(position);pickContactInfo.setIsChecked(cb_item_pick_contacts.isChecked());// 重新整理列表資料mPickContactsAdapter.notifyDataSetChanged();}});


6)儲存按鈕點擊事件

// 儲存按鈕的點擊監聽處理tv_pick_contacts_save.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// 擷取被選擇的連絡人List<String> addMembers = mPickContactsAdapter.getAddMembers();// 設定資料準備返回建立群頁面Intent intent = new Intent();intent.putExtra("members", addMembers.toArray(new String[0]));setResult(RESULT_OK, intent);// 結束當前頁面finish();}});

  

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.