Challenges in RecyclerView of Android L

前端 未结 4 1390
一整个雨季
一整个雨季 2021-02-20 04:27

I have been playing around with RecyclerView for a little bit. Is there any easy way to put OnClickListener for items in RecyclerView? I h

相关标签:
4条回答
  • Simply add this in your Adapter:

    @Override
    public void onBindViewHolder(AdapterRecyclerView.MyViewHolder viewHolder, int position) {
        yourItems.setOnClickListener(new OnClickListener() {
           @Override
           public void onClick(View v) {
              //do your stuff
           }
       });
    }
    
    0 讨论(0)
  • 2021-02-20 05:15

    Please see my answer here. You do need an extra class (which may be included as part of the full release) but it will allow you to create OnItemClickListeners the way you are used to for ListViews.

    0 讨论(0)
  • 2021-02-20 05:19

    Since you still didn't mark correct any answer, and even if it's an old question, I will try to provide the way I do. I think it is very clean and professional. The functionalities are taken from different blogs (I still have to mention them in the page), merged and methods have been improved for speed and scalability, for all activities that use a RecycleView.

    https://github.com/davideas/FlexibleAdapter

    • At lower class there is SelectableAdapter that provides selection functionalities and it's able to maintain the state after the rotation, you just need to call the onSave/onRestore methods from the activity.
    • Then the class FlexibleAdapter handles the content with the support of the animation (calling notify only for the position. Note: you still need to set your animation to the RecyclerView when you create it the activity).
    • Then you need to extend over again this class. Here you add and implements methods as you wish for your own ViewHolder and your Domain/Model class (data holder). Note: I have provided an example which does not compile because you need to change the classes with the ones you have in your project.

    I think that, it's the ViewHolder that should keep the listeners of the clicks and that it should be done at the creation and not in the Binding method (that is called at each invalidate from notify...() ).

    Also note that this adapter handles the basic clicks: single and long clicks, if you need a double tap you need to use the way Jabob Tabak does in its answer.

    I still have to improve it, so keep an eye on it. I also want to add some new functionalities like the Undo.

    0 讨论(0)
  • 2021-02-20 05:22

    Here you get a simple Adapter class which can perform onItemClick event on each list row for the recyclerview.

    0 讨论(0)
提交回复
热议问题