📅  最后修改于: 2023-12-03 14:59:17.553000             🧑  作者: Mango
介绍:
在Android中,我们经常用到ListView来展示数据列表。但是默认的ListView无法满足我们自定义的需求。本文将介绍如何通过自定义ListView来添加图像和副标题。
步骤:
创建自定义布局文件list_item.xml。
在该布局文件中,我们可以自由地添加ImageView、TextView等控件,以满足我们的需求。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/imageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@mipmap/ic_launcher"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginStart="8dp">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Title"
android:textSize="16sp"
android:textColor="#333"/>
<TextView
android:id="@+id/subtitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Subtitle"
android:textSize="14sp"
android:textColor="#999"/>
</LinearLayout>
</LinearLayout>
创建自定义适配器Adapter。
在Adapter中,我们需要实现getView()方法,并在其中动态地设置list_item布局中的控件。
public class MyAdapter extends BaseAdapter {
private final List<MyItem> items;
private final LayoutInflater inflater;
public MyAdapter(Context context, List<MyItem> items) {
this.items = items;
inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return items.size();
}
@Override
public Object getItem(int position) {
return items.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item, parent, false);
holder = new ViewHolder();
holder.imageView = convertView.findViewById(R.id.imageView);
holder.titleTextView = convertView.findViewById(R.id.title);
holder.subtitleTextView = convertView.findViewById(R.id.subtitle);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
MyItem item = items.get(position);
holder.imageView.setImageResource(item.getImageResId());
holder.titleTextView.setText(item.getTitle());
holder.subtitleTextView.setText(item.getSubtitle());
return convertView;
}
static class ViewHolder {
ImageView imageView;
TextView titleTextView;
TextView subtitleTextView;
}
}
创建数据模型类MyItem。
MyItem类用于定义ListView中的每个条目所需要的数据,包括图像、标题和副标题。
public class MyItem {
private final int imageResId;
private final String title;
private final String subtitle;
public MyItem(int imageResId, String title, String subtitle) {
this.imageResId = imageResId;
this.title = title;
this.subtitle = subtitle;
}
public int getImageResId() {
return imageResId;
}
public String getTitle() {
return title;
}
public String getSubtitle() {
return subtitle;
}
}
在Activity中使用自定义适配器Adapter。
最后,在Activity中将自定义适配器Adapter与ListView关联起来,并设置ListView的数据源List
public class MainActivity extends AppCompatActivity {
private ListView listView;
private MyAdapter adapter;
private List<MyItem> items;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.listView);
items = new ArrayList<>();
items.add(new MyItem(R.mipmap.ic_launcher, "Item 1", "Subtitle 1"));
items.add(new MyItem(R.mipmap.ic_launcher, "Item 2", "Subtitle 2"));
items.add(new MyItem(R.mipmap.ic_launcher, "Item 3", "Subtitle 3"));
items.add(new MyItem(R.mipmap.ic_launcher, "Item 4", "Subtitle 4"));
items.add(new MyItem(R.mipmap.ic_launcher, "Item 5", "Subtitle 5"));
items.add(new MyItem(R.mipmap.ic_launcher, "Item 6", "Subtitle 6"));
items.add(new MyItem(R.mipmap.ic_launcher, "Item 7", "Subtitle 7"));
items.add(new MyItem(R.mipmap.ic_launcher, "Item 8", "Subtitle 8"));
items.add(new MyItem(R.mipmap.ic_launcher, "Item 9", "Subtitle 9"));
items.add(new MyItem(R.mipmap.ic_launcher, "Item 10", "Subtitle 10"));
adapter = new MyAdapter(this, items);
listView.setAdapter(adapter);
}
}
效果图:
注意事项:
参考文献: