注:ListView只能实现纵向滚动
- ListView简单用法就是:
- 数据通过适配器Adapter对象(构造函数第3个参数)(即数据放进Adapter对象)
- 然后使用ListView.setAdapter(apdapter)将适配器对象传给ListView对象
/*xxxAdapter构造函数,一般用ArrayAdapter多
Adapter(Context上下文,listView子项的布局文件id,数据(数组之类))
*/
- 所谓定制LIstView界面就是自定义Adapter构造函数中,我们传入的子项布局文件,只要修改子项布局文件就可以有多样的ListView
①定义一个实体类用于适配器的适配类型(即泛型中<>指定的类),如ArrayAdapter<实体类>
public class Fruit {
String name;
int imageId;
public Fruit(String name,int imageId){
this.imageId=imageId;
this.name=name;
}
public String getName() {
return name;
}
public int getImageId() {
return imageId;
}
}
②为ListView的子项定义布局(即Adapter构造函数传入的布局),这里不同,则ListView就不同
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="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/fruit_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/fruit_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft=