项目需求
完善WECHAT项目中的“通讯录”模块,添加“+”按钮,实现添加联系人,并在通讯录界面实现RecyclerView的视图布局
截图展示
![](https://i-blog.csdnimg.cn/blog_migrate/32cf25ea6bcd7ae7a615e6996802f593.jpeg)
实现步骤
1.在相关xml中添加RecyclerView,参照该连接实现RecyclerView的布局(展开与收缩),主要设计:在item里将主内容和副内容写出来,通关点击item副内容现实和隐藏来达到效果
RecyclerView设计参考链接
public void onBindViewHolder(final adapter_expand.expandviewholder holder, int position) {
//holder.tvTeam.setText(list.get(position));
//holder.tvTeamChild.setText(list.get(position) + "的联系方式");
holder.tvTeam.setText(data.get(position).get("name"));
holder.tvTeamChild.setText("电话:"+data.get(position).get("phoneNumber"));
final boolean isExpanded = position == expandedPosition;
holder.rlChild.setVisibility(isExpanded ? View.VISIBLE : View.GONE);
holder.rlParent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mViewHolder != null) {
mViewHolder.rlChild.setVisibility(View.GONE);
notifyItemChanged(expandedPosition);
}
expandedPosition = isExpanded ? -1 : holder.getAdapterPosition();
mViewHolder = isExpanded ? null : holder;
notifyItemChanged(holder.getAdapterPosition());
}
});
}
item.expand.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
android:layout_margin="5dp"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/rl_parent"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#009ACD">
<TextView
android:id="@+id/tv_team"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textColor="@android:color/white"
tools:text="主布局" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/rl_child"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#B0C4DE"
android:visibility="gone">
<TextView
android:id="@+id/tv_team_child"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textColor="@android:color/white"
tools:text="副布局" />
</RelativeLayout>
</LinearLayout>
2.在top.xml中添加一个button,用于联系人的添加
3.新建add_contact.xml用于点击添加按钮之后的页面
4.更改contactFragment.java,实现数据的获取(部分代码)
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.tab03, container, false);
recyclerView=view.findViewById(R.id.rcv_expandcollapse);
context=this.getActivity();
initexpandData();
adapter=new adapter_expand(context,data);
LinearLayoutManager manager=new LinearLayoutManager(context);
manager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(manager);
recyclerView.setHasFixedSize(true);
recyclerView.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL));
return view;
}
private void initexpandData(){
Uri uri= ContactsContract.Contacts.CONTENT_URI;
ContentResolver resolver = context.getContentResolver();
Cursor cursor=resolver.query(uri, null, null, null, null); //得到记录集
if(cursor!=null){
while(cursor.moveToNext()){
//先获取联系人_id字段的索引号后再获取_id值
int idFieldIndex=cursor.getColumnIndex("_id");
int id=cursor.getInt(idFieldIndex);
//先获取联系人姓名字段的索引号后再获取姓名字段值
int nameFieldIndex = cursor.getColumnIndex("display_name");
String name=cursor.getString(nameFieldIndex);
int numCountFieldIndex=cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);
int numCount=cursor.getInt(numCountFieldIndex); //获取联系人的电话号码个数
String phoneNumber="";
if(numCount>0){ //联系人有至少一个电话号码
//在类ContactsContract.CommonDataKinds.Phone中根据id查询相应联系人的所有电话;
Cursor phonecursor=getActivity().getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"=?",
new String[]{Integer.toString(id)}, null);
if(phonecursor!=null){
if(phonecursor.moveToFirst()){ //仅读取第一个电话号码
int numFieldIndex=phonecursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
phoneNumber=phonecursor.getString(numFieldIndex);
}
phonecursor.close();
}
}
item=new HashMap<String,String>(); //必须循环创建
item.put("name", name);
item.put("phoneNumber", phoneNumber);
data.add(item);
}
cursor.close();
}
}
5.修改MainActivity,在MainActivity中需要添加一个授权监听函数以及对button按钮的监听
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.READ_CONTACTS},1);
}else{
initView();
initFragment();
selectFragment(0);
initEvent();
}
}
public void onClick(View v) {
resetImgs();
switch(v.getId()){
case R.id.id_tab_weixin:
selectFragment(0);
break;
case R.id.id_tab_friends:
selectFragment(1);
break;
case R.id.id_tab_contacts:
selectFragment(2);
break;
case R.id.id_tab_setting:
selectFragment(3);
break;
case R.id.id_add_img:
final View addDialog=getLayoutInflater().inflate(R.layout.add_contact,null);
new AlertDialog.Builder(MainActivity.this).setView(addDialog).setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
EditText mname=(EditText)addDialog.findViewById(R.id.name);
EditText mphone=(EditText)addDialog.findViewById(R.id.phone);
final String name=mname.getText().toString();
final String phone=mphone.getText().toString();
// 创建一个空的ContentValues
ContentValues values = new ContentValues();
// 向RawContacts.CONTENT_URI执行一个空值插入
// 目的是获取系统返回的rawContactId
Uri rawContactUri = getContentResolver().insert(
ContactsContract.RawContacts.CONTENT_URI, values);
long rawContactId = ContentUris.parseId(rawContactUri);
values.clear();
values.put(Data.RAW_CONTACT_ID, rawContactId);
// 设置内容类型
values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
// 设置联系人名字
values.put(StructuredName.GIVEN_NAME, name);
// 向联系人URI添加联系人名字
getContentResolver().insert(ContactsContract
.Data.CONTENT_URI, values);
values.clear();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
// 设置联系人的电话号码
values.put(Phone.NUMBER, phone);
// 设置电话类型
values.put(Phone.TYPE, Phone.TYPE_MOBILE);
// 向联系人电话号码URI添加电话号码
getContentResolver().insert(ContactsContract
.Data.CONTENT_URI, values);
values.clear();
selectFragment(2);
Toast.makeText(MainActivity.this, "联系人数据添加成功",
Toast.LENGTH_SHORT).show();
}
}).show();
break;
default:
break;
}
}
参考链接
RecyclerView 顶部悬浮
RecyclerView 展开与收缩
完整代码
(包括RecyclerView 顶部悬浮(吸顶)设计的实现)
gitee