Android 彈出菜單示例#
這個(gè)示例展示如何使用PopupMenu來現(xiàn)實(shí)和一個(gè)彈出的菜單分俯。
說明
這個(gè)示例展示一個(gè)水果的列表,每一個(gè)列表項(xiàng)均可以點(diǎn)擊,每一個(gè)列表項(xiàng)的右邊有一個(gè)圖片按鈕闲孤,點(diǎn)擊該按鈕,將彈出一個(gè)菜單烤礁。
這個(gè)示例運(yùn)行在Android4.0以上讼积,不支持Android4.0以下版本!
運(yùn)行效果
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"/>
水果列表中每一個(gè)列表項(xiàng)的布局
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">設(shè)置</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/你的應(yīng)用的包名/Fruits.java
package com.ddzj.listpopupmenu;
/*
* 數(shù)據(jù)源
*/
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={"蘋果","杏子","香蕉","藍(lán)莓","櫻桃","栗子",
"火龍果","葡萄","柚子","荔枝","枇杷","芒果","桑椹","橘子","芭蕉",
"桃子","鳳梨","李子","草莓","獼猴桃"};
}
src/你的應(yīng)用的包名/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; // 數(shù)據(jù)與ListView之間的連接橋梁
private List<Map<String, Object>> mFruitList ; // 存儲數(shù)據(jù)的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>>();
// 將數(shù)據(jù)添加到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);
}
/*
* 創(chuàng)建適配器
*/
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);
}
/*
* 當(dāng)列表中的項(xiàng)被點(diǎn)擊時(shí)脚仔,回調(diào)此方法
*/
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// 彈出吐司勤众,告訴用戶點(diǎn)擊的水果名稱
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)鲤脏,在數(shù)據(jù)集中得到與指定位置對應(yīng)的數(shù)據(jù)項(xiàng)们颜。即mFruitList中對應(yīng)位置的數(shù)據(jù)
// setTag(getItem(position));這里的是將getItem(position)返回的數(shù)據(jù)存儲在popupButton中,
// 以便后面可以通過getTag()得到該數(shù)據(jù)并將其從mFruitList中移除
popupButton.setTag(getItem(position));
return view;
}
}
/*
* 當(dāng)列表項(xiàng)中最右邊的向下按鈕被點(diǎn)擊時(shí)猎醇,回調(diào)此方法
*/
@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() {
// 彈出一個(gè)菜單
showPopupMenu(v);
}
});
}
private void showPopupMenu(View v) {
final Object tag = v.getTag(); // 得到我們存儲在v中的數(shù)據(jù)
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(); // 提醒適配器數(shù)據(jù)改變窥突,要求刷新列表
return true;
}
});
popupMenu.show(); // 顯示彈出菜單
}
}
圖片資源
ic_grape.png
ic_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