ListView中pointToPosition()方法使用具体演示样例
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()方法使用具体演示样例的更多相关文章
- 关于 underscore 中模板引擎的应用演示样例
//关于 underscore 中模板引擎的应用演示样例 <!doctype html> <html> <head> <meta charset=" ...
- android listview综合使用演示样例_结合数据库操作和listitem单击长按等事件处理
本演示样例说明: 1.自己定义listview条目样式,自己定义listview显示列数的多少,灵活与数据库中字段绑定. 2.实现对DB的增删改查,而且操作后listview自己主动刷新. 3.响应用 ...
- 让你提前认识软件开发(19):C语言中的协议及单元測试演示样例
第1部分 又一次认识C语言 C语言中的协议及单元測试演示样例 [文章摘要] 在实际的软件开发项目中.常常要实现多个模块之间的通信.这就须要大家约定好相互之间的通信协议,各自依照协议来收发和解析消息. ...
- C++ Primer中文本查询演示样例Query的实现
近期在看C++ Primer复习C++的语法,看到书中15.9章中的文本查询演示样例时,认为设计得非常不错,于是便动手照着实现了一个,改动了非常久最终执行成功了,从中也学习到了非常多的语法.以下把实现 ...
- Android中MVP模式与MVC模式比較(含演示样例)
原文链接 http://sparkyuan.me/ 转载请注明出处 MVP 介绍 MVP模式(Model-View-Presenter)是MVC模式的一个衍生. 主要目的是为了解耦,使项目易于维护. ...
- 01_MUI之Boilerplate中:HTML5演示样例,动态组件,自己定义字体演示样例,自己定义字体演示样例,图标字体演示样例
1安装HBuilder5.0.0,安装后的界面截图例如以下: 2 依照https://www.muicss.com/docs/v1/css-js/boilerplate-html中的说明,创建上 ...
- Mockito的简单使用方法演示样例
Mockito是一个流行的Mocking框架.它使用起来简单,学习成本非常低.并且具有非常简洁的API,測试代码的可读性非常高.因此它十分受欢迎,用 户群越来越多.非常多的开源的软件也选择了Mocki ...
- JSP中文件的上传于下载演示样例
一.文件上传的原理 1.文件上传的前提: a.form表单的method必须是post b.form表单的enctype必须是multipart/form-da ...
- 源代码方式向openssl中加入新算法完整具体步骤(演示样例:摘要算法SM3)【非engine方式】
openssl简单介绍 openssl是一个功能丰富且自包括的开源安全工具箱.它提供的主要功能有:SSL协议实现(包括SSLv2.SSLv3和TLSv1).大量软算法(对称/非对称/摘要).大数运算. ...
随机推荐
- Bean实例化(三种方法)
(一)构造器实例化Bean 1. Bean1.java package com.inspur.ioc; public class Bean1 { } 2.Beans1.xml <?xml ver ...
- apk打包
1.在导航栏中选择Builder->Generate Signed Apk 2.新建点击Creat new... 3.注意路径后面写apk的名字(这个名字将会显示在手机软件的下方)
- React Native学习之自定义Navigator
Navigator还是最常用的组件, 所以自己封装了一个, 使用起来也比较简单, 如下: 首先导入组件 var MLNavigator = require('../Lib/MLNavigator'); ...
- 从系统相册中选择GIF图片上传到服务器
-(void)assetPickerController:(ZYQAssetPickerController *)picker didFinishPickingAssets:(NSArray *)as ...
- wait和notify函数的规范代码模板
// The standard idiom for calling the wait method in Java synchronized (sharedObject) { while (condi ...
- javolution-core-java-6.1.0.jar 的使用
官方网址:http://javolution.org/apidocs/javolution/io/Struct.html 第一步:导包 第二步:创建继承的结构体 结构体定义如下所示: public c ...
- Visual Studio 断点无法命中怎么办?
经常远程调试服务器打断点是空心的,很抓狂,正确的方法应该是 #if DEBUG if (System.Diagnostics.Debugger.IsAttached) { System.Diagnos ...
- Delphi 通过SQLite3, SQLiteTable3 操作数据库
var sql, sFile:string; db:TSQLiteDatabase;begin try sFile := G_AppPath + CH_IPC712Db; //if FileExist ...
- pomise的简单用法
1.then的基本简单用法,当异步获取完数据后就会自动执行then的方法 function runAsync1(){ var p = new Promise(function(resolve, rej ...
- Coherence装载数据的研究-PreloadRequest
最近给客户准备培训,看到Coherence可以通过三种方式批量加载数据,分别是: Custom application InvocableMap - PreloadRequest Invocation ...