Tuesday, May 12, 2015

Adding context menus to RecyclerView items


Add an affordance

The "three-dot" overflow pattern was my first choice.





Setup click listeners in ViewHolder


        private ImageView mOverflowIcon;

        public FileViewHolder(View holdMe) {
            super(holdMe);
            holdMe.setOnClickListener(this);
            holdMe.setOnLongClickListener(this);
            mOverflowIcon = (ImageView) holdMe.findViewById(R.id.mfp_context_menu);
            mOverflowIcon.setOnClickListener(this);
        }

Create context menu in OnClickListener

Display the PopupMenu. ("this" is the ViewHolder)

        @Override
        public void onClick(View v) {
            if (v == mOverflowIcon) {
                PopupMenu popup = new PopupMenu(v.getContext(), v);
                popup.inflate(R.menu.mfp_overflow_menu_file);
                popup.setOnMenuItemClickListener(this);
                popup.show();
            }
        }

The menu xml.

        <?xml version="1.0" encoding="utf-8"?>
        <menu xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto">
            <item
                android:id="@+id/delete_file"
                android:title="@string/mfp_delete"
                app:showAsAction="always" />
        </menu>

Add your logic for the menu items

Just the implementation of the OnMenuItemClickListener, and since it's the ViewHolder you have the data as well.

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            Toast.makeText(mOverflowIcon.getContext(),"DO SOME STUFF HERE", Toast.LENGTH_LONG).show();
            return true;
        }

NOTE: WindowLeaked

Just a note, if an orientation change occurs while the PopupMenu is still showing you will get a nice informative WindowLeaked exception. This does not crash your app, it is just poor practice. You need to call popupMenu.dismiss() when the activity context is being destroyed, your resulting code for that may vary depending on your implementation.


33 comments:

  1. how do you know which item you clicked or longClicked on?

    ReplyDelete
    Replies
    1. The function onLongClicked takes a parameter "int position" , which you can use to determine which item it is (position of the clicked item in the array)

      Delete
    2. This is the way to do it using a ListView, not a RecyclerView as demonstrated in this post. You need to manage clicks and long-clicks manually on the views themselves. This can be done in the ViewHolder.

      Delete
    3. IEEE Final Year Project centers make amazing deep learning final year projects ideas for final year students Final Year Projects for CSE to training and develop their deep learning experience and talents.

      IEEE Final Year projects Project Centers in India are consistently sought after. Final Year Students Projects take a shot at them to improve their aptitudes, while specialists like the enjoyment in interfering with innovation.

      corporate training in chennai corporate training in chennai

      corporate training companies in india corporate training companies in india

      corporate training companies in chennai corporate training companies in chennai

      I have read your blog its very attractive and impressive. I like it your blog. Digital Marketing Company in Chennai Project Centers in Chennai

      Delete
  2. how do you know which item you clicked or longClicked on?

    ReplyDelete
    Replies
    1. The ViewHolder is the LongClickListener, so you should have the associated data in scope. e.g. FileViewHolder.this.mFile or just mFile.

      Delete
  3. Your very own commitment to getting the message throughout came to be rather powerful and have consistently enabled employees just like me to arrive at their desired goals.
    java training in marathahalli | java training in btm layout

    java training in jayanagar | java training in electronic city

    java training in chennai | java training in USA

    selenium training in chennai

    ReplyDelete
  4. This is most informative and also this post most user friendly and super navigation to all posts... Thank you so much for giving this information to me.. 
    Devops Training in Chennai

    Devops Training in Bangalore

    Devops Training in pune

    ReplyDelete
  5. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
    Devops training in velachery
    Devops training in annanagar
    Devops training in sholinganallur

    ReplyDelete
  6. I simply wanted to write down a quick word to say thanks to you for those wonderful tips and hints you are showing on this site.
    Blueprism training in marathahalli

    Blueprism training in btm

    Blueprism online training

    ReplyDelete
  7. Thank you for allowing me to read it, welcome to the next in a recent article. And thanks for sharing the nice article, keep posting or updating news article.
    Blueprism training in marathahalli

    Blueprism training in btm

    Blueprism online training

    ReplyDelete
  8. Thank you for an additional great post. Exactly where else could anybody get that kind of facts in this kind of a ideal way of writing? I have a presentation next week, and I’m around the appear for this kind of data.

    angularjs Training in chennai
    angularjs Training in chennai

    angularjs-Training in tambaram

    angularjs-Training in sholinganallur

    angularjs-Training in velachery

    ReplyDelete
  9. Hmm, it seems like your site ate my first comment (it was extremely long) so I guess I’ll just sum it up what I had written and say, I’m thoroughly enjoying your blog. I as well as an aspiring blog writer, but I’m still new to the whole thing. Do you have any recommendations for newbie blog writers? I’d appreciate it.

    AWS Interview Questions And Answers

    AWS Training in Bangalore | Amazon Web Services Training in Bangalore

    AWS Training in Pune | Best Amazon Web Services Training in Pune

    Amazon Web Services Training in Pune | Best AWS Training in Pune

    AWS Online Training | Online AWS Certification Course - Gangboard

    ReplyDelete
  10. Hmm, it seems like your site ate my first comment (it was extremely long) so I guess I’ll just sum it up what I had written and say, I’m thoroughly enjoying your blog. I as well as an aspiring blog writer, but I’m still new to the whole thing. Do you have any recommendations for newbie blog writers? I’d appreciate it.

    AWS Interview Questions And Answers

    AWS Training in Bangalore | Amazon Web Services Training in Bangalore

    AWS Training in Pune | Best Amazon Web Services Training in Pune

    Amazon Web Services Training in Pune | Best AWS Training in Pune

    AWS Online Training | Online AWS Certification Course - Gangboard

    ReplyDelete
  11. I am really happy with your blog because your article is very unique and powerful for new reader.
    Click here:
    selenium training in chennai
    selenium training in bangalore
    selenium training in Pune
    selenium training in pune
    Selenium Online Training



    https://michaelrps.blogspot.com/2014/06/c-interface-can-be-inherited.html

    ReplyDelete
  12. I am really impressed with your efforts and really pleased to visit this post.
    Devops Training in Chennai | Devops Training Institute in Chennai

    ReplyDelete
  13. You got an extremely helpful website I actually have been here reading for regarding an hour. I’m an initiate and your success is incredibly a lot of a concept on behalf of me.

    devops online training

    aws online training

    data science with python online training

    data science online training

    rpa online training

    ReplyDelete
  14. Thank you for this post. Thats all I are able to say. You most absolutely have built this blog website into something speciel. You clearly know what you are working on, youve insured so many corners.thanks
    Microsoft Azure online training
    Selenium online training
    Java online training
    Python online training
    uipath online training

    ReplyDelete
  15. thanks for your information really good and very nice web design company in velachery

    ReplyDelete
  16. It's a wonderful post and very helpful, thanks for all this information about Java. You are including better information regarding this topic in an effective way.Thank you so much.
    Java training in chennai | Java training in annanagar | Java training in omr | Java training in porur | Java training in tambaram | Java training in velachery

    ReplyDelete
  17. Thank you a lot for providing individuals with a very spectacular possibility to read critical reviews from this site.
    AWS training in chennai | AWS training in anna nagar | AWS training in omr | AWS training in porur | AWS training in tambaram | AWS training in velachery

    ReplyDelete
  18. Thanks a lot very much for the high your blog post quality and results-oriented help. I won’t think twice to endorse to anybody who wants and needs support about this area.



    oracle training in chennai

    oracle training in annanagar

    oracle dba training in chennai

    oracle dba training in annanagar

    ccna training in chennai

    ccna training in annanagar

    seo training in chennai

    seo training in annanagar

    ReplyDelete
  19. Thank you for allowing me to read it, welcome to the next in a recent article. And thanks for sharing the nice article, keep posting or updating news article.
    oracle training in chennai

    oracle training in omr

    oracle dba training in chennai

    oracle dba training in omr

    ccna training in chennai

    ccna training in omr

    seo training in chennai

    seo training in omr

    ReplyDelete
  20. Web design encompasses many different skills and disciplines in the production and maintenance of websites. The different areas of web design include web graphic design; user interface design; authoring, including standardised code and proprietary software; user experience design; and search engine optimization.
    Java training in chennai

    python training in chennai

    web designing and development training in chennai

    selenium training in chennai

    digital-marketing training in chennai

    ReplyDelete