android如何获得 RecyclerView 位置从 PopupMenu onMenuItemClick

标签: Android
发布时间: 2017/3/19 16:36:39
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

使用 RecylcerView ,我想有一个弹出 menu 每个列表中的项目,与此类似︰

Popup Menu in Google Play Music playlist.

创建弹出窗口 menu 是简单,但你怎么在单击的项的位置 onMenuItemClicked 吗?

public class Activity extends AppCompatActivity implements PopupMenu.OnMenuItemClickListener {

    public void showPopupMenu(View v) {
        PopupMenu popupMenu = new PopupMenu(this, v);
        MenuInflater inflater = popupMenu.getMenuInflater();
        inflater.inflate(R.menu.edit_delete_menu, popupMenu.getMenu());
        popupMenu.show();
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {

        //get position here from RecyclerView here?

        switch (item.getItemId()) {
            case R.id.edit:
            //Do position specific action
            break;
            case R.id.delete:
                //Do position specific action
                break;
        }
        return false;
    }


}

解决方法 1:

好的所以我 (惊讶) 设法回答我自己的问题在这里。

为了获得的位置从 RecylcerView adapteronMenuItemClicked 使用 PopupMenu ,我创建了一个自定义实现的 PopupMenu

这样做提供更大的弹性,在使用时 PopupMenu ,例如显示在你的菜单图标

看看谷歌的源代码为 PopupMenu ,和创建您自己,像 MyPopupMenu ,完全是一样的但是您可以修改某些实例的类能做些什么。

若要完成我的问题,添加 OnClickListener 到更 button 内我 RecyclerView.Adapter 。单击时, button 调用接口方法传递两个 button view ,和适配器的当前位置。

在自定义实现的 MyPopupMenu ,添加为一个 int 值的每个构造函数的变量要求。此外将添加 int position 对接口方法 onMenuItemClick(MenuItem item, int position)MyPopupMenu

最后,组装在 activity 类。

public class MyActivity extends AppCompatActivity implements MyAdapter.OnItemEventListener, PopupMenu.OnMenuItemClickListener {

    @Override
    public void onMoreClicked(View v, int position) {
        MyPopupMenu popupMenu = new MyPopupMenu(this, v, position);
        MenuInflater inflater = popupMenu.getMenuInflater();
        inflater.inflate(R.menu.edit_delete_menu, popupMenu.getMenu());
        popupMenu.setOnMenuItemClickListener(this);
        popupMenu.show();
    }

    @Override
    public boolean onMenuItemClick(MenuItem item, int position) {
        switch (item.getItemId()) {
            case R.id.edit:
                //Do position specific action with int position
                break;
            case R.id.delete:
                //Do position specific action with int position
                break;
        }
        return false;
    }
}

class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private final OnItemEventListener onItemEventListener;

    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageButton more;

        ViewHolder(View v) {
            super(v);
            more = (ImageButton) v.findViewById(R.id.list_item_more_button);
        }
    }

    public NewGameAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
        final ViewHolder viewHolder = new ViewHolder(v);

        viewHolder.more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onItemEventListener.onMoreClicked(viewHolder.more, viewHolder.getAdapterPosition());
            }
        });

        return viewHolder;
    }

    interface OnItemEventListener {
        void onMoreClicked(View v, int position);
    }
}

让我知道你们的想法 !

赞助商