欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android控件ListView使用方法详解

程序员文章站 2022-05-02 21:13:35
android控件listview使用方法介绍,具体如下 一、listview的简单用法 首先新建一个listviewtest项目,并让android studio自动...

android控件listview使用方法介绍,具体如下

一、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和数据之间的关联就建立完成。

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

Android控件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);
  }
 }
}

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

Android控件ListView使用方法详解

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