android 動態contextmenu,在Android中使用ContextMenu與ListView

要從選定的ListView項中獲取該項,請參考ContextMenuInfo對象(請參見下面的最后一個實現方法)。完整解決方案如下:

1)在ListActivity類中為上下文菜單注冊ListView

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

// ...

getListView().setAdapter(mAdapter);

registerForContextMenu(getListView());

}

1a)如果列表上有復雜的視圖,則可能需要在Adapter類中的每個列表視圖上啟用長按

@Override

public View getView(int position, View convertView, ViewGroup parent) {

View view = convertView;

if (view == null) {

RelativeLayout layout = (RelativeLayout) LayoutInflater.from(mContext).inflate(R.layout.list_item, parent, false);

itemLayout = layout;

itemLayout.setLongClickable(true);

}

// ...

return view;

}

2)在ListActivity類中實現onCreateContextMenu()和onContextItemSelected()

@Override

public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {

super.onCreateContextMenu(menu, v, menuInfo);

AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;

String title = ((MyItem) mAdapter.getItem(info.position)).getTitle();

menu.setHeaderTitle(title);

menu.add(Menu.NONE, MENU_CONTEXT_DELETE_ID, Menu.NONE, DELETE_TEXT);

}

@Override

public boolean onContextItemSelected(MenuItem item) {

switch (item.getItemId()) {

case MENU_CONTEXT_DELETE_ID:

AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();

Log.d(TAG, "removing item pos=" + info.position);

mAdapter.remove(info.position);

return true;

default:

return super.onContextItemSelected(item);

}

}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/540844.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/540844.shtml
英文地址,請注明出處:http://en.pswp.cn/news/540844.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

《Android應用開發攻略》——2.2 異常處理

2.2 異常處理 Ian Darwin2.2.1 問題Java有一個精心定義的異常處理機制,但是需要花費一定的時間學習,才能高效地使用而不至于使用戶或者技術支持人員感到沮喪。2.2.2 解決方案Java提供了一個Exception層次結構,正確地使用它能夠帶來相當大的靈…

android 默認瀏覽器 視頻播放 二維碼,Android調用系統默認瀏覽器訪問的方法

一、啟動android默認瀏覽器這樣子,android就可以調用起手機默認的瀏覽器訪問。二、指定相應的瀏覽器訪問1、指定android自帶的瀏覽器訪問( “com.android.browser”:packagename ;“com.android.browser.BrowserActivity”:啟動主…

請寫出3個Android布局,一起擼一波干貨集中營練練手Android(三)布局+實現篇

MPGankIO 布局篇整個App到了這里就開始準備開始實現邏輯啦,有沒有點小期待后續如果有需要可以爬一波包包通緝令的數據O(∩_∩)O~~我們的布局采用5.0之后的新布局控件1.CardViewCardView特別的屬性如下:android:cardCornerRadius:在布局中設置…

小米凈水器壓力傳感器_凈水器中RO的完整形式是什么?

小米凈水器壓力傳感器RO:反滲透 (RO: Reverse Osmosis) RO is an abbreviation of Reverse Osmosis. It is a course of action that aids the RO water purifier to banish unfavorable ions, dissolved solids, and TDS from the water. Reverse osmosis is the c…

即時通訊應用戰爭開打,到底誰能最終定義我們的交流方式?

題圖:風靡亞洲的Line 北京時間4月4日消息,據科技網站TechRadar報道,對業界來說,即時通訊應用是一個巨大的市場,除了專門發力該領域的公司,專注搜索的谷歌和專注社交的Facebook最近幾年也都開始深耕此類應用…

離散點自動生成等高線_有限自動機| 離散數學

離散點自動生成等高線有限狀態機 (Finite state machine) A finite state machine (FSM) is similar to a finite state automation (FSA) except that the finite state machine "prints" an output using an output alphabet distinct from the input alphabet. Th…

android點擊加號,Android仿微信朋友圈點擊加號添加圖片功能

本文為大家分享了類似微信朋友圈,點擊號圖片,可以加圖片功能,供大家參考,具體內容如下xml:xmlns:app"http://schemas.android.com/apk/res-auto"android:layout_width"match_parent"android:layout_height&qu…

AI 創業公司 Kyndi 獲850萬美元融資,幫助公司預測未來

雷鋒網(公眾號:雷鋒網)8月10日消息,據外媒報道, Kyndi 是一家總部位于帕洛阿爾托的 AI 創業公司。該公司今天宣布,已經完成了850萬美元的 B 輪融資。 本輪融資的資金來源包括 PivotNorth Capital,Darling Ventures 和 …

css max-width_CSS中的max-width屬性

css max-widthCSS | 最大寬度屬性 (CSS | max-width property) The max-width property is used to help in setting the width of an element to the maximum. Although if the element or content is already larger than the maximum width then the height of that content…

20個編寫現代CSS代碼的建議

本文翻譯自Danny Markov 的20-Tips-For-Writing-Modern-CSS一文。 本文歸納于筆者的Web Frontend Introduction And Best Practices:前端入門與最佳實踐中CSS入門與最佳實踐系列,其他的關于CSS樣式指南的還有提升你的CSS姿勢、Facebook里是怎樣提升CSS代碼質量的。本…

android package.xml,Android自動化編譯設置AndroidManifest.xml中package值(包名)

手動修改Android的AndroidManifest.xml中package值(包名)很簡單,手動修改即可。但是項目中需要把Android的項目源代碼放到服務器端在客戶下載時候動態編譯生成,且生成的app簽名相同但包名不同(若此時包名相同就是相同的app),這種需求需要在服…

css 相同的css屬性_CSS中的order屬性

css 相同的css屬性CSS | 訂單屬性 (CSS | order Property) Introduction: 介紹: Web development is an ever-growing field that would never find its end, therefore it is equally necessary to learn new ways to deal with the elements of the web page or …

StoreServ的ASIC架構師必須面向未來做出決斷

StoreServ陣列采用特殊硬件,即一套ASIC來加速存儲陣列操作,而且其每代陣列都會在這方面進行重新設計。目前的設計為第五代。 作為惠普企業業務公司研究員兼StoreServ架構師,Siamak Nazari當下主要負責第六代ASIC的設計工作。 每代ASIC設計往往…

android網頁省略分頁器,Android輕量級網頁風格分頁器

博客同步自:個人博客主頁輕量級仿網頁風格分頁器,和RecycleView封裝一起配合使用,也可單獨使用,喜歡就star、fork下吧~謝謝目錄功能介紹效果圖如何引入簡單使用依賴github地址功能介紹支持延遲加載分頁支持單獨分頁器組件使用&…

scala重載無參構造方法_Scala中的無參數方法

scala重載無參構造方法Scala無參數方法 (Scala parameterless method) A method which accepts no parameters from the calling code. It also denotes that there will not be any empty parentheses. These are special types of methods in Scala that are initialized and…

傳統存儲做到極致也驚人!看宏杉科技發布的CloudSAN

傳統存儲陣列首先考慮的是高可靠、高性能。那么在成本上、擴展上、部署上就差。 互聯網企業帶來分布式存儲,擴展上、部署上是優勢了,但是單節點的可靠性差、數據一致性差、IO延遲大、空間浪費嚴重,能耗大。 這兩者的問題,我想很多…

android inflate,Android 關于inflate

通俗的說,inflate就相當于將一個xml中定義的布局找出來.因為在一個Activity里如果直接用findViewById()的話,對應的是setConentView()的那個layout里的組件.因此如果你的Activity里如果用到別的layout,比如對話框上的layout,你還要設置對話框上的layout里的組件(像圖片ImageVie…

keil lic_LIC的完整形式是什么?

keil licLIC:印度人壽保險公司 (LIC: Life Insurance Corporation of India) LIC is an abbreviation of the Life Insurance Corporation of India. It is a public segment insurance and investment group corporation in India that generally deals with life …

“云”上存儲初顯規模 如何架構是關鍵

在安防系統中,存儲設備只是給數據提供存儲空間,數據存儲的意義更多是為了給上層應用提供二次挖掘。目前的智能分析、大數據、圖幀等技術都是基于數據存儲做的數據挖掘。為了將二次挖掘應用的性能提升到最高,在優化分析算法的同時,…

在線圖片轉成html,在線將JPEG 轉換成HTML。 免費將.jpeg 轉換成.html。

描述|介紹JPEG – is a popular graphic format, which is characterized by a high degree of compression, which leads to a decrease in image quality. It uses the technology of encoding of smooth color renditions, providing the ability to reduce the amount of d…