我有这个方法:
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);
}
上下文菜单未显示。我也实现了 onContextItemSelected 但它不起作用,因为没有显示菜单。每当我需要此 PopupWindow 时都会调用此方法,并从 OptionsMenu 作为选项之一调用它。我还尝试为没有侦听器的上下文菜单事件注册列表视图,但它也不起作用。