Android简单实现QQ联系人列表源码,要求有好友分组,点击好友可以模拟聊天(不需要联网)
时间: 2024-03-18 13:45:15 浏览: 142
基于Android的仿QQ好友分组ExpandableListView设计源码
以下是一个稍微复杂一些的 Android 实现 QQ 联系人列表并模拟聊天的源码示例:
1. 在布局文件中定义一个 ExpandableListView 控件,用于展示联系人列表和好友分组:
```xml
<ExpandableListView
android:id="@+id/expandableListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:groupIndicator="@null" />
```
2. 创建一个 Friend 类,用于表示每个好友的信息:
```java
public class Friend {
private String name;
private String avatarUrl;
// 其他好友信息的字段
public Friend(String name, String avatarUrl) {
this.name = name;
this.avatarUrl = avatarUrl;
}
// getter 和 setter 方法
}
```
3. 创建一个 Group 类,用于表示每个好友分组的信息:
```java
public class Group {
private String name;
private List<Friend> friends;
public Group(String name, List<Friend> friends) {
this.name = name;
this.friends = friends;
}
// getter 和 setter 方法
}
```
4. 创建一个 FriendAdapter 类,继承自 BaseExpandableListAdapter,用于在 ExpandableListView 中展示好友分组和联系人列表:
```java
public class FriendAdapter extends BaseExpandableListAdapter {
private List<Group> groups;
private Context context;
public FriendAdapter(Context context, List<Group> groups) {
this.context = context;
this.groups = groups;
}
@Override
public int getGroupCount() {
return groups.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return groups.get(groupPosition).getFriends().size();
}
@Override
public Object getGroup(int groupPosition) {
return groups.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return groups.get(groupPosition).getFriends().get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.item_group, parent, false);
}
TextView groupNameTextView = convertView.findViewById(R.id.groupNameTextView);
groupNameTextView.setText(groups.get(groupPosition).getName());
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.item_friend, parent, false);
}
Friend friend = groups.get(groupPosition).getFriends().get(childPosition);
ImageView avatarImageView = convertView.findViewById(R.id.avatarImageView);
TextView nameTextView = convertView.findViewById(R.id.nameTextView);
// 设置好友头像和姓名
Glide.with(context).load(friend.getAvatarUrl()).into(avatarImageView);
nameTextView.setText(friend.getName());
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
```
5. 创建一个 item_group.xml 布局文件,用于展示每个好友分组的名称:
```xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/groupNameTextView"
android:layout_width="match_parent"
android:layout_height="40dp"
android:gravity="center_vertical"
android:paddingStart="16dp"
android:textSize="16sp"
android:textColor="@android:color/black"
android:background="@android:color/darker_gray" />
```
6. 创建一个 item_friend.xml 布局文件,用于展示每个好友的头像和姓名:
```xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="80dp"
android:padding="16dp">
<ImageView
android:id="@+id/avatarImageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/avatar_placeholder"
android:scaleType="centerCrop" />
<TextView
android:id="@+id/nameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/avatarImageView"
android:layout_marginStart="16dp"
android:textSize="18sp"
android:textColor="@android:color/black"
android:text="好友姓名" />
</RelativeLayout>
```
7. 在 MainActivity 中获取好友分组和联系人列表数据,将其展示在 ExpandableListView 中,并添加点击事件模拟聊天:
```java
public class MainActivity extends AppCompatActivity {
private ExpandableListView expandableListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
expandableListView = findViewById(R.id.expandableListView);
// 模拟获取好友分组和联系人列表数据
List<Group> groups = new ArrayList<>();
List<Friend> friends1 = new ArrayList<>();
friends1.add(new Friend("张三", "https://xxx.com/avatar1.jpg"));
friends1.add(new Friend("李四", "https://xxx.com/avatar2.jpg"));
friends1.add(new Friend("王五", "https://xxx.com/avatar3.jpg"));
Group group1 = new Group("我的好友", friends1);
groups.add(group1);
// 创建好友列表适配器
FriendAdapter adapter = new FriendAdapter(this, groups);
// 设置好友列表适配器
expandableListView.setAdapter(adapter);
// 为每个好友添加点击事件,模拟聊天
expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
Friend friend = groups.get(groupPosition).getFriends().get(childPosition);
Toast.makeText(MainActivity.this, "正在与 " + friend.getName() + " 聊天...", Toast.LENGTH_SHORT).show();
return true;
}
});
}
}
```
这样就实现了一个稍微复杂一些的 QQ 联系人列表,并且可以点击好友模拟聊天。请注意,这只是一个示例,实际开发中可能需要更复杂的逻辑和界面设计。
阅读全文