How to open Menu Context Android with click button in listview adapter?

眉间皱痕 提交于 2020-01-12 03:49:06

问题


How to open Menu Context Android with click button in listview adapter ?

I tried with my code, but not show the menu context,

code

 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");
}

can you tell me, how it should work ?


回答1:


Use like this:

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);
}

Output:

Hope this will work for you.




回答2:


The accepted answer is really not optimal - it may simply be dated.

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);

Alternatively you can show the context menu in a specific location like so:

button.setOnClickListener(view -> view.showContextMenu(x, y));


来源:https://stackoverflow.com/questions/17419357/how-to-open-menu-context-android-with-click-button-in-listview-adapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!