要实现ListView的长按点击事件,可以直接调用setOnItemLongClickListener,在onLongClick里面实现逻辑,也可以使用注册上下文菜单的方法,比如要实现下面的效果
可以在item的长按事件中弹出一个对话框或者popupwindow来实现,今天我们换一种方法
- package com.example.test;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.ContextMenu;
- import android.view.ContextMenu.ContextMenuInfo;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.AdapterView.AdapterContextMenuInfo;
- import android.widget.ArrayAdapter;
- import android.widget.ListView;
- import android.widget.Toast;
- public class MainActivity extends Activity {
- private ListView mListView;
- private String msg[] = new String[] { "测试1", "测试2", "测试3", "测试4" };
- String number;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- mListView = (ListView) findViewById(R.id.listview);
- ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
- android.R.layout.simple_list_item_1, msg);
- mListView.setAdapter(adapter);
- registerForContextMenu(mListView);
- }
- @Override
- public void onCreateContextMenu(ContextMenu menu, View v,
- ContextMenuInfo menuInfo) {
- super.onCreateContextMenu(menu, v, menuInfo);
- number = (String) mListView
- .getItemAtPosition(((AdapterContextMenuInfo) menuInfo).position);//获取listview的item对象
- getMenuInflater().inflate(R.menu.context_menu, menu);
- }
- @Override
- public boolean onContextItemSelected(MenuItem item) {
- if (item.getItemId() == R.id.menu_item1) {
- Toast.makeText(this, number, Toast.LENGTH_SHORT).show();
- } else if (item.getItemId() == R.id.menu_item2) {
- Toast.makeText(this, "选项2被选择了", Toast.LENGTH_SHORT).show();
- }
- return super.onContextItemSelected(item);
- }
- }
长按测试2,显示上下文菜单,再点击菜单项1,显示一个吐司,效果如下
- <?xml version="1.0" encoding="utf-8"?>
- <menu xmlns:android="http://schemas.android.com/apk/res/android" >
- <item
- android:id="@+id/menu_item1"
- android:icon="@drawable/avatar_default"
- android:title="菜单项1"/>
- <item
- android:id="@+id/menu_item2"
- android:icon="@drawable/avatar_default"
- android:title="菜单项2"/>
- </menu>
activity_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"
- android:background="@color/white"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- tools:context="com.example.test.MainActivity" >
- <ListView
- android:id="@+id/listview"
- android:layout_width="match_parent"
- android:layout_height="wrap_content" />
- </RelativeLayout>
registerForContextMenu的源码
- public void registerForContextMenu(View view) {
- view.setOnCreateContextMenuListener(this);
- }
可以看出,不只是ListView,只要是View就可以注册上下文菜单,打开 setOnCreateContextMenuListener
- public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
- if (!isLongClickable()) {
- setLongClickable(true);
- }
- getListenerInfo().mOnCreateContextMenuListener = l;
- }
原来是注册了View的长按事件