目录
功能介绍
- 使用recyclerview在页面中展示好友
- 好友实现按照昵称首汉字字母排序
- 实现点击好友跳转详细资料界面
效果预览
功能实现
1. 通讯录
通讯录fragment2.xml使用的是Frame布局镶嵌于MainLayout.xml中,在FrameLayout中再添加recyclerview。
fragment2.xml
预览:
代码:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/RecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
item.xml
预览:
代码:
<?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.android.com/tools"
android:id="@+id/item"
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="vertical">
<TextView
android:id="@+id/title_group"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_weight="1"
android:background="@android:color/system_neutral1_100"
android:text="a"
android:visibility="gone" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal">
<ImageView
android:id="@+id/friend_image"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="5dp"
android:scaleType="centerInside"
app:srcCompat="@drawable/icon_addfriend" />
<TextView
android:id="@+id/friend_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/border_bottom"
android:gravity="center_vertical"
android:text="新的朋友"
android:textSize="20dp" />
</LinearLayout>
</LinearLayout>
- 排序功能实现思路:每个好友设置一个group属性,在创建的时候会自动将昵称的首汉字的首字母赋值给group(特殊的,“公众号”、“标签”等功能选项不参与分组且在通讯录最前面,因此group属性应制定默认值,默认值ASCⅡ码保证最小)。
- 然后将所有好友用List集合保存,在List集合中就按照group升序排序。
- 然后布局中,每个item添加一个textview在最前面,默认不显示,在recyclerview加载item的时候,判断当前的当前的好友的group于上一个好友的group是否相等,如果不相等则显示textview,并用textview显示gruop,再加载item;如果相等则之家加载item。
Person.java(好友)
代码:
package com.example.whj;
public class Person implements Comparable<Person>{
private String name;
private int image;
private char group;
public char getGroup() {
return group;
}
public void setGroup(char group) {
this.group = group;
}
public Person(String name, int image, char gruop) {
this.name = name;
this.image = image;
this.group = gruop;
}
public Person(String name, int image) {
this.name = name;
this.image = image;
this.group = Tool.getFirstLetter(name);
}
@Override
public int compareTo(Person o) {
return this.group - o.getGroup();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getImage() {
return image;
}
public void setImage(int image) {
this.image = image;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", image=" + image +
", group=" + group +
'}';
}
}
该类实现Comparable接口方便后面使用Collections.sort()方法排序。通过Tool.getFirstLetter(name)获得昵称的首汉字的首字母,Tool类参考自https://blog.csdn.net/chenbing81/article/details/51960019
Fragment2 .java
代码:
package com.example.whj;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Fragment2 extends Fragment {
private RecyclerView recyclerView;
private Myadapter myadapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment2, container, false);
List<Person> people = initData();
recyclerView = view.findViewById(R.id.RecyclerView);
Context context = getContext();
myadapter = new Myadapter(people, context);
LinearLayoutManager manager = new LinearLayoutManager(context);
manager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(manager);
recyclerView.setAdapter(myadapter);
return view;
}
public List<Person> initData() {
List<Person> data = new ArrayList<>();
data.add(new Person("张三",R.drawable.f_static_00));
data.add(new Person("????",R.drawable.f_static_01));
data.add(new Person("新的朋友",R.drawable.icon_addfriend,'0'));
data.add(new Person("群聊",R.drawable.icon_qunliao,'0'));
data.add(new Person("标签",R.drawable.icon_biaoqian,'0'));
data.add(new Person("公众号",R.drawable.icon_public,'0'));
data.add(new Person("韦海杰",R.drawable.f_static_02));
data.add(new Person("李娜娜",R.drawable.f_static_03));
data.add(new Person("朝锅巴",R.drawable.f_static_04));
data.add(new Person("欧阳嘎哈",R.drawable.f_static_05));
data.add(new Person("王老五",R.drawable.f_static_06));
data.add(new Person("姐木心#肿么记伱",R.drawable.f_static_07));
data.add(new Person("消遣",R.drawable.f_static_08));
data.add(new Person("刘子俺",R.drawable.f_static_09));
data.add(new Person("永不言败",R.drawable.f_static_010));
data.add(new Person("滴水穿石",R.drawable.f_static_011));
data.add(new Person("水清云淡",R.drawable.f_static_012));
data.add(new Person("天会亮心会暖",R.drawable.f_static_013));
data.add(new Person("张召忠",R.drawable.f_static_013));
data.add(new Person("花落花开",R.drawable.f_static_014));
data.add(new Person("南宫涵",R.drawable.f_static_015));
data.add(new Person("辛格才",R.drawable.f_static_016));
data.add(new Person("汪汪队",R.drawable.f_static_017));
data.add(new Person("高鬼魂",R.drawable.f_static_018));
Collections.sort(data);
return data;
}
}
该类用于加载数据和设置recyclerView。
Myadapter.java
代码:
package com.example.whj;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class Myadapter extends RecyclerView.Adapter<Myadapter.MyViewHolder> {
private List<Person> mydata;
private Context mycontext;
public Myadapter(List<Person> data, Context context) {
mydata = data;
mycontext = context;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mycontext).inflate(R.layout.item,parent,false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
Person cur = mydata.get(position);
Person last;
//实现排序功能
if (position == 0){
holder.textView.setText(cur.getName());
holder.imageView.setImageResource(cur.getImage());
holder.title.setVisibility(View.GONE);
} else {
last = mydata.get(position - 1);
if (cur.getGroup() != last.getGroup()) {
holder.item.setMinimumHeight(80);
holder.title.setText(" " + cur.getGroup());
holder.title.setVisibility(View.VISIBLE);
holder.textView.setText(cur.getName());
holder.imageView.setImageResource(cur.getImage());
} else {
holder.textView.setText(cur.getName());
holder.imageView.setImageResource(cur.getImage());
holder.title.setVisibility(View.GONE);
}
}
//点击跳转个人详细资料页面
holder.item.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bundle bundle = new Bundle();
bundle.putInt("image", cur.getImage());
bundle.putString("name", cur.getName());
Intent intent = new Intent(mycontext,PersonalActivity.class);
intent.putExtra("data",bundle);
mycontext.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return mydata.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder{
private TextView textView;
private ImageView imageView;
private TextView title;
private LinearLayout item;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.friend_text);
imageView = itemView.findViewById(R.id.friend_image);
title = itemView.findViewById(R.id.title_group);
item = itemView.findViewById(R.id.item);
}
}
}
该类用于加载item和实现点击跳转。
2. 详细资料
personal_document.xml
预览:
代码:
<?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.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/material_dynamic_neutral95"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="34dp"
android:orientation="horizontal"
android:background="@color/white">
<ImageView
android:id="@+id/document_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="7dp"
android:layout_weight="1"
app:srcCompat="@drawable/icon_back" />
<TextView
android:layout_width="325dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<ImageView
android:id="@+id/document_more"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="@drawable/icon_more" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="303dp"
android:orientation="vertical"
android:background="@color/white">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/document_image"
android:layout_width="271dp"
android:layout_height="113dp"
android:layout_gravity="center"
android:layout_weight="1"
android:scaleType="fitCenter"
tools:srcCompat="@drawable/f_static_01" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/document_remarks"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="35dp"
android:text="备注"
android:textColor="@color/black"
android:textSize="30dp"
android:textStyle="bold" />
<TextView
android:id="@+id/document_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:layout_marginTop="5dp"
android:text="昵称:"
android:textSize="15dp" />
<TextView
android:id="@+id/document_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:text="微信号:"
android:textSize="15dp" />
<TextView
android:id="@+id/document_area"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:text="地区:"
android:textSize="15dp" />
</LinearLayout>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/material_dynamic_neutral95" />
<LinearLayout
android:id="@+id/document_setting"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:text="设置备注和标签 "
android:textColor="@color/black"
android:textSize="20dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="15dp"
android:layout_weight="1"
android:scaleType="fitEnd"
app:srcCompat="@drawable/right" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/material_dynamic_neutral95" />
<LinearLayout
android:id="@+id/document_power"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:text="朋友权限"
android:textColor="@color/black"
android:textSize="20dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="15dp"
android:layout_weight="1"
android:scaleType="fitEnd"
app:srcCompat="@drawable/right" />
</LinearLayout>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="10dp"
android:background="@color/material_dynamic_neutral95" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/white">
<LinearLayout
android:id="@+id/document_friend_circle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:text="朋友圈"
android:textColor="@color/black"
android:textSize="20dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="15dp"
android:layout_weight="1"
android:scaleType="fitEnd"
app:srcCompat="@drawable/right" />
</LinearLayout>
<LinearLayout
android:id="@+id/document_more_information"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:text="更多信息"
android:textColor="@color/black"
android:textSize="20dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="15dp"
android:layout_weight="1"
android:scaleType="fitEnd"
app:srcCompat="@drawable/right" />
</LinearLayout>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="10dp"
android:background="@color/material_dynamic_neutral95" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/white">
<LinearLayout
android:id="@+id/document_sent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_weight="1"
android:scaleType="fitEnd"
app:srcCompat="@drawable/weixin_normal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:text="发信息"
android:textColor="@color/Turquoise3"
android:textSize="20dp" />
</LinearLayout>
<LinearLayout
android:id="@+id/document_call"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_weight="1"
android:scaleType="fitEnd"
app:srcCompat="@drawable/chat_video_call_receive" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:text="音视频通话"
android:textColor="@color/Turquoise3"
android:textSize="20dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
PersonalActivity.java
代码:
package com.example.whj;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Instrumentation;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
public class PersonalActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.personal_document);
Intent intent = getIntent();
Bundle bundle = intent.getBundleExtra("data");
int image = bundle.getInt("image");
String name = bundle.getString("name");
ImageView imageView = findViewById(R.id.document_back);
ImageView imageView1 = findViewById(R.id.document_image);
TextView textView = findViewById(R.id.document_name);
TextView textView1 = findViewById(R.id.document_remarks);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
textView1.setText(name);
imageView1.setImageResource(image);
textView.append(name);
}
}