Recently, I received a request to implement a popover for class selection, which is quite common in ordinary apps. I had some problems implementing it myself, so make a note of it

First, the effect drawing is given




Here are some of the problems I encountered

Problem 1: RecyclerView click to select item, sliding RecyclerView item click effect will disappear, there will be other items selected

At this point I came up with a method called onViewAttachedToWindow(). This method is called when the item is visible in recyclerView, so you can record the selected position in this method

The code is as follows :(don’t bother to use markdown)



Here I make a judgment between selecting the class without clicking on item and selecting the class with clicking on item.

Because onBindViewHolder() is called after this method is executed, all you need to do is change the data



Problem two: RecyclerView Item selection

You can find a lot of solutions by bing. I refer to the solutions written by others



The interface is used to pass in the position of the click, judge the position and the data in the list, and then call notifyDataSetChanged() to refresh the position

Here is the code for onBindViewHolder()



You can see the last line of code, this is another pit. When I was done, I realized that if I clicked on the item, then the dialog disappeared but the item that I clicked on didn’t click. After studying, I found that the dialog disappeared too fast. The Dialog disappeared before item showed the clicking effect, or the clicking effect appeared in the process of dialog disappearing, which was invisible to the naked eye. Therefore, the dialog was delayed to disappear later, which was a perfect solution. As for the above problem, adding judgments and calling notifyDataSetChanged() is probably slowing you down to keep up with dialog dimiss().

We have met these two problems at present, and we will update if there are any problems