I have a denormalized database which can be simplified as follows:
|__ menu
| |___ menuKey1
| |___ subMenus (key list of subMenus belongs to menu)
| |___ other fields...
|
|__ subMenu
| |___ subMenuKey1
| |__ menuItems (key list of menuItems belongs to subMenu)
| |__ other fields...
|
|__ menuItem
|_____ ...
What I want to show in the UI is the list of submenus (outer RecyclerView), each having their own list of menu items (one inner RecyclerView for each row of outer RecyclerView). I achieved this with following code in onCreate()
of my fragment:
rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference subMenuKeyRef = rootRef.child("menu").child(menuKey).child("subMenus");
DatabaseReference subMenuRef = rootRef.child("subMenu");
subMenuAdapter = new FirebaseIndexRecyclerAdapter<MySubMenu, SubMenuHolder>(
MySubMenu.class,
R.layout.row_submenu,
SubMenuHolder.class,
subMenuKeyRef,
subMenuRef) {
@Override
protected void populateViewHolder(SubMenuHolder viewHolder, MySubMenu model, int position) {
String subMenuKey = getRef(position).getKey();
DatabaseReference menuItemKeyRef = rootRef.child("subMenu").child(subMenuKey).child("menuItems");
DatabaseReference menuItemRef = rootRef.child("menuItem");
FirebaseIndexRecyclerAdapter menuItemAdapter = new FirebaseIndexRecyclerAdapter<MyMenuItem, MenuItemHolder>(
MyMenuItem.class,
R.layout.row_menu_item,
MenuItemHolder.class,
menuItemKeyRef,
menuItemRef) {
@Override
protected void populateViewHolder(MenuItemHolder viewHolderx, MyMenuItem modelx, int positionx) {
viewHolderx.setName(modelx.getName());
viewHolderx.setPrice(modelx.getPrice());
}
};
viewHolder.setName(model.getName());
viewHolder.setMenuItemList(menuItemAdapter);
}
};
subMenuList.setAdapter(subMenuAdapter);
Here is row_submenu.xml
, which is row of outer RecyclerView:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone">
<TextView
android:id="@+id/submenu_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAllCaps="true"
android:textStyle="bold"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.v7.widget.RecyclerView
android:id="@+id/submenu_menu_items"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:paddingBottom="8dp"
app:layoutManager="android.support.v7.widget.LinearLayoutManager"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/submenu_name" />
</android.support.constraint.ConstraintLayout>
row_menu_item.xml
just shows menuItem's name and price in TextViews so I don't include it here.
To sum up, this design causes weird animations, jumps, etc. during initialization of RecyclerViews. I have two questions:
Is there a better alternative in terms of design? (Something equivalent to ExpandableListView)
If not, how can I detect that initial data load is complete (so that I can hide everything until last menuItem is retrieved)? I think calling
addListenerForSingleValueEvent
onmenuItemRef
won't work here.