簡體   English   中英

列表視圖項的Android工具欄上下文菜單

[英]Android toolbar context menu for listview items

我正在尋找有關如何在ListView項目的工具欄中實現上下文菜單的幫助,如WhatsApp所做的那樣。 到目前為止,我發現的唯一教程是關於彈出的對話框,這不是我想要的。 有人可以幫助我或提供教程鏈接嗎? 謝謝 :)

檢查這個官方的android指南。

編輯:

使用上下文操作模式

對於提供上下文操作的視圖,通常應該在兩個事件(或兩者)之一上調用上下文操作模式:

  • 用戶在視圖上執行長按。
  • 用戶在視圖中選擇復選框或類似的UI組件。

應用程序如何調用上下文操作模式並定義每個操作的行為取決於您的設計。 基本上有兩種設計:

  • 對於單個任意視圖的上下文操作。
  • 對於ListView或GridView中的項目組的批處理上下文操作(允許用戶選擇多個項目並對它們執行所有操作)。

為各個視圖啟用上下文操作模式

  1. 實現ActionMode.Callback接口。 在其回調方法中,您可以指定上下文操作欄的操作,響應操作項上的單擊事件,以及處理操作模式的其他生命周期事件。

     private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() { // Called when the action mode is created; startActionMode() was called @Override public boolean onCreateActionMode(ActionMode mode, Menu menu) { // Inflate a menu resource providing context menu items MenuInflater inflater = mode.getMenuInflater(); inflater.inflate(R.menu.context_menu, menu); return true; } // Called each time the action mode is shown. Always called after onCreateActionMode, but // may be called multiple times if the mode is invalidated. @Override public boolean onPrepareActionMode(ActionMode mode, Menu menu) { return false; // Return false if nothing is done } // Called when the user selects a contextual menu item @Override public boolean onActionItemClicked(ActionMode mode, MenuItem item) { switch (item.getItemId()) { case R.id.menu_share: shareCurrentItem(); mode.finish(); // Action picked, so close the CAB return true; default: return false; } } // Called when the user exits the action mode @Override public void onDestroyActionMode(ActionMode mode) { mActionMode = null; } }; 
  2. 要顯示欄時調用startActionMode()(例如,當用戶長按視圖時)。

     someView.setOnLongClickListener(new View.OnLongClickListener() { // Called when the user long-clicks on someView public boolean onLongClick(View view) { if (mActionMode != null) { return false; } // Start the CAB using the ActionMode.Callback defined above mActionMode = getActivity().startActionMode(mActionModeCallback); view.setSelected(true); return true; } }); 

在ListView或GridView中啟用批處理上下文操作

如果ListView或GridView(或AbsListView的另一個擴展)中有一組項目,並且希望允許用戶執行批處理操作,則應該:

  • 實現AbsListView.MultiChoiceModeListener接口,並使用setMultiChoiceModeListener()為視圖組設置它。 在偵聽器的回調方法中,您可以指定上下文操作欄的操作,響應操作項上的單擊事件,以及處理從ActionMode.Callback接口繼承的其他回調。
  • 使用CHOICE_MODE_MULTIPLE_MODAL參數調用setChoiceMode()。

     ListView listView = getListView(); listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL); listView.setMultiChoiceModeListener(new MultiChoiceModeListener() { @Override public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) { // Here you can do something when items are selected/de-selected, // such as update the title in the CAB } @Override public boolean onActionItemClicked(ActionMode mode, MenuItem item) { // Respond to clicks on the actions in the CAB switch (item.getItemId()) { case R.id.menu_delete: deleteSelectedItems(); mode.finish(); // Action picked, so close the CAB return true; default: return false; } } @Override public boolean onCreateActionMode(ActionMode mode, Menu menu) { // Inflate the menu for the CAB MenuInflater inflater = mode.getMenuInflater(); inflater.inflate(R.menu.context, menu); return true; } @Override public void onDestroyActionMode(ActionMode mode) { // Here you can make any necessary updates to the activity when // the CAB is removed. By default, selected items are deselected/unchecked. } @Override public boolean onPrepareActionMode(ActionMode mode, Menu menu) { // Here you can perform updates to the CAB due to // an invalidate() request return false; } }); 

有關更多菜單功能, 請查看此鏈接。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM