簡體   English   中英

單擊時Android無法顯示菜單下拉菜單

[英]Android cannot show menu drop down when click

我想在單擊列表視圖項時顯示菜單列表,我可以獲取對象,但是無法顯示菜單。 你能看看我的代碼嗎? 謝謝!

  public View onCreateView

    (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        mActivity = this.getActivity();

        View root = inflater.inflate(R.layout.orders, container, false);

        adapter = new OrdersItemAdapter(root.getContext(), R.layout.order_row);

        final ListView list = (ListView) root.findViewById(R.id.order_list);

        list.setAdapter(adapter);

        mActivity.registerForContextMenu(list);

        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
               // Toast.makeText(parent.getContext(), "view clicked: " + arr[0], Toast.LENGTH_SHORT).show();
                mActivity.registerForContextMenu(list);
                mActivity.openContextMenu(view);
                view.showContextMenu();
                String str = list.getItemAtPosition(position).toString();
                System.out.println(str);
            }
        });
        refresh();
        return root;
    }
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        this.onCreateContextMenu(menu, v, menuInfo);
        System.out.println("...on create context android.view.MenuItem...");
        menu.add(0, v.getId(), 0, "Action 1");
        menu.add(0, v.getId(), 0, "Action 2");
    }

在onCreateView(...)中調用setHasOptionsMenu(true)

https://developer.android.com/reference/android/app/Fragment.html#setHasOptionsMenu(boolean)

暫無
暫無

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

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