onContextItemSelected 未在 Fragment 中调用

onContextItemSelected is not called in Fragment

我正在尝试在 ListView 中使用长按弹出菜单。我可以调用弹出菜单,但是当我点击这个弹出菜单的选项时,onContextItemSelected() 不起作用。

这是我的代码:

package com.pa.homeautomationblank.fragments;

import android.app.Fragment;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.pa.homeautomationblank.R;
import com.pa.homeautomationblank.listeners.OnItemCentraisLongClickListener;
import com.pa.homeautomationblank.listeners.OnReconhecerCentraisClickListener;
import com.pa.homeautomationblank.model.Central;
import com.pa.homeautomationblank.model.dao.CentralDAO;

public class CentraisFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View view = inflater.inflate(R.layout.fragment_centrais, container,false);

        RelativeLayout rLay = (RelativeLayout) view.findViewById(R.id.layout_centrais_bottom);
        Button btnReconhecerCentrais = (Button) rLay.findViewById(R.id.btnReconhecerCentrais);

        btnReconhecerCentrais.setOnClickListener(new OnReconhecerCentraisClickListener());

        CentralDAO cDAO = new CentralDAO(view.getContext());
        ArrayAdapter<Central> adapter = new ArrayAdapter<Central>(view.getContext(), android.R.layout.simple_list_item_1 , cDAO.selectAll());

        rLay = (RelativeLayout) view.findViewById(R.id.layout_centrais_conteudo);
        ListView lv = (ListView) rLay.findViewById(R.id.lvCentrais);
        lv.setAdapter(adapter);
        lv.setOnItemLongClickListener(new OnItemCentraisLongClickListener());
        registerForContextMenu(lv);

        return (view);
    }

    @Override   
    public void onCreateContextMenu(ContextMenu menu, View v,  ContextMenu.ContextMenuInfo menuInfo){
        super.onCreateContextMenu(menu, v, menuInfo);
        if (v.getId()==R.id.lvCentrais){
            menu.setHeaderTitle(R.string.operacoes);
            String[] menuitems = getResources().getStringArray(R.array.menu_acoes_central);
            for (int i=0;i<menuitems.length;i++){
                menu.add(Menu.NONE,i,i,menuitems[i]);
            }
        }
    }

    @Override
    public boolean onContextItemSelected(MenuItem item){

        int menuindex = item.getItemId();

        switch (menuindex) {
        case 0:
                Toast.makeText(getView().getContext(), "Rename", Toast.LENGTH_SHORT).show();
            break;
        case 1:
            Toast.makeText(getView().getContext(), "Remove", Toast.LENGTH_SHORT).show();
            break;

        default:
            Toast.makeText(getView().getContext(), "invalid option!", Toast.LENGTH_SHORT).show();
            break;
        }


        return super.onContextItemSelected(item);
    }

}

我在方法中设置了断点,但没有调用事件。

提前致谢!

我解决了我的问题。

我在 onCreateContextMenu 方法内部将此方法的实现:onContextItemSelected 更改为 setOnMenuItemClickListener,如下代码:

@Override   
    public void onCreateContextMenu(ContextMenu menu, View v,  ContextMenu.ContextMenuInfo menuInfo){
        super.onCreateContextMenu(menu, v, menuInfo);
        if (v.getId()==R.id.lvCentrais){
            menu.setHeaderTitle(R.string.operacoes);
            String[] menuitems = getResources().getStringArray(R.array.menu_acoes_central);
            for (int i=0;i<menuitems.length;i++){
                menu.add(Menu.NONE,i,i,menuitems[i]);
            }
            for (int i=0;i<menu.size();i++){
                menu.getItem(i).setOnMenuItemClickListener(new OnMenuItemClickListener() {

                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        int menuindex = item.getItemId();

                        switch (menuindex) {
                        case 0:
                                Toast.makeText(getView().getContext(), "Rename", Toast.LENGTH_SHORT).show();
                            break;
                        case 1:
                            Toast.makeText(getView().getContext(), "Remove", Toast.LENGTH_SHORT).show();
                            break;

                        default:
                            Toast.makeText(getView().getContext(), "invalid option!", Toast.LENGTH_SHORT).show();
                            break;
                        }
                        return false;
                    }
                });
            }
        }
    }

感谢大家的帮助!

对于仍在寻找解决方法的任何人,一旦我通过创建一个匿名 OnMenuItemClickListener 委托回 onContextItemSelected(MenuItem item) 并将其设置在我菜单中的所有项目上来解决这个问题。

@Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
    // Creation/inflate menu here
    
    OnMenuItemClickListener listener = new OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            onContextItemSelected(item);
            return true;
        }
    };
    
    for (int i = 0, n = menu.size(); i < n; i++)
        menu.getItem(i).setOnMenuItemClickListener(listener);
}