ListView 学习
- ListView 允许用户通过手指上下滑动的方式将屏幕外的数据滚动到屏幕内,同时屏幕上原有的数据会滚动出屏幕。
一个最简单的ListView
布局<ListView android:id="@+id/list_view" android:layout_width="match_parent" android:layout_height="match_parent">
MainActivity中代码
//定义一个数组用于滑动显示
private String[] data = {"apple", "banana","orange","watermenlon","pear","grape","pineapple","strawberry","cherry","mango","apple", "banana","orange","watermenlon","pear","grape","pineapple","strawberry","cherry","mango"};
...
//定义一个适配器,通过泛型来指定要适配的数据类型然后构造函数中把要适配的数据传入。我们用了android.R.Layout.simple.list_item_1作为ListView子项的布局id,这是一个android内置的布局文件,里面只有一个TextView,可用于简单地显示一段文本。
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1,data);
ListView listView = findViewById(R.id.list_view);
listView.setAdapter(adapter);
- 定制ListView的界面实例
定义一个实体类
public class Fruit {
private String name;
private int imageId;
public Fruit(String name, int imageId){
this.name = name;
this.imageId = imageId;
}
public int getImageId() {
return imageId;
}
public String getName() {
return name;
}
}
自定义fuit_item.xml布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/furit_image"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/fruit_name"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"/>
</LinearLayout>
自定义适配器,这个适配器继承自ArrayAdapter,并将泛型指定为Fruit类
public class FruitAdapter extends ArrayAdapter<Fruit> {
private int resourceId;
//重写了父类的构造函数,用于将上下文,ListView子项布局的id和数据都传递进来。
public FruitAdapter(@NonNull Context context, int textViewResouceId, List<Fruit> objects) {
super(context, textViewResouceId,objects);
resourceId = textViewResouceId;
}
@NonNull
@Override
//重写getview方法,这个方法在每个子项被滚动到屏幕内的时候会被调用
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
Fruit fruit = getItem(position);//获取当前项的Fruit实例
//为这个子项加载我们传入的布局
View view = LayoutInflater.from(getContext()).inflate(resourceId,parent,false);
ImageView fruitImage = view.findViewById(R.id.furit_image);
TextView fruitName = view.findViewById(R.id.fruit_name);
fruitImage.setImageResource(fruit.getImageId());
fruitName.setText(fruit.getName());
return view;
}
}
MainActivity中代码
public class MainActivity extends AppCompatActivity {
//private String[] data = {"apple", "banana","orange","watermenlon","pear","grape","pineapple","strawberry","cherry","mango","apple", "banana","orange","watermenlon","pear","grape","pineapple","strawberry","cherry","mango"};
private List<Fruit> fruitList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1,data);
// ListView listView = findViewById(R.id.list_view);
// listView.setAdapter(adapter);
initFruit();
FruitAdapter adapter = new FruitAdapter(MainActivity.this,R.layout.fruit_item,fruitList);
ListView listView = findViewById(R.id.list_view);
listView.setAdapter(adapter);
}
private void initFruit(){
for(int i = 0; i< 2; i++){
Fruit apple = new Fruit("apple", R.drawable.apple_pic);
fruitList.add(apple);
Fruit banana = new Fruit("banana",R.drawable.banana_pic);
fruitList.add(banana);
Fruit orange = new Fruit("orange",R.drawable.orange_pic);
fruitList.add(orange);
Fruit watermelon = new Fruit("watermelon",R.drawable.watermelon_pic);
fruitList.add(watermelon);
Fruit pear = new Fruit("pear",R.drawable.pear_pic);
fruitList.add(pear);
Fruit grape = new Fruit("grape",R.drawable.grape_pic);
fruitList.add(grape);
Fruit pineapple = new Fruit("pineapple",R.drawable.pineapple_pic);
fruitList.add(pineapple);
Fruit strawberry = new Fruit("strawberry",R.drawable.strawberry_pic);
fruitList.add(strawberry);
Fruit cherry = new Fruit("cherry",R.drawable.cherry_pic);
fruitList.add(cherry);
Fruit mango = new Fruit("mango",R.drawable.mango_pic);
fruitList.add(mango);
}
}
}