Java 类android.support.v7.widget.PopupMenu.OnMenuItemClickListener 实例源码

项目:Mixtape    文件:RecyclerBodyView.java   
/**
 * Shows a contextual popup menu anchored to the supplied view. Item selections are passed to
 * the presenter.
 *
 * @param anchor
 *      the view to anchor the menu to, not null
 * @param item
 *      the LibraryItem associated with the contextual menu, not null
 */
private void showMenu(final View anchor, final LibraryItem item) {
    checkNotNull(item, "item cannot be null.");
    checkNotNull(anchor, "overflowButton cannot be null.");

    final PopupMenu menu = new PopupMenu(getContext(), anchor);
    menu.inflate(contextualMenuResourceId);
    menu.show();

    // Propagate menu selections back to the presenter
    menu.setOnMenuItemClickListener(new OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(final MenuItem menuItem) {
            for (final MenuItemSelectedListener listener : menuItemSelectedListeners) {
                listener.onContextualMenuItemSelected(RecyclerBodyView.this, item, menuItem);
            }

            return true;
        }
    });
}
项目:-Android-popup-menu--Support-library    文件:MainActivity.java   
private void showPopupMenu(View view, int menu)
{
    PopupMenu popupMenu = new PopupMenu(this, view);
    popupMenu.getMenuInflater().inflate(menu, popupMenu.getMenu()); 
    popupMenu.setOnMenuItemClickListener(new OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item)
        {
            Toast.makeText(MainActivity.this, item.getTitle(), Toast.LENGTH_SHORT).show();
            return true;
        }
    });
    //dont forget to show the menu ;)
    popupMenu.show();
}
项目:Android-Card-Design    文件:CardAdapter.java   
private OnClickListener createPopupMenuClickListener( final CardItem cardItem, final int position) {
    return new OnClickListener() {
        @Override
        public void onClick(View view) {
            PopupMenu menu = new PopupMenu( getContext(), view );
            menu.getMenuInflater().inflate( cardMenuResourceId , menu.getMenu() );
            menu.setOnMenuItemClickListener( new OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem menuItem) {
                    return onCardMenuItemClickListener.onCardMenuItemClick(cardItem, menuItem, position);
                }
            } );
            menu.show();
        }           
    };
}
项目:Harmony-Music-Player    文件:SongListFragment.java   
public void showMenu(final int position, View v) {
    PopupMenu popup = new PopupMenu(getActivity(), v);
    MenuInflater inflater = popup.getMenuInflater();
    final Song song = mAdapter.getItem(position);
    inflater.inflate(R.menu.song_list_item, popup.getMenu());
    popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            switch (item.getItemId()) {
                case R.id.action_add_to_queue:
                    ((MainActivity) getActivity()).addToQueue(song);
                    return true;
                case R.id.action_set_as_next_track:
                    ((MainActivity) getActivity()).setAsNextTrack(song);
                    return true;
                case R.id.action_edit_tags:
                    showID3TagEditor(song);
                    return true;
                case R.id.action_add_to_playlist:
                    showPlaylistPicker(song);
                    return true;
            }
            return false;
        }
    });
    popup.show();
}
项目:iBeebo    文件:BrowserWeiboMsgCommentAndRepostAdapter.java   
private void bindCommentData(final ViewHolder holder, int position) {
    Drawable drawable = bg.get(holder);
    if (drawable != null) {
        holder.listview_root.setBackgroundDrawable(drawable);

    } else {
        drawable = holder.listview_root.getBackground();
        bg.put(holder, drawable);
    }

    if (listView.getCheckedItemPosition() == position + listView.getHeaderViewsCount()) {
        holder.listview_root.setBackgroundColor(checkedBG);
    }

    final CommentBean comment = (CommentBean) getItem(position);

    UserBean user = comment.getUser();
    if (user != null) {
        holder.username.setVisibility(View.VISIBLE);
        if (!TextUtils.isEmpty(user.getRemark())) {
            holder.username.setText(new StringBuilder(user.getScreen_name()).append("(").append(user.getRemark())
                    .append(")").toString());
        } else {
            holder.username.setText(user.getScreen_name());
        }
        if (!SettingUtils.getEnableCommentRepostListAvatar()) {
            holder.avatar.setLayoutParams(new RelativeLayout.LayoutParams(0, 0));
        } else {
            buildAvatar(holder.avatar, position, user);
        }

    } else {
        holder.username.setVisibility(View.INVISIBLE);
        holder.avatar.setVisibility(View.INVISIBLE);
    }

    holder.avatar.checkVerified(user);

    holder.weiboTextContent.setText(comment.getListViewSpannableString());

    holder.time.setTime(comment.getMills());

    holder.comment_source.setText(Html.fromHtml(comment.getSource()).toString());

    holder.reply.setVisibility(View.VISIBLE);
    holder.reply.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            PopupMenu popupMenu = new PopupMenu(getActivity(), holder.reply);
            popupMenu.inflate(R.menu.comments_popmenu);
            popupMenu.setOnMenuItemClickListener(new OnMenuItemClickListener() {

                @Override
                public boolean onMenuItemClick(MenuItem arg0) {
                    // TODO Auto-generated method stub
                    int id = arg0.getItemId();
                    switch (id) {
                        case R.id.reply_comment: {
                            replyComment(comment);
                            break;
                        }
                        case R.id.menu_copy: {
                            ClipboardManager cm = (ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
                            cm.setPrimaryClip(ClipData.newPlainText("sinaweibo", comment.getText()));
                            Toast.makeText(getActivity(), getActivity().getResources().getString(R.string.copy_successfully), Toast.LENGTH_SHORT).show();
                            break;
                        }

                        default:
                            break;
                    }
                    return false;
                }
            });
            popupMenu.show();
        }
    });
}
项目:iBeebo    文件:BrowserWeiboMsgCommentAndRepostAdapter.java   
private void bindCommentData(final ViewHolder holder, int position) {
    Drawable drawable = bg.get(holder);
    if (drawable != null) {
        holder.listview_root.setBackgroundDrawable(drawable);

    } else {
        drawable = holder.listview_root.getBackground();
        bg.put(holder, drawable);
    }

    if (listView.getCheckedItemPosition() == position + listView.getHeaderViewsCount()) {
        holder.listview_root.setBackgroundColor(checkedBG);
    }

    final CommentBean comment = (CommentBean) getItem(position);

    UserBean user = comment.getUser();
    if (user != null) {
        holder.username.setVisibility(View.VISIBLE);
        if (!TextUtils.isEmpty(user.getRemark())) {
            holder.username.setText(new StringBuilder(user.getScreen_name()).append("(").append(user.getRemark())
                    .append(")").toString());
        } else {
            holder.username.setText(user.getScreen_name());
        }
        if (!SettingUtils.getEnableCommentRepostListAvatar()) {
            holder.avatar.setLayoutParams(new RelativeLayout.LayoutParams(0, 0));
        } else {
            buildAvatar(holder.avatar, position, user);
        }

    } else {
        holder.username.setVisibility(View.INVISIBLE);
        holder.avatar.setVisibility(View.INVISIBLE);
    }

    holder.avatar.checkVerified(user);

    holder.weiboTextContent.setText(comment.getListViewSpannableString());

    holder.time.setTime(comment.getMills());

    holder.comment_source.setText(Html.fromHtml(comment.getSource()).toString());

    holder.reply.setVisibility(View.VISIBLE);
    holder.reply.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            PopupMenu popupMenu = new PopupMenu(getActivity(), holder.reply);
            popupMenu.inflate(R.menu.comments_popmenu);
            popupMenu.setOnMenuItemClickListener(new OnMenuItemClickListener() {

                @Override
                public boolean onMenuItemClick(MenuItem arg0) {
                    // TODO Auto-generated method stub
                    int id = arg0.getItemId();
                    switch (id) {
                        case R.id.reply_comment: {
                            replyComment(comment);
                            break;
                        }
                        case R.id.menu_copy: {
                            ClipboardManager cm = (ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
                            cm.setPrimaryClip(ClipData.newPlainText("sinaweibo", comment.getText()));
                            Toast.makeText(getActivity(), getActivity().getResources().getString(R.string.copy_successfully), Toast.LENGTH_SHORT).show();
                            break;
                        }

                        default:
                            break;
                    }
                    return false;
                }
            });
            popupMenu.show();
        }
    });
}
项目:Anki-Android    文件:NoteEditor.java   
private void setRemapButtonListener(ImageButton remapButton, final int newFieldIndex) {
    remapButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Timber.i("NoteEditor:: Remap button pressed for new field %d", newFieldIndex);
            // Show list of fields from the original note which we can map to
            PopupMenu popup = new PopupMenu(NoteEditor.this, v);
            final String[][] items = mEditorNote.items();
            for (int i = 0; i < items.length; i++) {
                popup.getMenu().add(Menu.NONE, i, Menu.NONE, items[i][0]);
            }
            // Add "nothing" at the end of the list
            popup.getMenu().add(Menu.NONE, items.length, Menu.NONE, R.string.nothing);
            popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    // Get menu item id
                    Integer idx = item.getItemId();
                    Timber.i("NoteEditor:: User chose to remap to old field %d", idx);
                    // Retrieve any existing mappings between newFieldIndex and idx
                    Integer previousMapping = getKeyByValue(mModelChangeFieldMap, newFieldIndex);
                    Integer mappingConflict = mModelChangeFieldMap.get(idx);
                    // Update the mapping depending on any conflicts
                    if (idx == items.length && previousMapping != null) {
                        // Remove the previous mapping if None selected
                        mModelChangeFieldMap.remove(previousMapping);
                    } else if (idx < items.length && mappingConflict != null && previousMapping != null && newFieldIndex != mappingConflict) {
                        // Swap the two mappings if there was a conflict and previous mapping
                        mModelChangeFieldMap.put(previousMapping, mappingConflict);
                        mModelChangeFieldMap.put(idx, newFieldIndex);
                    } else if (idx < items.length && mappingConflict != null) {
                        // Set the conflicting field to None if no previous mapping to swap into it
                        mModelChangeFieldMap.remove(previousMapping);
                        mModelChangeFieldMap.put(idx, newFieldIndex);
                    } else if (idx < items.length) {
                        // Can simply set the new mapping if no conflicts
                        mModelChangeFieldMap.put(idx, newFieldIndex);
                    }
                    // Reload the fields                     
                    updateFieldsFromMap(getCurrentlySelectedModel());
                    return true;
                }
            });
            popup.show();
        }
    });
}