Every Rose has its Thorns
It’s undeniable that being a brand new widget, RecyclerView has some downsides that need to be addressed in the short/mid term, most probably in the shape of new open source libraries.
Multiple selection
One of these downsides is the single or multiple selection UI pattern. This feature has been covered extensively in ListViews, but ViewHolder does not include any tool for implementing selection.
If you want to use this UI pattern, ask yourself if using a RecyclerView will add significant value to your app because unfortunately the solution to this issue is not so simple.
Luckily for us, Bill Phillips from Big Nerd Ranch has found a nice workaround to get the multiple selection working. You can find his solution here.
Headers and footers
It would have been nice to have native support for headers and footers in RecyclerView. However, we are facing here another use case in which we need to rely on third party libraries to achieve this.
Obviously, you can make this functionality a reality by coding it yourself. As stated above, it is not so complicated to place a custom layout as a first or last item of RecyclerView. Besides providing a custom layout for those elements, you just have to include the header (or footer) element to the dataset and notify the Adapter. It’s a shame that such a common use case has to be coded in every single project.
Be careful when accessing the UI from a background thread
This is not a characteristic that is exclusive to RecyclerView, but I think it is important to mention it because this is a very common mistake.
In Android you can not access the UI from a background thread. This may be a problem when trying to update the adapter from a callback (for example: trying to notify the adapter of changes in the dataset after fetching data from the Internet) .
If you find yourself getting the following exception:
java.lang.IllegalStateException: Cannot call this method while RecyclerView is computing a layout or scrolling
This probably occurs because you are calling notifyItemInserted(position);, notifyItemChanged(position), or notifyItemRemoved(position); from a background thread (or from a callback, which runs on a background thread).
To solve this, use a handler:
Conclusion and comments
RecyclerView has a more than promising future. Its outstanding flexibility and customization capabilities provide some incredibly interesting scenarios for Android developers.
However, the current lack of documentation and open source libraries can make the migration path to this new technology a little bit troublesome to say the least.
Since this new widget is officially developed and supported by Google, and seems that is going to be the new “best approach” to represent large heterogeneous datasets, I strongly advise you to start testing RecyclerView as soon as possible.
Nevertheless, if you are thinking about implementing this widget in a commercial application I would advise you to think twice, as the lack of resources for RecyclerView can significantly increase the coding time needed to get it working properly. This kind of situation takes place every time a new technology hits the market and it is very likely to change once RecyclerView gains popularity amongst developers and new libraries and documents get released.
Whatever you decide, I wish you the best of luck in your next project!
Posted by Juan Ignacio Molina (juan.molina@wolox.com.ar)