ListView的简单使用
使用ListView展示一些水果名称
首先设计布局:修改activity_main.xml中的代码,在布局中添加ListView控件将其id指定为list_view:
<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"
tools:context=".MainActivity">
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
接下来修改MainActivity中的代码,
public class MainActivity extends AppCompatActivity {
private String[] data = {"Apple", "Banana", "Orange", "Watermelon", "Pear", "Grape", "Pineapple", "Strawberry", "Cherry","Mango","Apple", "Banana", "Orange","Watermelon", "Pear", "Grape", "Pineapple", "Strawberry", "Cherry"};//水果名称数据
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter<String> adapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1,data);
ListView listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(adapter);
}
}
1.构建适配器对象
- 由于data数组中的数据是无法直接传递给ListView的,所以还需要借助适配器,这里使用的是ArrayAdapter(它可以根据泛型来指定要适配的数据)
- 然后在ArrayAdapter的构造函数中将要适配的数据传入即可。构造函数接收上下文、ListView子项布局id、要适配的数据。
ArrayAdapter<String> adapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1,data);
示例中子项布局id使用的是android.R.layout.simple_list_item_1,它是Android内置的布局文件,里面只有一个TextView,由于示例只想显示一段文本,所以使用此布局。
2.建立ListView和数据之间的关联
- 调用ListView的setAdapter() 方法,将构建好的适配器对象传递进去。
listView.setAdapter(adapter);
使用ListView展示水果图片和名称
一.首先定义数据实体类Fruit,name表示名称,imageId表示水果对应的图片:
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;
}
}
二.ListView布局
<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"
tools:context=".MainActivity">
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
三.定义ListView的子项布局用于显示水果图片和水果名称:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<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="10dp"
/>
</LinearLayout>
四.自定义适配器继承自ArrayAdapter,泛型肯定是fruit类:
public class FruitAdapter extends ArrayAdapter<Fruit> {
private int resourceId;
public FruitAdapter(Context context, int textViewResourceId, List<Fruit> objects) {
super(context, textViewResourceId, objects);
resourceId = textViewResourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {//在每个子项被滑动到屏幕时会调用
Fruit fruit = getItem(position);//获取当前项的Fruit实例
View view = view LayoutInflater.from(getContext()).inflate(resourceId, parent, false);//为当前子项加载布局
ImageView fruitImage = (ImageView)view.findViewById(R.id.fruit_image);
TextView fruitName = (TextView) view.findViewById(R.id.fruit_name);
fruitImage.setImageResource(fruit.getImageId());
fruitName.setText(fruit.getName());
return view;
}
}
1.FruitAdapter重写了父类的构造函数,传入参数与上文相同。
2.重写了getView() 方法,这个方法在每个子项被滚动到屏幕内的时候调用,在此方法中:
- 首先通过getItem() 方法获取当前项的Fruit实例
- 然后使用LayoutInflater来为这个子项加载子项布局
使用LayoutInflater的from()方法可以构建一个LayoutInflater对象,然后调用inflate() 方法动态加载布局文件,inflate()方法接受三个参数,第一个参数是要加载布局id,第二个参数是给加载好的布局再添加一个父布局,第三个指定为false
3.获取ImageView和TextView的实例,并分别调用setImageResource()和setText() 方法设置显示的图片和文字。
4.最后将设计好的布局返回,适配器就完成了。
五.在MainActivity中实现
public class MainActivity extends AppCompatActivity {
private List<Fruit> fruitList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initFruits();//初始化水果数据
FruitAdapter adapter = new FruitAdapter(MainActivity.this, R.layout.fruit_item, fruitList);
ListView listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(adapter);
}
private void initFruits() {
for (int i = 0; i < 2; i++) {
Fruit apple = new Fruit("Apple,", R.drawable.ic_launcher_background);
fruitList.add(apple);
Fruit banana = new Fruit("banana,", R.drawable.ic_launcher_background);
fruitList.add(banana);
Fruit Orange = new Fruit("Orange,", R.drawable.ic_launcher_background);
fruitList.add(Orange);
Fruit Watermelon = new Fruit("Watermelon,", R.drawable.ic_launcher_background);
fruitList.add(Watermelon);
Fruit Pear = new Fruit("Pear,", R.drawable.ic_launcher_background);
fruitList.add(Pear);
Fruit Grape = new Fruit("Grape,", R.drawable.ic_launcher_background);
fruitList.add(Grape);
Fruit Pineapple = new Fruit("Pineapple,", R.drawable.ic_launcher_background);
fruitList.add(Pineapple);
Fruit Strawberry = new Fruit("Strawberry,", R.drawable.ic_launcher_background);
fruitList.add(Strawberry);
Fruit Cherry = new Fruit("Cherry,", R.drawable.ic_launcher_background);
fruitList.add(apple);
}
}
}
此处实现与上文中简单实现用法相同。