简单使用
1.在界面编写ListView
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
2.实例化一个ArrayAdapter对象,填入Context上下文,每一项的布局文件,数据这三个参数
private final String[] data = {
"Apple", "Banana", "Orange", "Pear", "Mango", "Grape",
"watermelon", "Cherry", "Pineapple", "Strawberry",
"Apple", "Banana", "Orange", "Pear", "Mango", "Grape",
"watermelon", "Cherry", "Pineapple", "Strawberry"
};
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,data);
3.ListView填装适配器
ListView listView = findViewById(R.id.listView);
listView.setAdapter(adapter);
丰富你的ListView
1.只显示一段文本实在是太单调了,我们现在对ListView的界面进行定制,先创建一个实体类,作为适配器的适配类型
public class Fruit {
private String name;
private int imageId;
public Fruit(String name, int imageId) {
this.name = name;
this.imageId = imageId;
}
public String getName() {
return name;
}
public int getImageId() {
return imageId;
}
}
2.然后定义ListView子项的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/fruit_image"
android:layout_width="45dp"
android:layout_height="45dp" />
<TextView
android:id="@+id/fruit_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_gravity="center_vertical"/>
</LinearLayout>
3.接下来创建一个自定义的适配器,这个适配器经过优化处理了。
public class FruitAdapter extends ArrayAdapter<Fruit> {
private int resourceId;
public FruitAdapter(@NonNull Context context, int resource, @NonNull List<Fruit> objects) {
super(context, resource, objects);
resourceId = resource;
}
// getView()会在每个子项滚动到屏幕内的时候调用
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
// 通过getItem()得到当前项的Fruit实例
Fruit fruit = getItem(position);
View view;
// ViewHolder 是用来储存ListView子项的各种控件
ViewHolder viewHolder;
// 提升ListView的运行效率
if (convertView == null) {
// 使用LayoutInflater,来为这个子项加载我们传入的布局
view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);
viewHolder = new ViewHolder();
viewHolder.fruitImage = view.findViewById(R.id.fruit_image);
viewHolder.fruitName = view.findViewById(R.id.fruit_name);
view.setTag(viewHolder);
} else {
view = convertView;
viewHolder = (ViewHolder) view.getTag();
}
viewHolder.fruitImage.setImageResource(fruit.getImageId());
viewHolder.fruitName.setText(fruit.getName());
return view;
}
}
4.最后按照简单使用的三步骤
private List<Fruit> fruitList = new ArrayList<>();
private void onCreate(Bundle savedInstanceState) {
...
// 初始化数据,完成数据的填充
for (int i = 0; i < data.length; i++) {
Fruit fruit = new Fruit(data[i], R.mipmap.ic_launcher);
fruitList.add(fruit);
}
ListView listView = findViewById(R.id.listView);
FruitAdapter adapter = new FruitAdapter(this, R.layout.fruit_item, fruitList);
listView.setAdapter(adapter);
}
ListView点击事件
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Fruit fruit = fruitList.get(i);
Toast.makeText(MainActivity.this, fruit.getName(), Toast.LENGTH_LONG).show();
}
});