在ListView选中一行,可以显示上下文界面如下:
在ListView中添加上下文菜单ContextMenu的核心代码:
//添加上下文事件点击事件 list.setOnCreateContextMenuListener(new OnCreateContextMenuListener() { @Override public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) { menu.setHeaderTitle("长按菜单-ContextMenu"); menu.add(0, 0, 0, "弹出长按菜单0"); menu.add(0, 1, 0, "弹出长按菜单1"); } });
源代码如下:
package com.easyway.android.listview; import java.util.ArrayList; import java.util.HashMap; 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.view.View.OnCreateContextMenuListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListView; import android.widget.SimpleAdapter; /** * ListView和上下文菜单的使用 * @author longgangbai * */ public class ListViewActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //绑定Layout里面的ListView ListView list = (ListView) findViewById(R.id.ListView01); //生成动态数组,加入数据 ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>(); for(int i=0;i<10;i++) { HashMap<String, Object> map = new HashMap<String, Object>(); map.put("ItemImage", R.drawable.icon);//图像资源的ID map.put("ItemTitle", "Level "+i); map.put("ItemText", "Finished in 1 Min 54 Secs, 70 Moves! "); listItem.add(map); } //生成适配器的Item和动态数组对应的元素 SimpleAdapter listItemAdapter = new SimpleAdapter(this,listItem,//数据源 R.layout.listview,//ListItem的XML实现 //动态数组与ImageItem对应的子项 new String[] {"ItemImage","ItemTitle", "ItemText"}, //ImageItem的XML文件里面的一个ImageView,两个TextView ID new int[] {R.id.ItemImage,R.id.ItemTitle,R.id.ItemText} ); //添加并且显示 list.setAdapter(listItemAdapter); //添加选中点击事件 list.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { setTitle("点击第"+arg2+"个项目"); } }); //添加上下文事件点击事件 list.setOnCreateContextMenuListener(new OnCreateContextMenuListener() { @Override public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) { menu.setHeaderTitle("长按菜单-ContextMenu"); menu.add(0, 0, 0, "弹出长按菜单0"); menu.add(0, 1, 0, "弹出长按菜单1"); } }); } //长按菜单响应函数 @Override public boolean onContextItemSelected(MenuItem item) { setTitle("点击了长按菜单里面的第"+item.getItemId()+"个项目"); return super.onContextItemSelected(item); } }
ListView中每行的配置信息:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout android:id="@+id/RelativeLayout01" android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:paddingBottom="4dip" android:paddingLeft="12dip" android:paddingRight="12dip"> <ImageView android:paddingTop="12dip" android:layout_alignParentRight="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/ItemImage" /> <TextView android:text="TextView01" android:layout_height="wrap_content" android:textSize="20dip" android:layout_width="fill_parent" android:id="@+id/ItemTitle" /> <TextView android:text="TextView02" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_below="@+id/ItemTitle" android:id="@+id/ItemText" /> </RelativeLayout>
主界面的xml配置:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:id="@+id/LinearLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <ListView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/ListView01" /> </LinearLayout>
下面一个实例:
package com.easyway.android.listview; import java.util.ArrayList; import android.app.ListActivity; import android.os.Bundle; import android.view.ContextMenu; import android.view.ContextMenu.ContextMenuInfo; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView.AdapterContextMenuInfo; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import android.widget.Toast; public class ArrayActivity extends ListActivity { private EditText inputtext; private Button btnadd; private ArrayList<String> list = new ArrayList<String>(); ArrayAdapter<String> adapter; private static final int ITEM1=Menu.FIRST; private static final int ITEM2=Menu.FIRST+1; private static final int ITEM3=Menu.FIRST+2; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /** * 这里虽然是继承了ListActivit,但是如果想使用自己的布局, * ListView中的id必须是“@android:id/list”,否则出错 */ setContentView(R.layout.ilistview); inputtext = (EditText)findViewById(R.id.edittext); btnadd = (Button)findViewById(R.id.additem); adapter = new ArrayAdapter<String>( this, android.R.layout.simple_expandable_list_item_1, list); //设置适配器 setListAdapter(adapter); //为 ListView 的所有 item 注册 ContextMenu this.registerForContextMenu(getListView()); btnadd.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { list.add(inputtext.getText().toString()); adapter.notifyDataSetChanged(); inputtext.setText(""); } }); } //设置Context,用来进行删除操作 @Override protected void onListItemClick(ListView l, View v, int position, long id) { // TODO Auto-generated method stub super.onListItemClick(l, v, position, id); list.remove(position); adapter.notifyDataSetChanged(); Toast.makeText(getApplicationContext(), "删除此项", Toast.LENGTH_SHORT).show(); } /** * 创建ContextMenu菜单 */ @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { // TODO Auto-generated method stub super.onCreateContextMenu(menu, v, menuInfo); menu.setHeaderTitle("提示信息"); menu.setHeaderIcon(R.drawable.icon); menu.add(0,ITEM1,0,"删除Item"); menu.add(0,ITEM2,0,"添加Item"); menu.add(0,ITEM3,0,"关于我"); } /** * ContextMenu中选项被选中的事件 */ @Override public boolean onContextItemSelected(MenuItem item) { AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo)item.getMenuInfo(); switch(item.getItemId()) { case ITEM1: //System.out.println("Item id="+item.getItemId()); //System.out.println("position="+getListAdapter().getItemId(menuInfo.position)); /** * getListAdapter().getItemId(menuInfo.position)返回的值是Long型, * 而list.remove(position)是String java.util.ArrayList.remove(int index), * 所以当执行到这里的时候,如果直接使用Long型的话,由于不是int型的,所以默认 * 使用的是:boolean java.util.ArrayList.remove(Object object) * 两个remove()不匹配,所以重载的是remove(Object object),而这个方法是不能删除item的 * 所以需要将得到的position强制转换为int型的才能运行 */ //list.remove(getListAdapter().getItemId(menuInfo.position));这句话是错误的 int pos = (int) getListAdapter().getItemId(menuInfo.position); if(list.remove(pos) != null) { System.out.println("success"); } else { System.out.println("failed"); } //通知适配器更新 adapter.notifyDataSetChanged(); Toast.makeText(getApplicationContext(), "删除此项", Toast.LENGTH_SHORT).show(); break; case ITEM2: list.add("I am android !"); adapter.notifyDataSetChanged(); break; case ITEM3: Toast.makeText(getApplicationContext(), "I am android !", Toast.LENGTH_SHORT).show(); break; } return super.onContextItemSelected(item); } }
listview的布局xml配置信息:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <EditText android:id="@+id/edittext" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" /> <Button android:id="@+id/additem" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="4" android:text="添加" /> </LinearLayout> <ListView android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>