0

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; }

}

  • Can you share the screen record of the issue? – Ashique Bava Nov 08 '21 at 05:09
  • Sure - sorry about the quality - that's what came out of the emulator video from logcat! https://www.dropbox.com/s/ujphb11vs4n1fad/Recyclerview%20nested%20items%20appear%20after%20scroll.mp4?dl=0 – James Harvey Nov 10 '21 at 06:20
  • Added the category (parent) and subcategory (child) repository and model code if that helps. I'm assuming there must be some way to link the data or make the model return data in a more timely manner. – James Harvey Nov 10 '21 at 06:22
  • As can be seen from the video - Accommodation has no children on first load, then after scroll the children are shown. – James Harvey Nov 10 '21 at 06:28

0 Answers0