1

我有这个方法:

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 作为选项之一调用它。我还尝试为没有侦听器的上下文菜单事件注册列表视图,但它也不起作用。

4

2 回答 2

6

我相信你必须为上下文菜单注册你的列表视图

registerForContextMenu(lv);

你注释掉的

编辑:

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

openContextMenu(view);

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

编辑2:

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

活动代码:

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;
    }
}
于 2013-10-18T09:06:54.523 回答
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;
}

}

于 2013-10-18T11:29:50.740 回答