ContextMenu没有显示

时间:2013-10-18 09:01:36

标签: android contextmenu

我有这个方法:

void showAllSongsMenu() {
    if (rebuilding) {
        Toast.makeText(MusicPlayerActivity.this,
                "Database rebuild in progress, please wait!",
                Toast.LENGTH_SHORT).show();
        return;
    }
    LayoutInflater layoutInflater = (LayoutInflater) getBaseContext()
            .getSystemService(LAYOUT_INFLATER_SERVICE);
    final View popupView = layoutInflater.inflate(R.layout.allsongs_list,
            null);
    allSongs = new PopupWindow(popupView, LayoutParams.FILL_PARENT,
            LayoutParams.FILL_PARENT, true);
    Button allSongsMenu = (Button) allSongs.getContentView().findViewById(
            R.id.close_all_songs_menu);
    allSongs.setBackgroundDrawable(MusicPlayerActivity.this.getResources()
            .getDrawable(R.drawable.unknown_artist));
    allSongs.setFocusable(true);
    allSongsMenu.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            allSongs.dismiss();
        }

    });
    ListView lv = (ListView) allSongs.getContentView().findViewById(
            R.id.all_songs_list);
    //registerForContextMenu(lv);
    lv.setOnCreateContextMenuListener(new OnCreateContextMenuListener(){

        @Override
        public void onCreateContextMenu(ContextMenu menu, View v,
                ContextMenuInfo menuInfo) {
            menu.setHeaderTitle("Song options");
            menu.add(0, v.getId(), 0, "enqueue song");
            menu.add(0, v.getId(), 0, "song info");
            menu.add(0, v.getId(), 0, "cancel action");
        }});
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            currentTrack = arg2;
            loadTrack();
            if (isTuning)
                if (track != null)
                    track.pause();
            isTuning = true;
            btnPlay.setBackgroundResource(R.drawable.pause);
            playTrack();
            allSongs.dismiss();
        }

    });
    base.getAllData();
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            R.layout.simple_row, base.getNames());
    lv.setAdapter(adapter);
    allSongs.showAtLocation(trackInfo, Gravity.CENTER, 0, -30);
}

未显示ContextMenu。我也有onContextItemSelected已实现,但它不起作用,因为菜单未显示。每当我需要这个PopupWindow时调用此方法,并从OptionsMenu调用它作为选项之一。我还尝试在没有监听器的情况下为上下文菜单事件注册listview,但它也没有用。

2 个答案:

答案 0 :(得分:4)

我相信您必须使用

注册上下文菜单的列表视图
registerForContextMenu(lv);

你注释掉了

编辑:

您还缺少打开上下文菜单的代码:

openContextMenu(view);

应放在某些事件上,例如:按钮单击

EDIT2:

你实际上可以从另一个对话框片段开始一个对话片段,这将简化你的编码。示例如下:

活动代码:

public class MyActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Button button = new Button(this);
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                new Test1().show(getSupportFragmentManager(),"bla");
            }
        });
        setContentView(button);
    }

}

第一个对话框片段:

public class Test1 extends DialogFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        Button button = new Button(getActivity());
        button.setText("test1");
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                new Test2().show(getActivity().getSupportFragmentManager(),"bla");
            }
        });
        return button;
    }

}

第二个对话框片段:

public class Test2 extends DialogFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        Button button = new Button(getActivity());
        button.setText("test2");
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        return button;
    }
}

答案 1 :(得分:0)

这件事的问题在于我想在PoupWindow中为ListView启动ContextMenu。这也是让我在其他方面遇到问题的原因。有没有办法让我在活动的主窗口上显示ListView(甚至可以切换活动的主视图,直到我用列表完成操作),而不是用于弹出窗口?

这是我的对话

package rs.ac.bg.etf.musicplayer.dialogs;

import android.app.Dialog;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import rs.ac.bg.etf.musicplayer.*;
import rs.ac.bg.etf.musicplayer.database.MusicDatabase;

public class AllSongsDialog extends Dialog implements OnClickListener {
MusicPlayerActivity act;
Button cancel;
ListView lv;

public AllSongsDialog(MusicPlayerActivity act) {
    super(act);
    this.act = act;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.dialog_allsongs);
    cancel = (Button) findViewById(R.id.close_allsongs_dialog);
    cancel.setOnClickListener(this);
    lv = (ListView) findViewById(R.id.all_songs_list);
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View v, int pos,
                long arg3) {
            act.setCurrentTrack(pos);
            act.loadTrack();
            if (act.isTuning())
                if (act.getTrack() != null)
                    act.getTrack().pause();
            act.setTuning(true);
            act.getBtnPlay().setBackgroundResource(R.drawable.pause);
            act.playTrack();
            dismiss();
        }

    });
    MusicDatabase base = act.getBase();
    base.getAllData();
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(
            this.getContext(), R.layout.simple_row, base.getNames());
    lv.setAdapter(adapter);
    registerForContextMenu(lv);
    setCancelable(true);
}

@Override
public void onClick(View v) {
    dismiss();
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = act.getMenuInflater();
    inflater.inflate(R.layout.context_menu, menu);
}

@SuppressWarnings("deprecation")
@Override
public boolean onContextItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.enqueue_song:
        Toast.makeText(getContext(), "Song enqueued!", Toast.LENGTH_SHORT)
                .show();
        return true;
    case R.id.song_info:

        return true;
    case R.id.cancel_menu:
        return true;
    }
    return false;
}

}