android-基础编程-ListView
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的更多相关文章
- 【Android基础】listview控件的使用(4)-----自定义布局的listview的使用
前面我介绍了listview控件的不同用法,但是这些用法在实际的开发项目中是不足以满足需求的,因为前面的几种用法只能简单的显示文本信息,而且布局都比较单一,很难做出复杂的结果,在实际的开发项目中,90 ...
- 【Android基础】listview控件的使用(2)-------继承自ListActivity的普通listview
由于listview在android控件中的重要性,所以android为我们直接封装了一个类ListviewActivity,直接将listview封装在了activity之中,在本篇中,我将介绍在L ...
- 【Android基础】listview控件的使用(1)------最简单的listview的使用
listview控件是项目开发中最常用的空间之一,我将慢慢推出关于listview的一系列的文章,先从最简单的,系统自带的listview开始吧! 先上效果图: activity_one.xml &l ...
- Android基础TOP7_1:ListView制作列表
结构: Activity: activity_main: <RelativeLayout xmlns:android="http://schemas.android.com/apk/r ...
- 【Android基础】listview控件的使用(3)------Map与SimpleAdapter组成的多显示条目的Listview
前面介绍的两种listview的使用都是最基础的,所以有很大的局限性,比如只能在一个item(即每一行的条目)中显示一个文本信息,这一篇我将介绍Map与SimpleAdapter组成的多显示条目的Li ...
- Android基础控件ListView基础操作
1.简介 基于Android基础控件ListView和自定义BaseAdapter适配器情况下,对ListView的数据删除和添加操作: public boolean add(E e) {//添加数据 ...
- android: 多线程编程基础
9.1 服务是什么 服务(Service)是 Android 中实现程序后台运行的解决方案,它非常适合用于去执行那 些不需要和用户交互而且还要求长期运行的任务.服务的运行不依赖于任何用户界面,即使 ...
- Android网络编程基础
Android网络编程只TCP通信 TCP 服务器端工作的主要步骤如下.步骤1 调用ServerSocket(int port)创建一个ServerSocket,并绑定到指定端口上.步骤2 调用acc ...
- <Android基础>(三) UI开发 Part 2 ListView
ListView 1)ListView的简单用法 2)定制ListView界面 3)提升ListView的运行效率 4)ListView的点击事件 3.5 ListView 3.5.1 ListVie ...
- 安卓Android基础第三天——数据库,ListView
数据库介绍sqlite问:什么情况下使用数据库?答:有大量相似结构的数据需要存储的时候 数据库的创建定义一个类继承SqliteOpenHelpercontext:上下文name:数据库名字,如&quo ...
随机推荐
- Android 工程目录
app java:我们写Java代码的地方,业务功能都在这里实现 res:存放我们各种资源文件的地方,有图片,字符串,动画,音频等,还有各种形式的XML文件 Gradle Scripts 1.res资 ...
- TouchSlide 插件使用介绍
TouchSlide(PC端插件http://www.superslide2.com/demo.html#effect1) 可用于javascript触屏滑动特效插件,移动端滑动特效,触屏焦点图,触屏 ...
- html标签二
1.没有前后顺序的信息列表<ul> <li></li> <li></li></ul>2.有序列表 <ol> < ...
- linux查看文件被哪个进程占用?
1> 如果文件是端口号 netstat -ntlp | grep portNum [root@localhost root]# netstat -ntlp Active Internet con ...
- Codeforces55D Beautiful numbers
原题链接 虽然依旧是套模板,但是因为我太弱了,不会建状态,所以去看了题解.. 这里就直接引用我看的题解吧,写的不错的. 题解 //我的代码 #include<cstdio> #includ ...
- PC 上的 LVM 灾难修复
LVM 介绍 LVM 简介 LVM 是逻辑盘卷管理(Logical Volume Manager)的简称,最早是 IBM 为 AIX 研发的存储管理机制.LVM 通过在硬盘和分区之间建立一个逻辑层,可 ...
- Spring 循环引用(二)源码分析
Spring 循环引用(二)源码分析 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) Spring 循环引用相关文章: & ...
- sqli-labs:17,增删改
增 insert into users values(','lcamry','lcamry'); 删 delete from users where id=16 删数据库:drop database ...
- Maven中maven-source-plugin,maven-javadoc-plugin插件的使用
摘要:今天领导说要把项目通过maven生产源码包和文档包并发布到自己的私服上,经过查看mavne官网发现有两个maven插件可以做到这些工作,一个是maven-source-plugin,另一个是ma ...
- Windows 8.1 新控件和功能:
http://msdn.microsoft.com/zh-cn/library/windows/apps/bg182878.aspx#five 将 XAML 树呈现为位图: 适用于 Windows 8 ...