public class MainActivity extends Activity implements OnItemClickListener,
OnScrollListener { private ListView listView;
private SimpleAdapter simple_adapter;
private List<Map<String, Object>> list;
private int i=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 匹配布局文件中的ListView控件
listView = (ListView) findViewById(R.id.listView);
//数据适配器的数据源
String[] data = new String[] { "java", "C++", "JavaScript", "Php",
"Python" };
//参数1:MainActivity:当前上下文
//参数2:当前ListView加载的每一个列表项所对应的布局文件
//参数3:数据源
// 数据适配器的定义,并加载数据源
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
MainActivity.this, android.R.layout.simple_list_item_1, data);
// 给ListView设置数据适配器
listView.setAdapter(adapter);
// 设置ListView的元素被选中时的事件处理监听器
listView.setOnItemClickListener(this);
// getData();
// 设置SimpleAdapter监听器
//new String[]{}:代表数据源中键值对的键名
new int []{}:代表键值对的id
// simple_adapter = new SimpleAdapter(MainActivity.this,
// list, R.layout.list_item,
// new String[] { "image", "text" }, new int[] { R.id.image,
// R.id.text });
// listView.setAdapter(simple_adapter);
// listView.setOnScrollListener(this);
} // 加载SimpleAdapter数据集
private List<Map<String, Object>> getData() {
list = new ArrayList<Map<String, Object>>();
Map<String, Object> map = new HashMap<String, Object>();
map.put("text", "java");
map.put("image", R.drawable.ic_launcher);
Map<String, Object> map2 = new HashMap<String, Object>();
map2.put("text", "C++");
map2.put("image", R.drawable.ic_launcher);
Map<String, Object> map3 = new HashMap<String, Object>();
map3.put("text", "JavaScript");
map3.put("image", R.drawable.ic_launcher);
Map<String, Object> map4 = new HashMap<String, Object>();
map4.put("text", "Php");
map4.put("image", R.drawable.ic_launcher);
Map<String, Object> map5 = new HashMap<String, Object>();
map5.put("text", "Python2");
map5.put("image", R.drawable.ic_launcher);
list.add(map);
list.add(map2);
list.add(map3);
list.add(map4);
list.add(map5);
return list;
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} // (5)事件处理监听器方法
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
// 获取点击ListView item中的内容信息
String text = listView.getItemAtPosition(position) + "";
// 弹出Toast信息显示点击位置和内容
Toast.makeText(MainActivity.this, "position=" + position + " content=" + text,Toast.LENGTH_SHORT).show(); } @Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
// 手指离开屏幕前,用力滑了一下
if (scrollState == SCROLL_STATE_FLING) {
Toast.makeText(MainActivity.this, "用力滑一下",Toast.LENGTH_SHORT).show();
Map<String, Object> map = new HashMap<String, Object>();
map.put("text", "滚动添加 "+i++);
map.put("image", R.drawable.ic_launcher);
list.add(map);
listView.setAdapter(simple_adapter);
//重新通知线程刷新UI界面
simple_adapter.notifyDataSetChanged();
} else
// 停止滚动
if (scrollState == SCROLL_STATE_IDLE) { } else
// 正在滚动,手指没有离开屏幕
if (scrollState == SCROLL_STATE_TOUCH_SCROLL) { }
} @Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub } }

ListView用法的更多相关文章

  1. ListView用法总结C#

    ListView是个较为复杂的控件     网上教程写的很乱,C#中文资料太匮乏了,小白叔叔觉得有必要自己出一份了. http://blog.sina.com.cn/s/blog_43eb83b901 ...

  2. C# ListView用法

    ListView是个较为复杂的控件       1.定义   把它拽进来,系统会自动在Designer.cs里添加一个  this.listView1 = new System.Windows.For ...

  3. C# ListView用法详解 很完整

    一.ListView类 1.常用的基本属性: (1)FullRowSelect:设置是否行选择模式.(默认为false) 提示:只有在Details视图该属性才有意义. (2) GridLines:设 ...

  4. JSON详解+ C# String.Format格式说明+ C# ListView用法详解 很完整

    JSON详解 C# String.Format格式说明 C# ListView用法详解 很完整

  5. ListView用法总结

    前言 列表,它作为一种非常重要的显示形式,不管是在web端还是在移动平台上,都是一种非常友好的,功能强大的展现形式.在Android中,ListView就接管了这一重任.尽管在Android5.X时代 ...

  6. winform listview用法

    资源收集 C#winform中ListView的使用 C# WinForm开发系列 - ListBox/ListView/Panel(介绍了一些listview的高级用法) 直接上代码 示例一: th ...

  7. Spinner用法与ListView用法

    参考: http://blog.csdn.net/u012960536/article/details/46732421 --------------------------------------- ...

  8. Android ListView用法

    写了一个简单的示例来说明ListView的用法:给定一个单词,下面有四个含义,找出正确的一个,无论是否成功,则会跳转到下一个单词:主要用到的知识有: findViewById().  ListView ...

  9. 3.17学习总结.listview用法总结

    今天复习了listview控件的用法. 1.activity_main.xml 中的代码,如下: <?xml version="1.0" encoding="utf ...

  10. C# ListView用法详解

    一.ListView类 1.常用的基本属性: (1)FullRowSelect:设置是否行选择模式.(默认为false) 提示:只有在Details视图该属性才有意义. (2) GridLines:设 ...

随机推荐

  1. linux 的硬链接与软连接

    linux 里有硬链接和软连接两种概念.要明白这些概念首先要明白文件在linux 上其实有3个组成部分. data 真正的数据存储区域 inode 一个用来唯一表示data的数据结构 filename ...

  2. 模拟用户点击弹出新页面不会被浏览器拦截_javascript技巧

    原文:http://www.html5cn.com.cn/article/zxzx/3195.html 相信用过window.open的小伙伴们都遇到过被浏览器拦截导致页面无法弹出的情况:我们换下思路 ...

  3. 【Spark】Spark容错机制

    引入 一般来说,分布式数据集的容错性有两种方式:数据检查点和记录数据的更新. 面向大规模数据分析,数据检查点操作成本非常高,须要通过数据中心的网络连接在机器之间复制庞大的数据集,而网络带宽往往比内存带 ...

  4. 带头尾和动画的下拉刷新RecyclerView

    项目地址:https://github.com/shichaohui/AnimRefreshRecyclerView 项目中包括一个demo(普通Androidproject)和Android Lib ...

  5. ie 代理设置中地址和端口置灰的解决办法

    @echo offecho 代理设置reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings&quo ...

  6. HDU Today HDU杭电2112【Dijkstra || SPFA】

    http://acm.hdu.edu.cn/showproblem.php?pid=2112 Problem Description 经过锦囊相助,海东集团最终度过了危机,从此.HDU的发展就一直顺风 ...

  7. 读书笔记:Information Architecture for the World Wide Web, 3rd Edition 北极熊 第一部分 1-3

    Introducing Information Architecture 信息架构简介 Chapter 1 Defining Information Architecture 信息架构的意义(我们盖房 ...

  8. commons-fileupload 组件实现文件上传

    index.jsp 核心代码: //创建文件项工厂 DiskFileItemFactory factory = new DiskFileItemFactory(); //创建解析请求 数据的Servl ...

  9. Android6.0源码分析之录音功能(一)【转】

    本文转载自:http://blog.csdn.net/zrf1335348191/article/details/54949549 从现在开始一周时间研究录音,下周出来一个完整的博客,监督,激励!!! ...

  10. Mac系统打开命令行终端及查看操作系统版本号的方法

    Mac系统打开命令行终端的方法: 应用程序 --> 实用工具 --> 终端 Mac系统终端查看操作系统版本号的方法: 输入:#more /System/Library/CoreServic ...