Rendering fragments in Adapters

Fragments in Adapters

This is a black magic that I met recently, There is an Adapter pattern in Android, which bonds the data and View together, but what if the data part is a bunch of fragments. Can we render fragments in Adapters? After doing some research and experiments, the answer is: Fragments can be rendered in Adapters in some conditions.

ListView .vs. RecyclerView

When comes to Adapter pattern in Android, there are two kinds of Adapters, AdapterView, and RecyclerView, while the AdapterView is an abstract class, its most famous representative is the ListView.
What’s the difference between ListView and RecyclerView? ListView is the child of AdapterView, while RecyclerView is inherited from ViewGroup. I think that’s the biggest inherent difference. RecyclerView is also considered as the replacement for the ListView, it is invited in lollipop age, and with some benefits inside:
  • ViewHolder pattern inside;
  • decoupling the item layout manager;
  • isolated item decoration;
  • separated item animation;
  • ViewHolder recycling control;
In conclusion, RecyclerView is the much powerful, flexible, and a major enhancement over ListView. I always use RecyclerView in my new codes.

How to

Rendering fragments inside Adapters should follow a few preconditions.
  • a limited size of fragments;
  • can not frequently remove and add fragments;
There are some experiment codes inside below link:
I tried the RecyclerView and ListView both, the most effective way is to replace all adapter if there are some dataset change.
Again, this is a black magic, it drives me mad with codes writing in this way, I did those just for a stupid job, the code is ugly. Why? the question is what is the purpose of Fragments, it is to let the UI codes reusable and flexible. Someones believe fragments bring a self-organized way to construct android app architecture, they isolate the Android codes to separate functional fragments. However, they sacrifice the UI interaction inside an activity and bring the hard question like what this article describes. This is an example of overuse of Fragment, I believe.



Comments

Popular posts from this blog

Bluedroid stack in android

How to setup a NAT server?

Network programming in elisp