一、概述
这个接口定义了当AdapterView中一元素被点击时,一个回调函数被调用。
二、公共方法
public abstract void onItemClick (AdapterView<?> parent, View view, int position, long id)
当AdapterView中一元素被点击时,回调方法被调用。
如果需要访问与被选项相关的数据,执行程序可以调用getItemAtPosition(position)。
参数
parent 发生点击动作的AdapterView。
view 在AdapterView中被点击的视图(它是由adapter提供的一个视图)。
position 视图在adapter中的位置。
id 被点击元素的行id。
postion与id的区别:
position | The position of the view in the adapter. |
---|---|
id | The row id of the item that was clicked. |
例如:对于ListView mHomeListView
- TextView textView = new TextView(this);
- textView.setText("headerView");
- mHomeListView.addHeaderView(textView);
给mHomeListView 添加headerView 后,界面显示:
当点击 headerView行时:id=-1, position =0;
当点击 Stand行时:id=0,position=1;
当点击 Far Awa...行时:id=1,position=2;
至此,发现的规律:position是view在 adapterView中的位置,id是被点击元素在总数据adapter行id。
关于onItemClick方法,下面详细说说。(以下是官方的说明,参数命名不同而已,类型都是一样的。arg0=parent,arg1=view,arg2=position,arg3=id)
public abstract void onItemClick(AdapterView<?> parent,View view, int position, long id)
Callback method to be invoked when an item in this AdapterView has been clicked.
Implementers can call getItemAtPosition(position) if they need to access the data associated with the selected item.
//当点击listview某一项时,这个回调方法就会被调用。
Parameters
parent | The AdapterView where the click happened. |
---|---|
view | The view within the AdapterView that was clicked (this will be a view provided by the adapter) |
position | The position of the view in the adapter. |
id | The row id of the item that was clicked. |
主要说说最后三个参数,
view——————是你点击的Listview的某一项的内容,来源于adapter。如用((TextView)arg1).getText(),可以取出点击的这一项的内容,转为string类型。
position————是adapter的某一项,如点击了listview第2项,而第2项对应的是adapter的第2个数值,那此时position的值就为1了。
如对应adapter的第3个数值,那此时position的值就为2了。
id———————id的值为点击了Listview的哪一项对应的数值,点击了listview第2项,那id就等于1。
注:这些数值都是从0开始的。
- /*author:conowen
- * date:2012.2.26
- */
- package com.conowen.listview;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.AdapterView.OnItemClickListener;
- import android.widget.AdapterView.OnItemLongClickListener;
- import android.widget.ArrayAdapter;
- import android.widget.ListView;
- import android.widget.TextView;
- import android.widget.Toast;
- public class ListviewActivity extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- ListView lv = (ListView) findViewById(R.id.lv);
- String[] data = { "windows", "linux", "ios", "android", "wp7",
- "Symbian" };// 定义adapter
- lv.setAdapter(new ArrayAdapter<String>(this,// 把adapter绑定到listview里面
- android.R.layout.simple_expandable_list_item_1, data));
- // 点击事件
- lv.setOnItemClickListener(new OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
- long arg3) {
- // TODO Auto-generated method stub
- Toast.makeText(ListviewActivity.this, "你点击的是第" + arg3 + "项",
- Toast.LENGTH_SHORT).show();
- // 取出所点击的那一项的id
- }
- });
- // 长按事件
- lv.setOnItemLongClickListener(new OnItemLongClickListener() {
- @Override
- public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
- int arg2, long arg3) {
- // TODO Auto-generated method stub
- Toast.makeText(ListviewActivity.this,
- ((TextView) arg1).getText(), Toast.LENGTH_LONG).show();
- // 取出点击listview某一项的内容
- return false;
- }
- });
- }
- }