8

如何使用列表视图适配器中的单击按钮打开菜单上下文 Android?

我尝试了我的代码,但没有显示菜单上下文,

代码

 public View getView(int position, View convertView, ViewGroup parent) {
      vi=convertView;
    if(convertView==null)
        vi = inflater.inflate(R.layout.tulisan_komentar_list_item,parent, false);
    LinearLayout content_favorite= (LinearLayout)vi.findViewById(R.id.content_favorite);
    final TextView date_komentar = (TextView)vi.findViewById(R.id.date_komentar); // artist name
    final TextView isi_komentar = (TextView)vi.findViewById(R.id.isi_komentar); // duration
    final TextView nama_komentar = (TextView)vi.findViewById(R.id.nama_komentar); // duration
    final TextView id_tulisan_komentar = (TextView)vi.findViewById(R.id.id_tulisan_komentar); // duration
    final ImageButton act_komentar = (ImageButton)vi.findViewById(R.id.act_komentar);
    ImageView thumb_image=(ImageView)vi.findViewById(R.id.avatar_komentar); // thumb image

    HashMap<String, String> tulisan = new HashMap<String, String>();
    tulisan = data.get(position);

    // Setting all values in listview
    date_komentar.setText(tulisan.get(ContentCommentActivity.TAG_DATE_KOMENTAR));
    isi_komentar.setText(tulisan.get(ContentCommentActivity.TAG_ISI_KOMENTAR));
    nama_komentar.setText(tulisan.get(ContentCommentActivity.TAG_NAMA_KOMENTAR));
    id_tulisan_komentar.setText(tulisan.get(ContentCommentActivity.TAG_ID_TULISAN_KOMENTAR));
    String avatar_komentar = tulisan.get(ContentCommentActivity.TAG_AVATAR_KOMENTAR);

    if(hide_gambar.equals("Y")){
        thumb_image.setVisibility(View.GONE);   
    }

    else{
        thumb_image.setVisibility(View.GONE);  
         /* thumb_image.setVisibility(View.VISIBLE);   
       if (avatar_komentar.equals("")) {
            thumb_image.setVisibility(View.GONE);
        } else {
            imageLoader.DisplayImage(tulisan.get(ContentCommentActivity.TAG_AVATAR_KOMENTAR), thumb_image);
            thumb_image.setVisibility(View.VISIBLE);   

        } */
    }

    activity.registerForContextMenu(act_komentar);

    act_komentar.setOnClickListener(new android.view.View.OnClickListener() 
    {

        public void onClick(View v) 
        {
            activity.openContextMenu(v);
            v.showContextMenu();

        }
    });


    return vi;

}


public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    menu.setHeaderTitle("My Context Menu");
    menu.add(0, 1, 0, "Add");
    menu.add(0, 2, 0, "Edit");
    menu.add(0, 3, 1, "Delete");
}

你能告诉我,它应该如何工作?

4

2 回答 2

20

像这样使用:

act_komentar.setOnClickListener(new android.view.View.OnClickListener() {

    public void onClick(View v) {
        //To register the button with context menu.
        registerForContextMenu(act_komentar);
        openContextMenu(act_komentar);

    }
});

final int CONTEXT_MENU_VIEW = 1;
final int CONTEXT_MENU_EDIT = 2;
final int CONTEXT_MENU_ARCHIVE = 3;
@Override
public void onCreateContextMenu (ContextMenu menu, View
v, ContextMenu.ContextMenuInfo menuInfo){
    //Context menu
    menu.setHeaderTitle("My Context Menu");
    menu.add(Menu.NONE, CONTEXT_MENU_VIEW, Menu.NONE, "Add");
    menu.add(Menu.NONE, CONTEXT_MENU_EDIT, Menu.NONE, "Edit");
    menu.add(Menu.NONE, CONTEXT_MENU_ARCHIVE, Menu.NONE, "Delete");
}

@Override
public boolean onContextItemSelected (MenuItem item){
    // TODO Auto-generated method stub
    switch (item.getItemId()) {
        case CONTEXT_MENU_VIEW: {

        }
        break;
        case CONTEXT_MENU_EDIT: {
            // Edit Action

        }
        break;
        case CONTEXT_MENU_ARCHIVE: {

        }
        break;
    }

    return super.onContextItemSelected(item);
}

输出:

在此处输入图像描述

希望这对你有用。

于 2013-07-02T07:18:51.920 回答
3

公认的答案确实不是最优的——它可能只是过时了。

button.setOnCreateContextMenuListener((menu, v, menuInfo) -> {
    final MenuItem item = menu.add("item-text");
    item.setOnMenuItemClickListener(i -> {
        doWorkOnItemClick();
        return true; // Signifies you have consumed this event, so propogation can stop.
    });
    final MenuItem anotherItem = menu.add("another-item");
    anotherItem.setOnMenuItemClickListener(i -> doOtherWorkOnItemClick());
});
button.setOnClickListener(View::showContextMenu);

或者,您可以在特定位置显示上下文菜单,如下所示:

button.setOnClickListener(view -> view.showContextMenu(x, y));
于 2019-05-11T12:29:08.063 回答