Android 弹出菜单示例#
这个示例展示如何使用PopupMenu来现实和一个弹出的菜单。
说明
这个示例展示一个水果的列表,每一个列表项均可以点击,每一个列表项的右边有一个图片按钮,点击该按钮,将弹出一个菜单。
这个示例运行在Android4.0以上,不支持Android4.0以下版本!
运行效果
Paste_Image.png Paste_Image.png代码
latyout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_fruits"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"/>
水果列表中每一个列表项的布局
layout/list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/image_fruit"
android:layout_width="52dp"
android:layout_height="52dp"
android:contentDescription="@string/image_fruit_des" />
<TextView
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:gravity="center_vertical"
android:id="@+id/name_fruit"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceListItem" />
<ImageView
android:id="@+id/popupButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|center_vertical"
android:clickable="true"
android:contentDescription="@string/image_popup"
android:padding="8dp"
android:src="@android:drawable/arrow_down_float" />
</LinearLayout>
values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">弹出菜单示例</string>
<string name="action_settings">设置</string>
<string name="image_fruit_des">水果的图片</string>
<string name="image_popup">弹出菜单按钮</string>
<string name="menu_popup">删除</string>
</resources>
menu/popup_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!--弹出删除菜单-->
<item
android:id="@+id/menu_popup_remove"
android:title="@string/menu_popup"
/>
</menu>
src/你的应用的包名/Fruits.java
package com.ddzj.listpopupmenu;
/*
* 数据源
*/
public class Fruits {
// 水果的图片
public static final int[] IMAGE_FRUITS_ID = {R.drawable.ic_apple,R.drawable.ic_apricot,
R.drawable.ic_banana,R.drawable.ic_blueberry,R.drawable.ic_cherry,
R.drawable.ic_chestnut,R.drawable.ic_dragonfruit,R.drawable.ic_grape,
R.drawable.ic_grapefruit,R.drawable.ic_litchi,R.drawable.ic_loquat,
R.drawable.ic_mango,R.drawable.ic_mulberry,R.drawable.ic_orange,
R.drawable.ic_other_banana,R.drawable.ic_peach,R.drawable.ic_pineapple,
R.drawable.ic_plum,R.drawable.ic_strawberry,R.drawable.ic_yangtao};
// 水果的名字
public static final String[] FRUITS={"苹果","杏子","香蕉","蓝莓","樱桃","栗子",
"火龙果","葡萄","柚子","荔枝","枇杷","芒果","桑椹","橘子","芭蕉",
"桃子","凤梨","李子","草莓","猕猴桃"};
}
src/你的应用的包名/MainActivity.java
package com.ddzj.listpopupmenu;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.PopupMenu;
import android.widget.PopupMenu.OnMenuItemClickListener;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class MainActivity extends Activity implements OnItemClickListener, OnClickListener {
private ListView mListView; // 用户展示列表的ListView控件
private SimpleAdapter mAdapter; // 数据与ListView之间的连接桥梁
private List<Map<String, Object>> mFruitList ; // 存储数据的List
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
/*
* 初始化
*/
private void init() {
// TODO Auto-generated method stub
mListView = (ListView) findViewById(R.id.list_fruits);
mFruitList = new ArrayList<Map<String,Object>>();
// 将数据添加到mFruitList中
for (int i = 0,z=Fruits.FRUITS.length; i < z; i++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("image", Fruits.IMAGE_FRUITS_ID[i]);
map.put("name", Fruits.FRUITS[i]);
mFruitList.add(map);
}
/*
* 创建适配器
*/
mAdapter = new FruitAdapter(this, mFruitList, R.layout.list_item,
new String[]{"image","name"}, new int[]{R.id.image_fruit,R.id.name_fruit});
mListView.setAdapter(mAdapter);
mListView.setOnItemClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/*
* 当列表中的项被点击时,回调此方法
*/
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// 弹出吐司,告诉用户点击的水果名称
Toast.makeText(this, Fruits.FRUITS[position], Toast.LENGTH_SHORT).show();
}
/*
* 自定义的适配器,继承自SimpleAdapter
*/
class FruitAdapter extends SimpleAdapter{
public FruitAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from,
int[] to) {
super(context, data, resource, from, to);
}
/*
* 重写getView方法
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
View popupButton = view.findViewById(R.id.popupButton);
popupButton.setOnClickListener(MainActivity.this);
// getItem(position),在数据集中得到与指定位置对应的数据项。即mFruitList中对应位置的数据
// setTag(getItem(position));这里的是将getItem(position)返回的数据存储在popupButton中,
// 以便后面可以通过getTag()得到该数据并将其从mFruitList中移除
popupButton.setTag(getItem(position));
return view;
}
}
/*
* 当列表项中最右边的向下按钮被点击时,回调此方法
*/
@Override
public void onClick(final View v) {
// Causes the Runnable to be added to the message queue. The runnable will be run on the user interface thread.
v.post(new Runnable() {
@Override
public void run() {
// 弹出一个菜单
showPopupMenu(v);
}
});
}
private void showPopupMenu(View v) {
final Object tag = v.getTag(); // 得到我们存储在v中的数据
PopupMenu popupMenu = new PopupMenu(MainActivity.this, v);
popupMenu.getMenuInflater().inflate(R.menu.popup_menu, popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
mFruitList.remove(tag); // 从mFruitList中移除
mAdapter.notifyDataSetChanged(); // 提醒适配器数据改变,要求刷新列表
return true;
}
});
popupMenu.show(); // 显示弹出菜单
}
}
图片资源
ic_grape.pngic_grapefruit.png
ic_litchi.png
ic_loquat.png
ic_mango.png
ic_mulberry.png
ic_orange.png
ic_other_banana.png
ic_peach.png
ic_pineapple.png
ic_plum.png
ic_strawberry.png
ic_yangtao.png
ic_apple.png
ic_apricot.png
ic_banana.png
ic_blueberry.png
ic_cherry.png
ic_chestnut.png
ic_dragonfruit.png
网友评论