ListView总结
ListView类作为在Android开发中经常会使用到的组件,作为新手,还是感到这一块变化形式还是很多的,需要慢慢学习。现在这里大概总结一下。
基于数组的ListView:使用android:entries属性可以指定列表项数组,这种方式最简洁方便,但内容只能是文本,可定制的内容少之又少。
使用ArrayAdapter创建ListView:也仅限于将数组或集合里的元素包装成列表项,比直接使用数组好在,可以指定列表项的列表项组件。例如:使用TextView作为列表项组件,不过也只能使用TextView。
例子:
1.列表项布局文件arraylist_item.xml:是的,没错,布局文件只包含一个TextView,这也是行的。
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/text1"/>
2.主界面布局文件activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.zjlyyq.test.MainActivity"> <ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listView" />
</LinearLayout>
3.MainActivity:
package com.example.zjlyyq.test; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView; public class MainActivity extends AppCompatActivity {
ListView listView;
String[] names = new String[]{"晃过天空","芝加哥公牛队","但使东山谢安石","颠三倒四","临时监护人"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView)findViewById(R.id.listView);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.arraylist_item,names);
listView.setAdapter(adapter);
}
}

使用SimpleAdapter创建ListView:SimpleAdapter可以满足更强的定制,每个列表项对应一个布局文件,将一个Map里的元素对应在布局文件里的各组件,例如:
1.列表项布局文件simpleadapter_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_weight="0.15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/header"/>
<LinearLayout
android:layout_weight="0.85"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/username"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/talk"/>
</LinearLayout>
</LinearLayout>
2.MainActivity:
package com.example.zjlyyq.test; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; public class MainActivity extends AppCompatActivity {
ListView listView;
String[] names = new String[]{"晃过天空","芝加哥公牛队","但使东山谢安石","颠三倒四","临时监护人"};
int[] headers = new int[]{R.drawable.plane,R.drawable.plane,R.drawable.plane,R.drawable.plane,R.drawable.plane};
String[] talks = new String[]{"晚上吃了吗?","吃了","你呢?","还没?","哦哦"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView)findViewById(R.id.listView);
List<Map<String,Object>> itemslist = new ArrayList<Map<String,Object>>();
for(int i = 0;i < 5;i ++){
Map<String,Object> evetyItem = new HashMap<String,Object>();
evetyItem.put("header",headers[i]);
evetyItem.put("name",names[i]);
evetyItem.put("talk",talks[i]);
itemslist.add(evetyItem);
}
//创建一个SimpleAdapter
SimpleAdapter simpleAdapter = new SimpleAdapter(this,itemslist,R.layout.simpleadapter_item,
new String[]{"header","name","talk"},
new int[]{R.id.header,R.id.username,R.id.talk}); listView.setAdapter(simpleAdapter);
}
}

ListView总结的更多相关文章
- 张高兴的 UWP 开发笔记:横向 ListView
ListView 默认的排列方向是纵向 ( Orientation="Vertical" ) ,但如果我们需要横向显示的 ListView 怎么办? Blend for Visua ...
- Android—万能ListView适配器
ListView是开发中最常用的控件了,但是总是会写重复的代码,浪费时间又没有意义. 最近参考一些资料,发现一个万能ListView适配器,代码量少,节省时间,总结一下分享给大家. 首先有一个自定义的 ...
- Android—ListView条目背景为图片时,条目间距问题解决
ListView是android开发中使用最普遍的控件了,可有的listView条目的内容颇为丰富,甚至为了美观,背景用指定图片,如下图:
- Android中ListView实现图文并列并且自定义分割线(完善仿微信APP)
昨天的(今天凌晨)的博文<Android中Fragment和ViewPager那点事儿>中,我们通过使用Fragment和ViewPager模仿实现了微信的布局框架.今天我们来通过使用Li ...
- listview下拉刷新和上拉加载更多的多种实现方案
listview经常结合下来刷新和上拉加载更多使用,本文总结了三种常用到的方案分别作出说明. 方案一:添加头布局和脚布局 android系统为listview提供了addfootview ...
- Android listview和gridview以及view的区别
GridView 可以指定显示的条目的列数. listview一般显示的条目的列数都是一列 如果是列表(单列多行形式)的使用ListView,如果是多行多列网状形式的优先使用GridView andr ...
- mono for android Listview 里面按钮 view Button click 注册方法 并且传值给其他Activity 主要是context
需求:为Listview的Item里面的按钮Button添加一个事件,单击按钮时通过事件传值并跳转到新的页面. 环境:mono 效果: 布局代码 主布局 <?xml version=" ...
- 【腾讯Bugly干货分享】跨平台 ListView 性能优化
本文来自于腾讯Bugly公众号(weixinBugly),未经作者同意,请勿转载,原文地址:https://mp.weixin.qq.com/s/FbiSLPxFdGqJ00WgpJ94yw 导语 精 ...
- android内部培训视频_第三节(3)_常用控件(ViewPager、日期时间相关、ListView)
第三节(2):常用控件之ViewPager.日期时间相关.ListView 一.ViewPager 实例:结合PagerAdapter滑动切换图片 二.日期时间相关:AnalogClock\Dig ...
- 父ListView嵌套子ListView时点击事件没有响应
转发请备注出处:http://www.cnblogs.com/LT5505/p/5972999.html 问题: 在ListView中嵌套ListView之后,子ListView会把父ListView ...
随机推荐
- HDU4329
#include<cstdio> #include<algorithm> #include<map> using namespace std; int main() ...
- word20161224
V.34 V.90 validation / 验证 value entry / 值项 variable / 变量 variable bit rate, VBR / 可变传输率 VBR, variabl ...
- linux下查看和添加PATH环境变量
linux下查看和添加PATH环境变量 $PATH:决定了shell将到哪些目录中寻找命令或程序,PATH的值是一系列目录,当您运行一个程序时,Linux在这些目录下进行搜寻编译链接. 编辑你的 PA ...
- python import, from xx import yy
区别: 用import modulexx/packagexx.moduleyy是导入某一模块,如果想引用模块的内容(class, method,variables...)必须用全名,即 [module ...
- 48. 二叉树两结点的最低共同父结点(3种变种情况)[Get lowest common ancestor of binary tree]
[题目] 输入二叉树中的两个结点,输出这两个结点在数中最低的共同父结点. 二叉树的结点定义如下: C++ Code 123456 struct BinaryTreeNode { int ...
- 设计模式--桥接模式Bridge(结构型)
一.概述 在软件系统中,某些类型由于自身的逻辑,它具有两个或者多个维度的变化,如何应对这种"多维度的变化",就可以利用桥接模式. 引例: 设想如果要绘制矩形.圆形.椭圆.正方形,我 ...
- SemanticZoom配合GridView组件的使用关键点
1,SemanticZoom 有两个重要属性 默认值ZoomedInView(不设置的话,默认显示,包括分类名和分类成员)和ZoomedOutView(这个是缩小后的目录,只要包括分类名,点击跳到对应 ...
- 【jQuery】--图片轮播
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 顺序查找SequentialSearch
#include <stdio.h>int SequentialSearch(int *a,int n,int x);int main(void){ //num代表查找的数 int num ...
- MFC 创建多层目录
创建多层目录 BOOL CTestToolCtr::CreateFolder(CString strNewFolder) { /************************************ ...