ListView主要包括view和数据源。其数据适配器列表分为三种,ArrayAdapter,SimpleAdapter和SimpleCursorAdapter。

ListView的没有oom原因。经典图:

1.democoderjoy中使用,这里我们新建一个ListViewActi的activity。布局文件listview比较简单

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ListView01"
android:layout_weight="1"
/>
<ListView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/list_view"
android:layout_weight="1"
android:stackFromBottom="true"
android:background="@drawable/icon"
android:scrollbars="none" />
</LinearLayout>

2.此外还需要一个item项

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
d
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:layout_alignParentRight="true"
/> <TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:textSize="20dp"
android:text="New Text"
android:id="@+id/textView01" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textView02"
android:layout_below="@+id/textView01"
/> </RelativeLayout>

3.arrayAdapter的使用采用布局的第二个listview id

核心代码:

ArrayAdapter<String> adapter=new ArrayAdapter<String>(
this,R.layout.simple_list_item_1,
mData);
ListView listView=(ListView) findViewById(R.id.list_view);
listView.setAdapter(adapter);

4.simpleAdater使用

ListView list = (ListView) findViewById(R.id.ListView01);
ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();
for(int i=0;i<10;i++)
{
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("ItemImage", R.drawable.icon);//图像资源的ID
map.put("ItemTitle", "Level "+i);
map.put("ItemText", "Finished in 1 Min 54 Secs, 70 Moves! ");
listItem.add(map);
}
SimpleAdapter listItemAdapter = new SimpleAdapter(this,listItem,//数据源
R.layout.listview_item,//ListItem的XML实现
//动态数组与ImageItem对应的子项
new String[] {"ItemImage","ItemTitle", "ItemText"},
//ImageItem的XML文件里面的一个ImageView,两个TextView ID
new int[] {R.id.imageView,R.id.textView01,R.id.textView02}
); View view = LayoutInflater.from(this).inflate(R.layout.head_view_layout, null);
list.addHeaderView(view, null, true);
list.setOverscrollHeader(getResources().getDrawable(R.drawable.icon));
list.addFooterView(view);
list.setHeaderDividersEnabled(true);
list.setFooterDividersEnabled(true);
list.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
list.setAdapter(listItemAdapter); //list.setOverScrollMode(View.OVER_SCROLL_NEVER);
//view.setVisibility(View.GONE);
//添加点击
list.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
setTitle("点击第"+arg2+"个项目");
Toast.makeText(getApplicationContext(),"点击第"+arg2+"个项目",Toast.LENGTH_SHORT).show();
}
});
SimpleCursorAdapter d;
//添加长按点击 响应餐单
list.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() { @Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
menu.setHeaderTitle("长按菜单-ContextMenu");
menu.add(0, 0, 0, "弹出长按菜单0");
menu.add(0, 1, 0, "弹出长按菜单1");
}
});

5.效果如下:

Tips:

a.  footerDividersEnabled:是否在footerView(表尾)前绘制一个分隔条,默认为true

     headerDividersEnabled:是否在headerView(表头)前绘制一个分隔条,默认为true

b. listview可以在布局中设置背景

c. listview具有headview 和footview 如程序中所使用的,

list.addHeaderView(view, null, true);
list.setOverscrollHeader(getResources().getDrawable(R.drawable.icon));
list.addFooterView(view);

使用的子view的布局如下:

<?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"
android:orientation="horizontal" > <TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="head foot list"/>
<CheckBox
android:id="@+id/group_selection_all"
android:layout_width="40dip"
android:layout_height="40dip"
android:layout_gravity="center_vertical"
android:layout_marginLeft="8dip"
android:layout_marginRight="0dip"
android:focusable="false"
android:clickable="true"
android:gravity="center"
android:scaleType="centerInside"/>
</LinearLayout>

d.设置从底向上排列参数 布局中定义stackFromBottom

android-基础编程-ListView的更多相关文章

  1. 【Android基础】listview控件的使用(4)-----自定义布局的listview的使用

    前面我介绍了listview控件的不同用法,但是这些用法在实际的开发项目中是不足以满足需求的,因为前面的几种用法只能简单的显示文本信息,而且布局都比较单一,很难做出复杂的结果,在实际的开发项目中,90 ...

  2. 【Android基础】listview控件的使用(2)-------继承自ListActivity的普通listview

    由于listview在android控件中的重要性,所以android为我们直接封装了一个类ListviewActivity,直接将listview封装在了activity之中,在本篇中,我将介绍在L ...

  3. 【Android基础】listview控件的使用(1)------最简单的listview的使用

    listview控件是项目开发中最常用的空间之一,我将慢慢推出关于listview的一系列的文章,先从最简单的,系统自带的listview开始吧! 先上效果图: activity_one.xml &l ...

  4. Android基础TOP7_1:ListView制作列表

    结构: Activity: activity_main: <RelativeLayout xmlns:android="http://schemas.android.com/apk/r ...

  5. 【Android基础】listview控件的使用(3)------Map与SimpleAdapter组成的多显示条目的Listview

    前面介绍的两种listview的使用都是最基础的,所以有很大的局限性,比如只能在一个item(即每一行的条目)中显示一个文本信息,这一篇我将介绍Map与SimpleAdapter组成的多显示条目的Li ...

  6. Android基础控件ListView基础操作

    1.简介 基于Android基础控件ListView和自定义BaseAdapter适配器情况下,对ListView的数据删除和添加操作: public boolean add(E e) {//添加数据 ...

  7. android: 多线程编程基础

    9.1   服务是什么 服务(Service)是 Android 中实现程序后台运行的解决方案,它非常适合用于去执行那 些不需要和用户交互而且还要求长期运行的任务.服务的运行不依赖于任何用户界面,即使 ...

  8. Android网络编程基础

    Android网络编程只TCP通信 TCP 服务器端工作的主要步骤如下.步骤1 调用ServerSocket(int port)创建一个ServerSocket,并绑定到指定端口上.步骤2 调用acc ...

  9. <Android基础>(三) UI开发 Part 2 ListView

    ListView 1)ListView的简单用法 2)定制ListView界面 3)提升ListView的运行效率 4)ListView的点击事件 3.5 ListView 3.5.1 ListVie ...

  10. 安卓Android基础第三天——数据库,ListView

    数据库介绍sqlite问:什么情况下使用数据库?答:有大量相似结构的数据需要存储的时候 数据库的创建定义一个类继承SqliteOpenHelpercontext:上下文name:数据库名字,如&quo ...

随机推荐

  1. vue(ajax:axios中文文档)

    axios 基于http客户端的promise,面向浏览器和nodejs 特色 浏览器端发起XMLHttpRequests请求 node端发起http请求 支持Promise API 监听请求和返回 ...

  2. python sockerserver tcp 文件下载 udp

    #tcp serverclass MyHandler(socketserver.BaseRequestHandler): def handle(self): # 通信循环 while True: tr ...

  3. socket 进阶

    1.验证客户端链接的合法性 如果你想在分布式系统中实现一个简单的客户端链接认证功能,又不像SSL那么复杂,那么利用hmac+加盐的方式来实现 import socket import os impor ...

  4. Liunx Pwd

    Linux中用 pwd 命令来查看”当前工作目录“的完整路径. 简单得说,每当你在终端进行操作时,你都会有一个当前工作目录. 在不太确定当前位置时,就会使用pwd来判定当前目录在文件系统内的确切位置. ...

  5. ES6 Reflect的认识

    首先我们要了解一下,为什么会新添加这么一个全局对象?如果你看过Reflect的一些函数,你就会发现,这个对象上的方法基本上都可以从Object上面找到,找不到的那些,也是可以通过对对象命令式的操作去实 ...

  6. image src base64 svg

    1.显示img: 大家可能注意到了,网页上有些图片的src或css背景图片的url后面跟了一大串字符,比如:data:image/png;base64, iVBORw0KGgoAAAANSUhEUgA ...

  7. unbutton 内部title label多行显示

    dateBtn1.titleLabel.lineBreakMode = NSLineBreakByWordWrapping; [dateBtn1 setTitle: @第一行\n第二行" f ...

  8. Python之路(第三篇):Python基本数据类型字符串(二)

    一.基本数据类型1.字符串 str字符串方法介绍(二)a --expandtabs( ) expandtabs( ) 把字符串中的 tab 符号('\t')转为空格参数默认为8,注意字符串原有的空格也 ...

  9. C++11与Unicode及使用标准库进行UTF-8、UTF-16、UCS2、UCS4/UTF-32编码转换

    zt https://blog.poxiao.me/p/unicode-character-encoding-conversion-in-cpp11/ Unicode Unicode是计算机领域的一项 ...

  10. Maximum Subarray LT53

    Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...