首页 > 解决方案 > recyclerview内的嵌套cardview渲染房间问题

问题描述

在房间内的 RecylerView 中使用嵌套的 CardView 会导致 CardView 不会被呈现,直到您随后向下滚动并在包含的 RecylerView 中向上滚动。然后子 CardView 按预期显示。

这是我在父级别中的 BindView 持有者,它使用其数据调用子级别适配器。我假设这是因为数据是从线程中获取的,因此父级没有为 CardView 呈现空间的信息 - 但我不知道该怎么做。

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

提前致谢。

code 公共类 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;
}

} 公共类 SubCategoryModel 扩展 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; }

}

标签: javaandroidandroid-recyclerviewandroid-roomnestedrecyclerview

解决方案


推荐阅读