2

我在試圖在使用ActionBarSherlock庫時嘗試對ContextMenu進行膨脹時遇到了類型問題,正如您所知,它最終會模仿/實現Android支持庫。如何在使用支持庫時在ListView中提供ContextMenu?

有一個SherlockFragmentActivity它設置佈局內容,並在該內容中存在兩個片段。其中一個片段是SherlockListFragment。在SherlockListFragmentonCreate內,我撥打電話註冊ContextMenu

registerForContextMenu(getListView());

問題試圖膨脹菜單時莖。

listView.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() { 
     @Override 
     public void onCreateContextMenu(ContextMenu contextMenu, View view, ContextMenu.ContextMenuInfo contextMenuInfo) { 
       MenuInflater inflater = getSherlockActivity().getSupportMenuInflater(); 
       inflater.inflate(R.menu.lot_menu, contextMenu); 
      } 
     }); 

我無法呼叫inflate由於該方法的參數中指定的類型,其期望作爲com.actionbarsherlock.view.Menu然而在類型傳遞是android.view.ContextMenu

我似乎錯過了一些東西,因爲在操作欄中充氣Menu是微不足道的,但ContextMenu似乎在使用支持框架時會引起問題。

如何正確註冊以根據需要使用支持框架,然後再膨脹ContextMenu

回答

3

嘗試使用getMenuInflater()而不是getSupportMenuInflater()來充氣到ContextMenu

+0

甚至沒有想過要嘗試......因此,在這種情況下甚至不需要支持框架,因爲根據Android文檔,從API級別1開始,ContextMenu就已經存在了。 –

+0

@AaronMcIver:賓果。傑克僅爲直接或間接與行爲條相關的東西創建了工作內容。所以,如果你想使用動作模式而不是上下文菜單,那麼你需要使用ActionBarSherlock特定的東西。 – CommonsWare

相關問題