다음과 같이 단순화 할 수있는 비정규 화 된 데이터베이스가 있습니다.
|__ 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
|_____ ...
UI에 표시하고 싶은 것은 하위 메뉴 (외부 RecyclerView) 목록으로, 각각 자체 메뉴 항목 목록 (외부 RecyclerView의 각 행에 대해 내부 RecyclerView 하나)이 있습니다. onCreate()
내 조각의 다음 코드로 이것을 달성했습니다 .
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);
다음은 row_submenu.xml
외부 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
TextViews에 menuItem의 이름과 가격 만 표시되므로 여기에 포함하지 않습니다.
요약하면,이 디자인은 RecyclerViews 초기화 중에 이상한 애니메이션, 점프 등을 유발합니다. 두 가지 질문이 있습니다.
디자인 측면에서 더 나은 대안이 있습니까? (ExpandableListView와 동등한 것)
그렇지 않은 경우 초기 데이터로드가 완료되었음을 어떻게 감지 할 수 있습니까 (마지막 menuItem이 검색 될 때까지 모든 것을 숨길 수 있음)? 나는 호출 생각 addListenerForSingleValueEvent
에 menuItemRef
여기에 작동하지 않습니다.
비동기 검색의 끝을 감지하기 위해 두 개의 카운터를 사용했습니다. 키 수는 누적 적으로 추가되어 totalItemCount
내부 어댑터 내부에로드 할 총 항목 수를 보유합니다. 내부 어댑터가 ViewHolder를 채우면 populatedItemCount
이 1 씩 증가하고 현재 값이와 비교됩니다 totalItemCount
. 값이 같으면 모든 데이터가 검색되고 행 내부의 상위 RecyclerView 및 RecyclerView가 표시 될 수 있음을 의미합니다.
다음은 질문에서 제공 한 중첩 어댑터의 마지막 버전입니다.
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) {
int totalItemCount = 0;
int populatedItemCount = 0;
@Override
protected void populateViewHolder(SubMenuHolder viewHolder, MySubMenu model, int position) {
totalItemCount += model.getMenuItems().size();
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) {
populatedMenuItemCount++;
viewHolderx.setName(modelx.getName());
viewHolderx.setPrice(modelx.getPrice());
// TODO - Find a resolution if this if never gets true
if (populatedMenuItemCount == totalMenuItemCount) {
(new Handler()).postDelayed(new Runnable() {
@Override
public void run() {
showSubMenuList();
}
}, 500);
}
}
};
viewHolder.setName(model.getName());
viewHolder.setMenuItemList(menuItemAdapter);
}
};
subMenuList.setAdapter(subMenuAdapter);
두 카운터를 비교하는 if 문에 대해서는 아직 확실하지 않습니다. 데이터 검색이 어떻게 든 중지되어 if 문의 조건이 true가되지 않고 앱이 중단되는 경우가 있습니까?
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다