android studio编写一个简单好友列表
时间: 2023-05-24 09:05:03 浏览: 135
Android Studio实现QQ空间布置
5星 · 资源好评率100%
很抱歉,我是一名语言模型,没有编写代码的能力。不过我可以提供一个简单的好友列表的demo,您可以参考一下:
1. 在layout文件夹中新建一个xml文件,取名为list_item_friend.xml。代码如下:
```
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<ImageView
android:id="@+id/avatar"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@drawable/avatar_default"
android:layout_centerVertical="true"/>
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/avatar"
android:layout_marginLeft="10dp"
android:textSize="20sp"
android:layout_alignParentTop="true"/>
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/avatar"
android:layout_marginLeft="10dp"
android:textSize="14sp"
android:layout_below="@id/name"/>
</RelativeLayout>
```
2. 新建一个MainActivity,并在onCreate()方法中添加以下代码:
```
public class MainActivity extends AppCompatActivity {
private ListView listView;
private FriendListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.friend_list);
adapter = new FriendListAdapter(this, getFriendList());
listView.setAdapter(adapter);
}
private List<Friend> getFriendList() {
List<Friend> friendList = new ArrayList<>();
friendList.add(new Friend("Alice", "Hello!"));
friendList.add(new Friend("Bob", "Hi!"));
friendList.add(new Friend("Carol", "How are you?"));
friendList.add(new Friend("David", "Nice to meet you!"));
friendList.add(new Friend("Emma", "I'm fine, thank you."));
return friendList;
}
}
```
3. 新建一个Friend类,在里面定义好友的基本信息:
```
public class Friend {
private String name;
private String message;
public Friend(String name, String message) {
this.name = name;
this.message = message;
}
public String getName() {
return name;
}
public String getMessage() {
return message;
}
}
```
4. 新建一个FriendListAdapter类,继承自BaseAdapter,并在里面实现getView()方法:
```
public class FriendListAdapter extends BaseAdapter {
private List<Friend> friendList;
private LayoutInflater inflater;
public FriendListAdapter(Context context, List<Friend> friendList) {
this.friendList = friendList;
inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return friendList.size();
}
@Override
public Object getItem(int position) {
return friendList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item_friend, parent, false);
holder = new ViewHolder();
holder.avatar = convertView.findViewById(R.id.avatar);
holder.name = convertView.findViewById(R.id.name);
holder.message = convertView.findViewById(R.id.message);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.name.setText(friendList.get(position).getName());
holder.message.setText(friendList.get(position).getMessage());
return convertView;
}
class ViewHolder {
ImageView avatar;
TextView name;
TextView message;
}
}
```
5. 新建一个drawable文件夹,在里面添加一个名为avatar_default的图片,作为好友的默认头像。
6. 运行程序,就能看到一个简单的好友列表了。
希望这个demo能够对您有所帮助!
阅读全文