Click here to Skip to main content
15,851,387 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have created a recyclerView with an icon that when clicked pop's up a menu with 2 items. now how do i set click listener for that items. i have an adapter where i have the code that pops up the menu item.

Java
private List<FeedItem> feedItemList;
   private Context mContext;

   public MyRecyclerAdapter(Context context, List<FeedItem> feedItemList) {
       this.feedItemList = feedItemList;
       this.mContext = context;
   }

   @Override
   public CustomViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
       View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.main_adapter, null);

       CustomViewHolder viewHolder = new CustomViewHolder(view);
       return viewHolder;
   }

   @Override
   public void onBindViewHolder(CustomViewHolder customViewHolder, int i) {
       FeedItem feedItem = feedItemList.get(i);


       customViewHolder.textView.setText(Html.fromHtml(feedItem.getHehw()));
       //customViewHolder.textView.setOnClickListener(clickListener);
       //customViewHolder.textView.setTag(customViewHolder);
       customViewHolder.textView1.setText(Html.fromHtml(feedItem.getHesub()));
       customViewHolder.imageView.setOnClickListener(clickListener);
       customViewHolder.imageView.setTag(customViewHolder);

   }

   @Override
   public int getItemCount() {
       return (null != feedItemList ? feedItemList.size() : 0);
   }

   public class CustomViewHolder extends RecyclerView.ViewHolder {

       protected TextView textView,textView1;
       protected ImageView imageView;
       public CustomViewHolder(View view) {
           super(view);
           this.imageView= (ImageView) view.findViewById(R.id.thumbnail);
           this.textView = (TextView) view.findViewById(R.id.HeHw);
           this.textView1= (TextView) view.findViewById(R.id.HeSub);
       }
   }
   View.OnClickListener clickListener = new View.OnClickListener() {
       @Override
       public void onClick(View view) {
           CustomViewHolder holder = (CustomViewHolder) view.getTag();
           int position = holder.getPosition();

           PopupMenu popup = new PopupMenu(mContext, view);
           MenuInflater inflater = popup.getMenuInflater();
           inflater.inflate(R.menu.popup_menu, popup.getMenu());

           popup.show();

           FeedItem feedItem = feedItemList.get(position);
           //Toast.makeText(mContext, feedItem.getThumbnail(), Toast.LENGTH_SHORT).show();

       }

   };


I have added a function that captures the click events on a recyclerView.
Java
private final RecyclerView mRecyclerView;
    private OnItemClickListener mOnItemClickListener;
    private OnItemLongClickListener mOnItemLongClickListener;
    private View.OnClickListener mOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mOnItemClickListener != null) {
                RecyclerView.ViewHolder holder = mRecyclerView.getChildViewHolder(v);
                mOnItemClickListener.onItemClicked(mRecyclerView, holder.getAdapterPosition(), v);
            }
        }
    };
    private View.OnLongClickListener mOnLongClickListener = new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            if (mOnItemLongClickListener != null) {
                RecyclerView.ViewHolder holder = mRecyclerView.getChildViewHolder(v);
                return mOnItemLongClickListener.onItemLongClicked(mRecyclerView, holder.getAdapterPosition(), v);
            }
            return false;
        }
    };
    private RecyclerView.OnChildAttachStateChangeListener mAttachListener
            = new RecyclerView.OnChildAttachStateChangeListener() {
        @Override
        public void onChildViewAttachedToWindow(View view) {
            if (mOnItemClickListener != null) {
                view.setOnClickListener(mOnClickListener);
            }
            if (mOnItemLongClickListener != null) {
                view.setOnLongClickListener(mOnLongClickListener);
            }
        }

        @Override
        public void onChildViewDetachedFromWindow(View view) {

        }
    };

    private ItemClickSupport(RecyclerView recyclerView) {
        mRecyclerView = recyclerView;
        mRecyclerView.setTag(R.id.item_click_support, this);
        mRecyclerView.addOnChildAttachStateChangeListener(mAttachListener);
    }

    public static ItemClickSupport addTo(RecyclerView view) {
        ItemClickSupport support = (ItemClickSupport) view.getTag(R.id.item_click_support);
        if (support == null) {
            support = new ItemClickSupport(view);
        }
        return support;
    }

    public static ItemClickSupport removeFrom(RecyclerView view) {
        ItemClickSupport support = (ItemClickSupport) view.getTag(R.id.item_click_support);
        if (support != null) {
            support.detach(view);
        }
        return support;
    }

    public ItemClickSupport setOnItemClickListener(OnItemClickListener listener) {
        mOnItemClickListener = listener;
        return this;
    }

    public ItemClickSupport setOnItemLongClickListener(OnItemLongClickListener listener) {
        mOnItemLongClickListener = listener;
        return this;
    }

    private void detach(RecyclerView view) {
        view.removeOnChildAttachStateChangeListener(mAttachListener);
        view.setTag(R.id.item_click_support, null);
    }

    public interface OnItemClickListener {

        void onItemClicked(RecyclerView recyclerView, int position, View v);
    }

    public interface OnItemLongClickListener {

        boolean onItemLongClicked(RecyclerView recyclerView, int position, View v);
    }


I have populted recyclerView with JSON data and with this click function i'm able to capture the row which is clicked.

when i click the icon that was set in the recyclerView it pops up a popupmenu. but i'm unable to setonClickListener to it .
Posted
Updated 28-Dec-15 0:34am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900