簡體   English   中英

長時間單擊listView中的行中的特定項時,獲取ContextMenu

[英]Get ContextMenu when long click on specific item in row in listView

我使用基本適配器填充列表視圖,此列表的每一行都有3 ImageView

我想顯示一個ContextMenu並在長按其中一張圖像時獲得圖像位置(不是通過單擊列表行,而是單擊圖像本身),所以我試圖在Activity中的listview行充氣並獲得圖像然后將該圖像注冊為contextMenu([registerForContextMenu(imgLeft)])但沒有任何效果。

我本想onLongClickListener中的每個圖像上調用事件onLongClickListener ,但是我有一個問題,即如何在方法getView ()中的baseAdapter (此處沒有Activity)中調用(顯示打開) contextMenu 或如何在baseAdaptercontextMenu注冊這些圖像...?

我在這里和Google中搜索了很多解決方案,卻一無所獲。

為每個ImageView注冊ContextMenu可能會起作用。 像這樣:

@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
    if (arg1 == null) {
        LayoutInflater vi = (LayoutInflater) cmx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        arg1 = vi.inflate(R.layout.services_item, null);
    }
    ImageView serviceThumb = (ImageView) arg1.findViewById(R.id.thumbIView);

    registerForContextMenu(serviceThumb);

    serviceThumb.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {

        @Override
        public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
            AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
            menu.setHeaderTitle("Test");
            for (int i = 0; i< 5; i++) {
                menu.add(Menu.NONE, i, i, i + " - Test");
            }
    }
    });
}

暫無
暫無

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

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