I am developing an app in which the main screen is entirely built from the response of a webserver. We used to display this content in a recycler view, but as the views are very complex and share almost nothing in common, the scroll was not smooth as the recycling behaviour was very expensive.
I decided to create a Fragment for each type of view that I have to expose and add them to a LinearLayout inside a ScrollView. The result was amazing. No recycling made the scroll very smooth and orientation change got a lot better too, as fragments have a lifecycle and their data can be serialized. However, I got a new problem:
https://code.google.com/p/android/issues/detail?id=69403
And from what I understand from this SO question:
Android multiple fragment transaction ordering
I would have to include the source of the support library in order to that workaround and I would't like that unless it is an absolute necessity.
Does anyone know any other way of how to display this kind of dynamic content?
EDIT: This is the code that I have right now:
FragmentTransaction tx = mFragmentManager.beginTransaction();
mContainerList = new ArrayList<>(containers);
if(mFragmentManager.getFragments() != null) {
for (Fragment f : mFragmentManager.getFragments())
if (f != null)
tx.remove(f);
Collections.reverse(mContainerList);
}
for(final InitResult.Container c : mContainerList) {
InitFragment fragment = null;
switch (c.getType()) {
case InitResult.Container.HORIZONTAL_LIST:
if (c.getItems() != null && c.getItems().size() > 0 && c.getItems().get(0).getType().equals(InitResult.Container.BANNER))
fragment = new InitBannerFragment();
else
fragment = new InitHListFragment();
break;
case InitResult.Container.GRID:
fragment = new InitGridFragment();
break;
case InitResult.Container.BANNER:
fragment = new InitBannerFragment();
break;
case InitResult.Container.PROMOTIONAL:
fragment = new InitPromotionalFragment();
break;
case InitResult.Container.PROTEGE:
fragment = new InitProtegeFragment();
break;
case InitResult.Container.PROTEGE_ACTIVE:
fragment = new InitProtegeActiveFragment();
break;
}
if (fragment != null) {
fragment.setInitContainer(c);
tx.add(containerList.getId(), fragment);
}
}
tx.commitAllowingStateLoss();
mFragmentManager.executePendingTransactions();
Whenever the screen is refreshed, I run this code. Notice that if I'm not doing this for the first time, I need to reverse the container list so it is listed int the desired order.
My problem happens when a new item comes from the server, not just a list with updated content. If this new item comes, it won't be placed in the desired order, it will be placed at the end of the list.