如何从PopupMenu onMenuItemClick获取RecyclerView位置

时间:2017-01-03 23:27:15

标签: android android-popupwindow

使用RecylcerView,我正在尝试为列表中的每个项目设置一个弹出菜单,类似于:

Popup Menu in Google Play Music playlist.

创建弹出菜单很简单,但是如何获得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;
    }


}

4 个答案:

答案 0 :(得分:1)

好吧,所以我(令人惊讶地)在这里设法回答了我自己的问题。

为了使用RecylcerViewonMenuItemClicked PopupMenu内的PopupMenu适配器获取位置,我创建了PopupMenu的自定义实现。

这样做可以在使用PopupMenu时提供更大的灵活性,例如displaying icons in your menus

查看Google的MyPopupMenu源代码,并创建自己的类似OnClickListener的内容,但是您可以修改类可以执行的操作的某些实例。

要解决我的问题,我在RecyclerView.Adapter内的“更多”按钮中添加了MyPopupMenu。单击时,该按钮调用一个接口方法,该方法既传递按钮视图,又传递适配器的当前位置。

int position的自定义实现中,为每个构造函数添加int值的变量需求。同时将onMenuItemClick(MenuItem item, int position)添加到MyPopupMenu中的接口方法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); } }

最后,在活动类中汇总。

Employee[ ] getAllEmployees()

Employee getEmployee(String id)

Employ (Employee employee)

modify_Employee (Employee employee, String id)

让我知道你们的想法!

答案 1 :(得分:0)

您将需要:

  1. Interface,其中一种方法click positionparameter。 例如:

    public interface OnPositionClickListener {
      void onPositionClicked(int position);
    }
    
  2. Implement Interface activityRecyclerView传递给@Override public void onPositionClicked(int position) { //show your popup here and do position based actions here } 的适配器。所以,你的活动内部应该是这样的方法实现:

    click listener
  3. 将适配器的click listener引用传递给视图持有者,并从菜单视图的可点击项public class SampleViewHolder extends RecyclerView.ViewHolder { public SampleViewHolder(View view, OnPositionClickListener listener) { //*** //your view bind code here // *** clickableView.setOnClickListener( new OnClickListener { @Override public void onClick(View view) { listener.onPositionClicked(getAdapterPosition()); } }); } } 调用它的方法。

    示例:

    position

答案 2 :(得分:0)

首先,在您显示的代码段中,它与可用图片无关。

请提供您的onBindViewHolder代码。这就是应该设置单击侦听器上的菜单项的位置。

答案 3 :(得分:0)

您好,亲爱的您需要在适配器中使用这种方式

  @Override
    public void onBindViewHolder(@NonNull final RecentPlayedAdapter.MyViewHolder holder, final int position) {
        holder.menudotes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
PopupMenu popupMenu=new PopupMenu(context, holder.menudotes, Gravity.START);
                    popupMenu.getMenuInflater().inflate(R.menu.popupmenu1,popupMenu.getMenu());
                    popupMenu.show();
                    popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                        @Override
                        public boolean onMenuItemClick(MenuItem item) {
                            switch (item.getItemId()) {
                                case R.id.download:
                                    Toast.makeText(context, "Download", Toast.LENGTH_SHORT).show();
                                    //Do position specific action with int position
                                    break;
                                case R.id.playlist:
                                    //Do position specific action with int position
                                    Toast.makeText(context, "Playlist", Toast.LENGTH_SHORT).show();

                                    break;
                                case R.id.share:
                                    //Do position specific action with int position
                                    Toast.makeText(context, "Share", Toast.LENGTH_SHORT).show();


                                    break;
                            }
                            return true;
                        }
                    });
    }
        });
    }