使用方法1

显示简单的文本

  1. 在layout文件中像加入普通控件一样在layout文件中引入ListView

    <ListView
    android:id="@+id/list_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    </ListView>
  2. 数组中的数据要通过适配器来导入ListView,Android有很多的适配器,常用的有ArrayAdapter,它通过泛型指定要适配的数据类型,然后在构造函数中把要适配的数据传入即可。

//data is a String Array
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity_simplelist.this, android.R.layout.simple_list_item_1, data);
ListView listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(adapter);

使用方法2

显示定制的界面

  1. 定义子项的构成

    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;
    }
    }
  2. 然后需要为子项写好布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" > <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"
android:layout_marginLeft="10dip" /> </LinearLayout>

3, 接下来还要自定义适配器

3.1 自定义适配器要写构造函数,有三个参数分别是上下文,ListView子项布局的id和数据

 public FruitAdapter(Context context, int textViewResourceId,
List<Fruit> objects) {
super(context, textViewResourceId, objects);
resourceId = textViewResourceId;
}

3.2 还需要重写getView方法,这个方法在滚动屏幕的时候被调用

  3.2.1 在getView方法中首先调用getItem取得当前子项的实例

Fruit fruit = getItem(position);

  3.2.2 然后调用LayoutInflater来为这个子项加载传入的布局

view = LayoutInflater.from(getContext()).inflate(resourceId, null);

  3.2.3 接着调用findViewById找到布局中的各个控件的id,然后调用它们的set函数设置图片或文字

fruitImage = (ImageView) view.findViewById(R.id.fruit_image);
fruitName = (TextView) view.findViewById(R.id.fruit_name);
fruitImage.setImageResource(fruit.getImageId());
fruitName.setText(fruit.getName());

如何提升ListView的效率

方法1: 利用缓存的布局 getView()有一个convertView参数,这个参数用于将之前加载好的布局进行缓存,当这个参数不为null时,直接使用而不是重新加载布局。

if (convertView == null) {
view = LayoutInflater.from(getContext()).inflate(resourceId, null);
} else {
view = convertView;
}

方法2:对控件实例进行缓存 当加载布局时,将控件保存到ViewHolder中;当不需要重新加载布局时,直接从ViewHolder中取出。

if (convertView == null) {
view = LayoutInflater.from(getContext()).inflate(resourceId, null);
viewHolder = new ViewHolder();
viewHolder.fruitImage = (ImageView) view.findViewById(R.id.fruit_image);
viewHolder.fruitName = (TextView) view.findViewById(R.id.fruit_name);
view.setTag(viewHolder);
} else {
view = convertView;
viewHolder = (ViewHolder) view.getTag();
}
 

Android中ListView的用法的更多相关文章

  1. Android中Listview点击item不变颜色以及设置listselector 无效

    Android中Listview点击item不变颜色以及设置listselector 无效 这是同一个问题,Listview中点击item是会变颜色的,因为listview设置了默认的listsele ...

  2. Android中ListView控件的使用

    Android中ListView控件的使用 ListView展示数据的原理 在Android中,其实ListView就相当于web中的jsp,Adapter是适配器,它就相当于web中的Servlet ...

  3. android中ListView控件&&onItemClick事件中获取listView传递的数据

    http://blog.csdn.net/aben_2005/article/details/6592205 本文转载自:android中ListView控件&&onItemClick ...

  4. android中ListView点击和里边按钮点击不能同时生效问题解决

    今天遇到一个问题:android中ListView点击和里边button点击不能同时生效问题解决. 原因是: listView 在开始绘制的时候,系统首先调用getCount()函数,根据他的返回值得 ...

  5. android 中uri.parse()用法

    android 中uri.parse()用法 1,调web浏览器 Uri myBlogUri = Uri.parse("http://xxxxx.com"); returnIt = ...

  6. Android中ListView无法点击

    Android中ListView无法点击 转自:http://xqjay19910131-yahoo-cn.iteye.com/blog/1319502   问题描述: ListView中Item加入 ...

  7. Android中Selector的用法(改变ListView和Button的默认背景)

    Android中的Selector的用法 http://blog.csdn.net/shakespeare001/article/details/7788400#comments Android中的S ...

  8. Android中 ListView 详解(二)

    本文版权归 csdn noTice501 所有,转载请详细标明原作者及出处,以示尊重! 作者:noTice501 原文:http://blog.csdn.net/notice520/article/d ...

  9. Android中ListView实现图文并列并且自定义分割线(完善仿微信APP)

    昨天的(今天凌晨)的博文<Android中Fragment和ViewPager那点事儿>中,我们通过使用Fragment和ViewPager模仿实现了微信的布局框架.今天我们来通过使用Li ...

随机推荐

  1. 数组栈的C语言实现

    #ifndef _CONST_H_#define _CONST_H_ #include <stdio.h>#include <stdlib.h> typedef enum { ...

  2. 月薪3万的程序员告诉你:这样工作才能拿高薪(转 IT之家)

    习惯即刻回报 他不懂得只有春天播种,秋天才会有收获.刚刚付出一点点,甚至还没有付出,就想要得到回报.技术刚刚掌握,能一边百度一边干活了就觉得该拿到多少多少钱了.找工作先想着多少多少钱,入职了没干几个月 ...

  3. java简单计算器

    写的一个小计算器,留着以后看吧. import java.awt.BorderLayout; import java.awt.Button; import java.awt.Color; import ...

  4. dubbo & zookeeper & springMVC

    一.安装准备 1.环境简介 名称 版本 OS Mac OS X 10.11.5 JDK 1.8.0_51 dubbo 2.5.4 tomcat 8.0.35 Zookeeper 3.4.8 maven ...

  5. 用FireFox火狐浏览器的3D Tilt 插件查看网页3D视图效果

    逛博客发现了网页的3D视图效果,一搜原来是Firefox特有的一个功能,先看效果: 相当炫酷,接下来介绍如何实现. 1.首先安装3d tilt 插件: 从火狐浏览器的添加插件页面,搜索:3D Tilt ...

  6. Linux 内核编译

    注:该文章部分内容摘录自以下链接. http://www.cnblogs.com/zhunian/archive/2012/04/04/2431883.html 创建内核的第一步是创建一个 .conf ...

  7. DIV未知宽度高度垂直水平居中

    使用过一种算是相对来说比较好理解一些的吧算是,代码如下: <style> width:800px; height:400px; margin:0 auto; position:absolu ...

  8. 软件工程线上课程(C语言实践篇)学习心得总结

    林牧 + 原创作品转载请注明出处 + <软件工程(C编码实践篇)>MOOC课程http://mooc.study.163.com/course/USTC-1000002006 软件工程的理 ...

  9. (转)Sqoop中文手册

    Sqoop中文手册 1.     概述 本文档主要对SQOOP的使用进行了说明,参考内容主要来自于Cloudera SQOOP的官方文档.为了用中文更清楚明白地描述各参数的使用含义,本文档几乎所有参数 ...

  10. Fibers in JVM

    转载: Fibers in JVM