尝试从ViewHolder对象中调用弹出菜单

时间:2017-08-27 20:08:14

标签: android popupmenu

我试图点击recyclerview中列表项中的imageview。我可以通过将view.getContext()传递给Toast方法来调用Toast消息。但由于某种原因,我不能这样做。它告诉我setOnMenuItemClickListener不能在PopupMenu中应用。我做错了什么?

public static class ViewHolder extends RecyclerView.ViewHolder{

        public TextView tvName;
        public TextView tvDate;
        public ImageView imageView;

        public ViewHolder(View itemView){
            super(itemView);

            tvName = (TextView)itemView.findViewById(R.id.match_name);
            tvDate = (TextView)itemView.findViewById(R.id.match_date);
            imageView = (ImageView)itemView.findViewById(R.id.imageView);

            imageView.setOnClickListener(new View.OnClickListener(){

                @Override
                public void onClick(View view) {

                    PopupMenu popupMenu = new PopupMenu(view.getContext(), view);
                    popupMenu.setOnMenuItemClickListener(view.getContext());
                    popupMenu.inflate(R.menu.popup_menu);
                    popupMenu.show();


                    Toast.makeText(view.getContext(), "onClick working", Toast.LENGTH_LONG).show();
                }
            });
        }

3 个答案:

答案 0 :(得分:1)

您可以像这样使用匿名PopupMenu.OnMenuItemClickListener

popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener(){
   public boolean onMenuItemClick(MenuItem item){
       // your stuff here (one frequently uses a switch-case, 
       // the cases depending on item.getId() )
   } 
});

答案 1 :(得分:0)

我使用这样的代码在PopMenu

中显示RecycleView
imageView.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {

               final PopupMenu popup = new PopupMenu(view.getContext(), imageView);
                                    popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
                                    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                                        @Override
                                        public boolean onMenuItemClick(MenuItem item) {
                                            switch (item.getItemId()) {
                                                case R.id.item_edit:
                                                    // to edit

                                                    break;
                                                case R.id.item_delete:
                                                    // to delete

                                                    break;
                                            }
                                            return false;
                                        }
                                    });
                                    //displaying the popup
                                    popup.show();

                Toast.makeText(view.getContext(), "onClick working", Toast.LENGTH_LONG).show();
            }
        });

可以帮助你。 :)

答案 2 :(得分:0)

Android Recyclerview使用onItemClickListener,为什么我们不能尝试这个只是像ListView一样工作。

来源:http://sapandiwakar.in/recycler-view-item-click-handler/

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

public class RecyclerItemClickListener implements
        RecyclerView.OnItemTouchListener {

    private OnItemClickListener mListener;
    public interface OnItemClickListener {
        public void onItemClick(View view, int position);
    }
    GestureDetector mGestureDetector;
    public RecyclerItemClickListener(Context context, OnItemClickListener listener) {
        mListener = listener;
        mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onSingleTapUp(MotionEvent e) {
                return true;
            }
        });
    }
    @Override
    public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
        View childView = view.findChildViewUnder(e.getX(), e.getY());
        if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
            mListener.onItemClick(childView, view.getChildAdapterPosition(childView));
        }
        return false;
    }

    @Override
    public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) {
    }

    @Override
    public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

    }
}