標籤:state .com .post imp odi private emc style 通過
樣本顯示如何使用v7 appcompat庫中的PopupMenu顯示彈出式菜單。主介面使用V4支援庫的ListFragment顯示資料列表,當點擊列表子項時,在子項下方彈出下拉式功能表,並通過設定setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener())監聽菜單點擊事件,下拉式功能表只有一個選項,用於移除該列表子項。
Demo:https://github.com/googlesamples/android-ActionBarCompat-ListPopupMenu/#readme
PopupMenu API:https://developer.android.google.cn/reference/android/support/v7/widget/PopupMenu
首先看看樣本的運行介面:
Demo關鍵代碼在Fragment中,首頁面MainActiviy只是包含了Fragment,這裡就不貼出來了:
public class PopupFragment extends ListFragment implements View.OnClickListener { @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); // 建立頁面列表 ArrayList<String> items = new ArrayList<>(); for (int i = 0; i < Cheeses.CHEESES.length; i++) { items.add(Cheeses.CHEESES[i]); } // 設定Adapter setListAdapter(new PopupAdapter(items)); } @Override public void onListItemClick(ListView l, View v, int position, long id) { String item = (String) l.getItemAtPosition(position); Toast.makeText(getActivity(), "Item Clicked: " + item, Toast.LENGTH_SHORT).show(); } @Override public void onClick(final View v) { // 為確保下拉式功能表彈出位置的正確,需要傳遞一個Runnable對象。因為下拉式功能表顯示前,View的位置可能不一樣(列表子項滾動位置會變)。 v.post(new Runnable() { @Override public void run() { showPopupMenu(v); } }); } private void showPopupMenu(View view) { final PopupAdapter adapter = (PopupAdapter) getListAdapter(); // 獲得發生點擊事件View的Tag final String item = (String) view.getTag(); // 建立一個PopupMenu,並把PopupMenu綁定到對應的view PopupMenu popup = new PopupMenu(getActivity(), view); // 為PopupMenu綁定布局檔案 popup.getMenuInflater().inflate(R.menu.popup, popup.getMenu()); // 為下拉式功能表設定監聽 popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem menuItem) { switch (menuItem.getItemId()) { case R.id.menu_remove: // 把對應列表子項移除 adapter.remove(item); return true; } return false; } }); // 顯示下拉式功能表 popup.show(); } class PopupAdapter extends ArrayAdapter { public PopupAdapter(ArrayList<String> items) { super(getActivity(), R.layout.list_item, android.R.id.text1, items); } @NonNull @Override public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { View view = super.getView(position, convertView, parent); View popupButton = view.findViewById(R.id.button_popup); popupButton.setTag(getItem(position)); popupButton.setOnClickListener(PopupFragment.this); return view; } }}
下拉式功能表布局檔案popup.xml
<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/menu_remove" android:title="remove" /></menu>
列表子項布局檔案list_item.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="?attr/listPreferredItemHeight" android:orientation="horizontal"> <TextView android:id="@android:id/text1" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:ellipsize="end" android:gravity="center_vertical" android:maxLines="1" android:paddingLeft="8dp" android:paddingRight="8dp" android:textAppearance="?android:attr/textAppearanceMedium" /> <ImageView android:id="@+id/button_popup" android:layout_width="56dip" android:layout_height="match_parent" android:background="?attr/selectableItemBackground" android:src="@mipmap/ic_action_settings" /></LinearLayout>
Android Demo Android ActionBarCompat-ListPopupMenu