硅谷社交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() {
	@Override
	public 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() {
	@Override
	public 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();
	}
});

  

时间: 2024-10-17 01:48:44

硅谷社交14--选择联系人页面的相关文章

硅谷社交13--新建群页面

1)页面布局 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.an

硅谷社交10--会话详情页面

1)页面布局 会话页面.png 群聊天页面.png 2)创建环信提供的会话页面 // 创建会话页面的fragment easeChatFragment = new EaseChatFragment(); // 获取环信id hxid = getIntent().getExtras().getString(EaseConstant.EXTRA_USER_ID); // 获取聊天类型 chatType = getIntent().getExtras().getInt(EaseConstant.EXT

硅谷社交3--登录页面

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&

iOS学习 - 14.本地联系人

苹果在iOS9的SDK中废除了AddressBookUI.framework的一些功能(是不是这个库都废除了,有待验证),具体和保存联系人相关的几个系统界面如下:联系人选择:AddressBookUI/ABPeoplePickerNavigationController.h联系人详情:AddressBookUI/ABPersonViewController.h未知联系人:AddressBookUI/ABUnknownPersonViewController.h新建联系人:AddressBookU

Android读取选择联系人的模版代码实现

1.创建SelectContactActivity类,在AndroidManifest.xml添加类 import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Activity; import android.content.ContentResolver; import android.content.Intent;

搜索和选择联系人

相关类packages/apps/Contacts/src/com/android/contacts/activities/ActionBarAdapter.javapackages/apps/Contacts/src/com/android/contacts/list/ContactEntryListFragment.javapackages/providers/ContactsProvider/src/com/android/providers/contacts/NameNormalizer

硅谷社交9--邀请信息列表页面

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&

硅谷社交8--联系人列表页面

1.是否有邀请信息红点的设置 // 获取当前是否有新的邀请信息 boolean is_notify = SpUtils.getInstace(IMApplication.getGlobalApplication()).getBoolean(SpUtils.IS_INVITE_NOTIY, false); iv_contact_notify.setVisibility(is_notify ? View.VISIBLE : View.GONE); 2.注册联系人邀请信息变化的广播 private B

硅谷社交5--联系人列表页面

1.easeui ContactListFragment extends EaseContactListFragment后页面如下图 2.标题加号的设置及点击事件监听 // 头部图标设置 titleBar.setRightImageResource(R.drawable.em_add); // 加号添加联系人 titleBar.getRightLayout().setOnClickListener(new View.OnClickListener() { @Override public voi