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. how to read openstack code : routes

    When coding a web system, you have to think about an important problem, how to map urls to logic. Op ...

  2. 功能超级强大的网络工具nc

    摘自:http://www.linuxso.com/command/nc.html 功能说明:功能强大的网络工具语 法:nc [-hlnruz][-g<网关...>][-G<指向器数 ...

  3. laravel有用的方法

    1.tinker 造假数据 factory('App\User',3)->create(); DB::table 返回collection,可以用collection中的很多方法 比如-> ...

  4. Java循环中删除一个列表元素

    本文主要想讲述一下我对之前看到一篇文章的说法.假设跟你的想法有出入,欢迎留言.一起讨论. #3. 在循环中删除一个列表元素 考虑以下的代码.迭代过程中删除元素: ArrayList<String ...

  5. Java RMI之HelloWorld程序以及相关的安全管理器的知识

    Java RMI 指的是远程方法调用 (Remote Method Invocation).它是一种机制,可以让在某个 Java 虚拟机上的对象调用还有一个 Java 虚拟机中的对象上的方法.可以用此 ...

  6. 通过构建Cocoapods私有库进行组件化开发探索

    专题一 一.创建私有索引库 选Github或者码云都可以,本例以Github为例.创建私有索引库用来作为自己组件库的索引: 二.本地添加私有索引库 添加:pod repo add 索引库名称 索引库地 ...

  7. ios打包静态库

    1. 什么是库? 所谓库就是程序代码的集合,是共享程序代码的一种方式. 2. 库的分类 根据程序代码的开源情况,库可以分为两类 开源库源代码是公开的,你可以看到具体实现.比如GitHub上比较出名的第 ...

  8. scu oj 4442 Party(2015年四川省acm程序设计竞赛)

    Party n frogs are invited to a tea party. Frogs are conveniently numbered by 1,2,-,n. The tea party ...

  9. Android架构的简单探讨(一)

    在CSDN上看到这样一篇译文,虽然最终的解决方案要按照自己特定的项目去设计,但该文还是引起了很多自己的共鸣,原文猛戳这里. 这是他提出的基于Messaging的MVC框架: 其中包含的设计思想在于:哪 ...

  10. height not divisible by 2

    height not divisible by 2 h.264 - FFMPEG (libx264) "height not divisible by 2" - Stack Ove ...