Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a context menu on the click of an element inside a listview item

Tags:

android

enter image description here

Clicking the more icon (3 vertical dots anchored to the right of the list item) opens up a context menu in Google Music:

enter image description here

I'm trying to recreate this with what I'm guessing is a context menu. Documentation says:

If your activity uses a ListView or GridView and you want each item to provide the same context menu, register all items for a context menu by passing the ListView or GridView to registerForContextMenu().

But I still want the list item itself clickable. I just want a context menu to show up when the user clicks on the more icon like it does in Google Music.

So I tried this:

@Override
public void onMoreClicked(ArtistsListItem item, int position, View imageButton) {       
     registerForContextMenu(imageButton);
}

onMoreClicked is just part of a custom listener I made to receive onClick callbacks from the list's adapter.

registerForContextMenu is called, but the fragment's onCreateContextMenu method is never invoked:

@Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo info) { //this method is never called
    super.onCreateContextMenu(menu, view, info);

    android.view.MenuInflater inflater = mActivity.getMenuInflater();
    inflater.inflate(R.menu.artist_list_menu, menu);
}

I ran some breakpoints to check if it was running but it never did. I did the same with the activity's onCreateContextMenu (the registerForContextMenu's class is the fragment, but just to be sure I did it that way) and no dice either.

I'm using ActionBarSherlock, I don't know if that makes a difference but I guess it's worth noting.

Does anyone have an idea what is going on here?

like image 578
JMRboosties Avatar asked Nov 02 '22 12:11

JMRboosties


1 Answers

I had a similar issue, this is what I ended up doing:
In the listAdapter.getItem I attach a listener to each sub view:

private Fragment mParentFragment;

public FriendListAdapter(Fragment fragment, ...) {
    . . .
    this.mParentFragment = fragment;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if( convertView == null ){
        convertView = LayoutInflater.from(mContext).inflate(R.layout.friend_layout, parent, false);
    }
    ViewGroup viewGroup = (ViewGroup) convertView;
    assert viewGroup != null;

    *
    *
    *

    Button viewMessageButton = (Button) viewGroup.findViewById(R.id.b_send_message);
    viewMessageButton.setTag(friend.getHandle());
    viewMessageButton.setOnClickListener(mParentFragment);
    return viewGroup;
}

and in the Fragment I just listen to onClick events:

public class FriendListFragment extends Fragment implements View.OnClickListener {
    @Override
    public void onClick(View view) {
        dialog.show(getActivity().getSupportFragmentManager(), SEND_MESSAGE_DIALOG);
    }
}

Or you can use PopupMenu from support v7 Which you can install in Android studio without gradle from Project Structure window.

Choose Modules on the left, then Import Library navigate to ..../android-studio/sdk/extras/android/support/v7/appcompat then in you main project module add appcompat as a module dependency.

like image 166
nana Avatar answered Nov 08 '22 09:11

nana