registerForContextMenu

registerForContextMenu

首先,我的英语不太好。
我有一个ListViewlike this和一个TextView的。
我的问题是我想把acontextMenu放在那里,但是我不能。我为此浪费了很多时间,找不到解决办法。
我用过registerForContextMenu(listViewTotes),用过onCreateContextMenuonContextItemSelected
谢谢!

最佳答案

我建议您使用onTimeLongClickListener()。看起来,OnEmClickListener()没有响应RegisterForContextMenu(arg0)。
对于使用onTimeLongClickListener()示例,代码如下:

public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.activity_main);

    listView = (ListView) findViewById(R.id.my_list);
    adapter = new MyAdapter(this,getModel());
    listView.setAdapter(adapter);
    listView.setOnItemLongClickListener(new PlayListOnItemLongClickListener());
}

private class PlayListOnItemLongClickListener implements AdapterView.OnItemLongClickListener {
    public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        registerForContextMenu(arg0);

        return false;
    }
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    getMenuInflater().inflate(R.menu.context_playlist_operation, menu);
    menu.setHeaderIcon(R.drawable.ic_launcher);
    menu.setHeaderTitle("What do you want to do");
}

in/listviewtest/res/menu/context_playlist_operation.xml中
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <group>
        <item
            android:id="@+id/context_playlist_remove_playlist"
            android:title="@string/app_name"
            />
    </group>
</menu>

07-28 02:18