一、ListView的基本概念
在Android所有常用的原生控件当中,用法最复杂的应该就是ListView了,它专门用于处理那种内容元素很多,手机屏幕无法展示出所有内容的情况。ListView可以使用列表的形式来展示内容,超出屏幕部分的内容只需要通过手指滑动就可以移动到屏幕内了。即使在ListView中加载非常非常多的数据,比如达到成百上千条甚至更多,ListView都不会发生崩溃,而且随着我们手指滑动来浏览更多数据时,程序所占用的内存竟然都不会跟着增长。
二、示例代码
定义一个实体类,作为ListView适配器的适配类型,代码如下所示:
package com.nyl.listviewtest; public class SkinCare {
private String name;
private String efficacy; //功效
private int icon; //图标 public SkinCare(String name, String efficacy, int icon) {
this.name = name;
this.efficacy = efficacy;
this.icon = icon;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getEfficacy() {
return efficacy;
} public void setEfficacy(String efficacy) {
this.efficacy = efficacy;
} public int getIcon() {
return icon;
} public void setIcon(int icon) {
this.icon = icon;
}
}
自定义一个ListView的子项布局,代码如下所示:
<?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="120dp"
android:orientation="horizontal"> <ImageView
android:id="@+id/ivSkinCare"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:orientation="vertical" > <TextView
android:id="@+id/tvName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autoLink="all"
android:textSize="15dp"/>
<TextView
android:id="@+id/tvEfficacy"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout>
创建一个自定义的适配器,这个适配器继承自ArrayAdapter,并将泛型指定为SkinCare这个实体类,新建SkinCareAdapter类,代码如下所示:
package com.nyl.listviewtest; import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView; import java.util.List; /**
* Created by Administrator on 2017/3/9 0009.
*/ public class SkinCareAdapter extends ArrayAdapter<SkinCare> { private int resourceId; public SkinCareAdapter(Context context, int resource, List<SkinCare> objects) {
super(context, resource, objects);
resourceId = resource;
} /**
* 这个方法在每个子项被滚动到屏幕内的时候调用
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//通过getItem()方法得到当前SkinCare的实例
SkinCare skinCare = getItem(position); /**
* 提高ListView的运行效率,在快速滚动的时候也可以表现出更好的性能
*/ //使用ViewHolder来优化
ViewHolder viewHolder; //判断convertView 为空
if (convertView == null) {
//如果为空使用LayoutInflater去加载布局
convertView = LayoutInflater.from(getContext()).inflate(resourceId, null); viewHolder = new ViewHolder();
viewHolder.ivSkinCare = (ImageView) convertView.findViewById(R.id.ivSkinCare);
viewHolder.tvName = (TextView) convertView.findViewById(R.id.tvName);
viewHolder.tvEfficacy = (TextView) convertView.findViewById(R.id.tvEfficacy);
//将ViewHolder存储在View中
convertView.setTag(viewHolder);
} else {
//如果不为空直接对convertView进行重用
//重新获取ViewHolder
viewHolder = (ViewHolder) convertView.getTag();
} /* //使用LayoutInflater来加载传入的布局(这种写法运行效率是很低)
View view = LayoutInflater.from(getContext()).inflate(resourceId,null);
*//**
* 调用View的findViewById()方法来获取布局控件
*//*
ivSkinCare = (ImageView) view.findViewById(R.id.ivSkinCare);
tvName = (TextView) view.findViewById(R.id.tvName);
tvEfficacy = (TextView) view.findViewById(R.id.tvEfficacy); //调用setImageResource()方法来设置显示的图片
ivSkinCare.setImageResource(skinCare.getIcon());
//设置护肤品的名字
tvName.setText(skinCare.getName());
//设置护肤品的功效
tvEfficacy.setText(skinCare.getEfficacy());*/ viewHolder.ivSkinCare.setImageResource(skinCare.getIcon());
viewHolder.tvName.setText(skinCare.getName());
viewHolder.tvEfficacy.setText(skinCare.getEfficacy());
//将布局返回
return convertView;
}
static class ViewHolder {
ImageView ivSkinCare;
TextView tvEfficacy;//护肤品的功效
TextView tvName; //护肤品名
} }
代码注释得很详细,不过多解释,接下编写MainActivity,代码如下所示:
package com.nyl.listviewtest; import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast; import java.util.ArrayList;
import java.util.List; public class MainActivity extends Activity implements AdapterView.OnItemClickListener { private List<SkinCare> skinCare = new ArrayList<>();
private SkinCareAdapter skinCareAdapter;
private ListView lv; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initSkinCare(); //初始化所有的护肤品数据
skinCareAdapter = new SkinCareAdapter(MainActivity.this,R.layout.skin_care,skinCare); //初始化布局控件
lv = (ListView) findViewById(R.id.lv);
lv.setAdapter(skinCareAdapter); //设置setOnItemClickListener()方法来为ListView注册一个监听器
lv.setOnItemClickListener(this); } /**
* 当用户点击了ListView中任何一个子项时,就会回调onItemClick()方法,在这个方法中可以通过i参数判断出用户点击的哪一个
* 子项,然后获取到相应的护肤品,通过Toast将护肤品的名字显示出来。
*/
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
SkinCare sc = skinCare.get(i);
Toast.makeText(MainActivity.this,sc.getName(),Toast.LENGTH_SHORT).show();
} /**
* 构造函数中将护肤品的名字,功效和对应的图片id传入,然后把创建好的对象添加到护肤品列表中
*/
private void initSkinCare() {
SkinCare cleansingMilk = new SkinCare("洗面奶","基础清洁",R.mipmap.skin_care);
skinCare.add(cleansingMilk); SkinCare toner = new SkinCare("爽肤水","二次清洁,深层补水",R.mipmap.skin_care);
skinCare.add(toner); SkinCare essence = new SkinCare("精华液","集中解决肌肤某个问题,由内而外修复肌肤",R.mipmap.skin_care);
skinCare.add(essence); SkinCare eyeCream = new SkinCare("眼霜","舒缓眼部疲劳,改善眼部肤色",R.mipmap.skin_care);
skinCare.add(eyeCream); SkinCare latex = new SkinCare("乳液","给皮肤补充充足的营养,激活肌肤细胞,形成胶原蛋白",R.mipmap.skin_care);
skinCare.add(latex ); SkinCare faceCream = new SkinCare("面霜","不断给肌肤补充充足的水分和养分,使肌肤保持润泽状态",R.mipmap.skin_care);
skinCare.add(faceCream ); SkinCare mask = new SkinCare("面膜","清透毛孔,清楚表皮污垢,黑头,死细胞",R.mipmap.skin_care);
skinCare.add(mask ); SkinCare sunScreen = new SkinCare("隔离霜","全面隔离紫外线、灰尘、尼古丁、电脑辐射等有害物质对皮肤造成伤害",R.mipmap.skin_care);
skinCare.add(sunScreen ); /**
* 第二种写法
*/
/*LinkedList<SkinCare> data = new LinkedList<>();
data.add(new SkinCare("洗面奶","基础清洁",R.mipmap.skin_care));*/
} }
运行程序,并点击一下精华液,效果如下:
简单的ListView知识就学到这里,更多的知识点下节继续!