Using a nested CardView within a RecylerView in-room causes the CardView to not be rendered until you subsequently scroll down and back up in the containing RecylerView. Then the child CardView is shown as expected.
This is my BindView holder in the parent level calling the child level adapter with its data. I'm assuming it's because the data is being fetched off the thread and so the parent doesn't have the info to render the spaces for CardView - but I can't figure out how to do that.
@Override
public void onBindViewHolder(ItemViewHolder holder, int position) {
category current = myCategoryItems.get(position);
holder.textViewName.setText(current.Category);
//Set sub adapter
myAdapter = new SubCategoryCardAdapter(holder.subRecyclerView.getContext(),myFcontext);
holder.subRecyclerView.setAdapter(myAdapter);
holder.subRecyclerView.setLayoutManager(new LinearLayoutManager(holder.subRecyclerView.getContext(), RecyclerView.HORIZONTAL, false));
holder.subRecyclerView.setHasFixedSize(true);
mySubCategoryModel = new ViewModelProvider(myFcontext).get(SubCategoryModel.class);
//Update view when data changes - triggered by observer
mySubCategoryModel.getItemListByCat(current.cat_id).observe(myFcontext, sub_categoryList ->
myAdapter.setItems(sub_categoryList,this));
}
Thanks in advance.
code
public class SubCategoryRepository {
private SubCategoryDao mySubCategoryDao;
private LiveData<List<sub_category>> itemsList;
private LiveData<List<sub_category>> itemsListByCat;
SubCategoryRepository(Application application) {
KaitkanDatabase database = KaitkanDatabase.getDatabase(application);
mySubCategoryDao = database.sub_categoryDao();
itemsList = mySubCategoryDao.getItemList();
}
LiveData<List<sub_category>> getAllItems() {
return itemsList;
}
LiveData<List<sub_category>> getItemListByCat( Integer catid) {
itemsListByCat = mySubCategoryDao.getItemListByCat(catid);
return itemsListByCat;
}
} public class SubCategoryModel extends AndroidViewModel {
private SubCategoryRepository mySubCategoryRepository;
private LiveData<List<sub_category>> allItems;
private LiveData<List<sub_category>> itemsForCat;
public SubCategoryModel(Application application) {
super(application);
mySubCategoryRepository = new SubCategoryRepository(application);
allItems = mySubCategoryRepository.getAllItems(); //gets the data as list from DB
}
public LiveData<List<sub_category>> getAllItems()
{ return allItems; }
public LiveData<List<sub_category>> getItemListByCat(Integer catid)
{ itemsForCat = mySubCategoryRepository.getItemListByCat(catid);
return itemsForCat; }
}