本篇文章给大家分享的是有关ListView控件怎么在Android中使用,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
一、ListView的简单用法
首先新建一个ListViewTest项目,并让Android Studio自动创建好活动。然后修改activity_main.xml中的代码,如下:
<?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中的代码:
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适配器的适配类型。
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。
<?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,代码如下:
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中的代码:
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); } }}
以上就是ListView控件怎么在Android中使用,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注编程网行业资讯频道。