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. .net项目错误:找不到方法:“System.Net.Http.HttpClient stellar_dotnet_sdk.Server.get_HttpClient()

    1.由于在项目里面引用了一个 新的项目stellar_dotnet_sdk,在  return new StellarWallet(ConvertToWalletSetting(coin));   的 ...

  2. Sqlite文件在ubunut的查看

    1. How to list the tables in a SQLite database file that was opened with ATTACH? The .tables, and .s ...

  3. python 数据类型 之 字典

    python 3.6.5字典的特性和定义定义:{'key_1':vlaue_1,'key_2':value_2}1.键与值用冒号 : 分开2.项与项 用 , 分开 特性1.可以存放多个值,可以不唯一, ...

  4. linux 文件搜索

    locate  文件名 在后台数据库中按文件名搜索,搜索速度快,不用遍历整个操作系统 /var/lib/mlocate locate 命令所搜索的后台数据库 updatedb 手动更新数据库 新建的文 ...

  5. oracle 直接复制表内容到新表

    不知道为什么,刚建的oracle数据库删除数据很慢,表里面有120多万数据,非常地慢 于是采用的复制的方法,命令如下: create table students_backup as select * ...

  6. Android——Activity练习

    manifests里的AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> < ...

  7. hdu 3368 曾经下过的棋

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3368 就是讲一种下棋的方法,很多人小时候也应该玩过,输入8*8的矩阵代表棋盘,*代表空位 D代表黑子, ...

  8. 01. pt-align

    01. pt-align pt-align xxx.txt =========================================== pt-align对齐输出格式 name city a ...

  9. Tinyos学习笔记(二)

    1.TinyOS communication tools java serialApp -comm serial@/dev/ttyUSB0:telosb java net.tinyos.tools.L ...

  10. idea maven 创建webapp项目没有src目录

    archetypeCatalog=internal