Android控件ListView使用方法详解

2020-10-22 11:23:21 浏览数 (1)

Android控件ListView使用方法介绍,具体如下

一、ListView的简单用法

首先新建一个ListViewTest项目,并让Android Studio自动创建好活动。然后修改activity_main.xml中的代码,如下:

代码语言:javascript复制
<?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" 

 <ListView
  android:id="@ id/list_view"
  android:layout_width="match_parent"
  android:layout_height="match_parent" 
 </ListView 
</LinearLayout 

接下来修改MainActivity中的代码:

代码语言:javascript复制
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","Mango"};
 @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=(ListView)findViewById(R.id.list_view);
  listview.setAdapter(adapter);
 }
}

数组中的数据无法直接传递给ListView,需要借助适配器来实现。

ArrayAdapter的构造函数中依次传入当前上下文,ListView子项布局的id,以及要适配的数据;

调用ListView的setAdapter()方法,将构建好的适配器对象传递进去,这样ListView和数据之间的关联就建立完成。

运行程序,可以通过滚动的方式来查看屏幕外的数据。

二、定制ListView的界面

首先准备一组图片,分别对应上面提供的每一种水果(注意图片大小尽量一致),放在drawable目录下,注意命名不能出现大写字母(比如Apple不合法);

在com.example.administrator.listviewtest下新建.java文件,定义一个实体类Fruit,作为ListView适配器的适配类型。

代码语言:javascript复制
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的子项指定一个自定义布局,在layout目录下新建fruit_item.xml。

代码语言:javascript复制
<?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="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 

接下来创建一个自定义的适配器,同样在com.example.administrator.listviewtest下新建.java文件,重写了父类的一组构造函数,用于将上下文、ListView子项布局的id和数据都传递进来,命名为FruitAdapter,代码如下:

代码语言:javascript复制
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);   //获取当前项的实例
  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;
 }
}

下面修改MainActivity中的代码:

代码语言:javascript复制
package com.example.administrator.listviewtest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
 private List<Fruit  fruitList=new ArrayList< ();

// private String[] data={"Apple","Banana","Orange","Watermelon","Pear","Grape","Pineapple","Strawberry","Cherry","Mango","Apple","Banana","Orange","Watermelon","Pear","Grape","Pineapple","Strawberry","Cherry","Mango"};
 @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);

//  ArrayAdapter<String  adapter = new ArrayAdapter<String (MainActivity.this, android.R.layout.simple_list_item_1, data);
  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.apple);
   fruitList.add(apple);
   Fruit orange=new Fruit("Orange",R.drawable.orange);
   fruitList.add(orange);
   Fruit banana=new Fruit("Banana",R.drawable.banana);
   fruitList.add(banana);
   Fruit waterlenmo=new Fruit("Waterlemon",R.drawable.waterlemon);
   fruitList.add(waterlenmo);
   Fruit pear=new Fruit("Pear",R.drawable.pear);
   fruitList.add(pear);
   Fruit grape=new Fruit("Grape",R.drawable.grape);
   fruitList.add(grape);
   Fruit pineapple=new Fruit("Pineapple",R.drawable.pineapple);
   fruitList.add(pineapple);
   Fruit strawberry=new Fruit("Strawberry",R.drawable.straw);
   fruitList.add(strawberry);
   Fruit cherry=new Fruit("Cherry",R.drawable.cherry);
   fruitList.add(cherry);
   Fruit mango=new Fruit("mango",R.drawable.mango);
   fruitList.add(mango);
  }
 }
}

运行程序后,效果图如下(图片大小后来才意识到,懒得换了……):

以上就是本文的全部内容,希望对大家的学习有所帮助。

0 人点赞