MainActivity例如以下:

package cc.testpointtoposition;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.app.Activity;
/**
* Demo描写叙述:
* ListView中pointToPosition()方法使用演示样例.
* 根据触摸点的坐标计算出点击的是ListView的哪个Item
*
* 參考资料:
* http://blog.csdn.net/guolin_blog/article/details/17357967
* Thank you very much
*/
public class MainActivity extends Activity {
private ListView mListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
listenListViewTouch();
} private void init(){
mListView=(ListView) findViewById(R.id.listView);
List<HashMap<String, Object>> list=new ArrayList<HashMap<String,Object>>();
HashMap<String, Object> hashMap=null;
for (int i = 0; i < 8; i++) {
hashMap=new HashMap<String, Object>();
hashMap.put("name", "小明"+i);
hashMap.put("phone", "135"+i);
hashMap.put("amount","500"+i);
list.add(hashMap);
}
SimpleAdapter adapter=new SimpleAdapter(this, list, R.layout.listviewitem,
new String []{"name","phone","amount"}, new int[]{R.id.name,R.id.phone,R.id.amount});
mListView.setAdapter(adapter);
} private void listenListViewTouch(){
mListView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int item=mListView.pointToPosition((int) event.getX(), (int) event.getY());
System.out.println("---> 如今点击了ListView中第"+(item+1)+"个Item");
return true;
}
});
} }

main.xml例如以下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" > <TextView
android:id="@+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="測试ListView的pointToPosition()方法" /> <ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@id/textView" /> </RelativeLayout>

listviewitem例如以下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:gravity="center"
android:orientation="horizontal" > <TextView
android:id="@+id/name"
android:layout_width="100dip"
android:layout_height="wrap_content" /> <TextView
android:id="@+id/phone"
android:layout_width="150dip"
android:layout_height="wrap_content" /> <TextView
android:id="@+id/amount"
android:layout_width="fill_parent"
android:layout_height="wrap_content" /> </LinearLayout>

ListView中pointToPosition()方法使用具体演示样例的更多相关文章

  1. 关于 underscore 中模板引擎的应用演示样例

    //关于 underscore 中模板引擎的应用演示样例 <!doctype html> <html> <head> <meta charset=" ...

  2. android listview综合使用演示样例_结合数据库操作和listitem单击长按等事件处理

    本演示样例说明: 1.自己定义listview条目样式,自己定义listview显示列数的多少,灵活与数据库中字段绑定. 2.实现对DB的增删改查,而且操作后listview自己主动刷新. 3.响应用 ...

  3. 让你提前认识软件开发(19):C语言中的协议及单元測试演示样例

    第1部分 又一次认识C语言 C语言中的协议及单元測试演示样例 [文章摘要] 在实际的软件开发项目中.常常要实现多个模块之间的通信.这就须要大家约定好相互之间的通信协议,各自依照协议来收发和解析消息. ...

  4. C++ Primer中文本查询演示样例Query的实现

    近期在看C++ Primer复习C++的语法,看到书中15.9章中的文本查询演示样例时,认为设计得非常不错,于是便动手照着实现了一个,改动了非常久最终执行成功了,从中也学习到了非常多的语法.以下把实现 ...

  5. Android中MVP模式与MVC模式比較(含演示样例)

    原文链接 http://sparkyuan.me/ 转载请注明出处 MVP 介绍 MVP模式(Model-View-Presenter)是MVC模式的一个衍生. 主要目的是为了解耦,使项目易于维护. ...

  6. 01_MUI之Boilerplate中:HTML5演示样例,动态组件,自己定义字体演示样例,自己定义字体演示样例,图标字体演示样例

     1安装HBuilder5.0.0,安装后的界面截图例如以下: 2 依照https://www.muicss.com/docs/v1/css-js/boilerplate-html中的说明,创建上 ...

  7. Mockito的简单使用方法演示样例

    Mockito是一个流行的Mocking框架.它使用起来简单,学习成本非常低.并且具有非常简洁的API,測试代码的可读性非常高.因此它十分受欢迎,用 户群越来越多.非常多的开源的软件也选择了Mocki ...

  8. JSP中文件的上传于下载演示样例

    一.文件上传的原理     1.文件上传的前提:         a.form表单的method必须是post         b.form表单的enctype必须是multipart/form-da ...

  9. 源代码方式向openssl中加入新算法完整具体步骤(演示样例:摘要算法SM3)【非engine方式】

    openssl简单介绍 openssl是一个功能丰富且自包括的开源安全工具箱.它提供的主要功能有:SSL协议实现(包括SSLv2.SSLv3和TLSv1).大量软算法(对称/非对称/摘要).大数运算. ...

随机推荐

  1. ACM--素数距离问题

    题目描述:现在给出你一些数,要求你写出一个程序,输出这些整数相邻最近的素数,并输出其相距长度.如果左右有等距离长度素数,则输出左侧的值及相应距离.如果输入的整数本身就是素数,则输出该素数本身,距离输出 ...

  2. 启用多处理器编译--加快VS2013编译

    依次打开项目“属性“==>”配置属性“==>”C/C++(或其它语言)“==>”常规“,最后一项,多处理器编译选择是. 官方解释如下: /MP 选项在命令行上以减少总时间编译源文件. ...

  3. Debian安装VirtualBox增强工具

    切换到root用户: apt-get install build-essential 或者 apt-get install gcc make apt-get install dkms apt-get ...

  4. ArcGIS中的多个栅格波段合成一幅影像

    此处用到了ArcGIS栅格处理中的Composite Bands工具( Data Management Tools --> Raster --> Raster Processing).具体 ...

  5. IOS开发使用委托delegate在不同窗口之间传递数据

    IOS开发使用委托delegate在不同窗口之间传递数据是本文要介绍的内容,主要是来讲解如何使用委托delegate在不同窗口之间传递数据,具体内容来看详细内容.在IOS开发里两个UIView窗口之间 ...

  6. stl之list双向链表容器应用基础

    不同于採用线性表顺序存储结构的vector和deque容器.list双向链表中任一位置的元素差值.插入和删除,都具有高效的常数阶算法时间复杂度O(1). 头文件 #include<list> ...

  7. gflops

    这个网站最棒了 http://kyokojap.myweb.hinet.net/gpu_gflops/

  8. Extjs grid 遍历store

    var projectMemberGrid = Ext.getCmp("projectMemberGrid"); var selFuns = []; projectMemberGr ...

  9. Java中Vector与ArrayList的差别具体解释

    首先看这两类都实现List接口,而List接口一共同拥有三个实现类.各自是ArrayList.Vector和LinkedList.List用于存放多个元素,可以维护元素的次序,而且同意元素的反复. 3 ...

  10. webpack的配置文件entry与output

    在webpack.config.js中entry是唯一入口文件 entry也可以是一个数组 如果是一个数组,会将数组里面的文件一起打包到bundle.js entry也可以是一个对象. 如果outpu ...