实现复杂ListView列表功能
实现代码:
activity_main.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:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="汽车列表"
android:gravity="center"
android:padding="10dp"
android:textSize="24sp"
android:textColor="#ffffff"
android:background="#3D8DEF"/>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:dividerHeight="2dp"
android:divider="#aaaaaa"
android:id="@+id/aListView"/>
</LinearLayout>
list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:layout_margin="5dp">
<ImageView
android:layout_width="70dp"
android:layout_height="100dp"
android:id="@+id/carIcon"
android:layout_margin="5dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#0000ff"
android:id="@+id/nameView"
android:textSize="16sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#1492E6"
android:id="@+id/Introduction"
android:textSize="16sp"
android:layout_marginTop="10dp"/>
</LinearLayout>
</LinearLayout>
MainActivity:
package com.example.listview;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
private String[] names={"保时捷","奔驰AMG","巴博斯","劳斯莱斯"};
private String[] Introductions={"保时捷,德国大众汽车旗下世界著名豪华汽车品牌,又译波尔舍,总部位于德国斯图加特,是欧美汽车的主要代表之一。","梅赛德斯-AMG是梅赛德斯-奔驰的高性能一个子品牌,在高性能车的研发上,90年代AMG推出的经典作品包括了C36AMG、E50AMG、C43AMG以及1997年以后的奔驰各个版本的“55”系列。","巴博斯成立于1977年,是德国汽车品牌,主要生产改装车。德国巴博斯汽车公司是全球最大、最著名的个性化汽车生产商。为满足顶尖客户的需求,巴博斯只为梅赛德斯-奔驰公司的所有车型进行个性化生产。","劳斯莱斯,又称罗尔斯-罗伊斯,英国豪华汽车品牌,1906年成立于英国,公司创始人为Frederick Henry Royce和Charles Stewart Rolls。Rolls-Royce出产的轿车是顶级汽车的杰出代表,以其豪华而享誉全球,是欧美汽车的主要代表之一。"};
private int[] carIcons={R.drawable.a1,R.drawable.a2,R.drawable.a3,R.drawable.a4};
private List<Map<String, Object>> datas=new ArrayList<Map<String, Object>>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView aListView = (ListView) findViewById(R.id.aListView);
initDate();
SimpleAdapter adapter=new SimpleAdapter(this,datas,R.layout.list_item,new String[]{"icon","name","Introduction"},new int[]{R.id.carIcon,R.id.nameView,R.id.Introduction});
aListView.setAdapter(adapter);
}
private void initDate() {
for (int i=0;i<names.length;i++){
Map<String,Object>item=new HashMap<String, Object>();
item.put("name","车名:"+names[i]);
item.put("Introduction","简介:"+Introductions[i]);
item.put("icon",carIcons[i]);
datas.add(item);
}
}
}
运行结果: