【问题标题】:Items in RecyclewView Adapter are not being shown despite OnBindViewHolder being called尽管调用了 OnBindViewHolder,但未显示 RecyclewView 适配器中的项目
【发布时间】:2021-01-31 02:43:30
【问题描述】:

我有一个片段,它位于具有回收器视图的视图寻呼机内。尽管实际上调用了 onbindviewholder,但我的回收站视图上的项目没有显示,我一直遇到这样的问题。我不知道可能是什么问题。

这是我的片段的 XML:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools">

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/rv_dishes"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    tools:listitem="@layout/dish_rv_item"
    />
</androidx.constraintlayout.widget.ConstraintLayout>

这是我的片段的代码:

class CategoryFragment(val dishes: ArrayList<Dish>):Fragment() {

private val binding by viewBinding(CategoryFragmentBinding::bind)

private val rvAdapter by lazy {
    CategoryRecyclerAdapter()
}

override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View {
    val view = CategoryFragmentBinding.inflate(inflater,container,false)
    return view.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    setUpRecyclerView()
    rvAdapter.addAll(dishes)
}


private fun setUpRecyclerView(){

    val linearLayoutManager = LinearLayoutManager(activity)
    linearLayoutManager.orientation = LinearLayoutManager.VERTICAL
    binding.rvDishes.layoutManager = linearLayoutManager
    binding.rvDishes.adapter = rvAdapter

}

companion object{
    fun newInstance(dishes:ArrayList<Dish>) = CategoryFragment(dishes)
} 
}

最后是我的适配器的代码:

class CategoryRecyclerAdapter(private val dishes:ArrayList<Dish> = ArrayList()): RecyclerView.Adapter<CategoryRecyclerAdapter.ViewHolder>() {


fun addAll(list: List<Dish>){
    dishes.clear()
    list.forEach {
       dishes.add(it)
    }
    notifyDataSetChanged()
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
    val view = LayoutInflater.from(parent.context).inflate(R.layout.dish_rv_item,parent,false)
    val binding = DishRvItemBinding.bind(view)
    return ViewHolder(binding)
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    Log.d("TAG", "binding view holder")
    holder.bindView(dishes[position])
}

override fun getItemCount(): Int = dishes.size

inner class ViewHolder(private val binding: DishRvItemBinding):RecyclerView.ViewHolder(binding.root){

    fun bindView(dish: Dish){
        binding.apply {
            tvDishName.text = dish.name
        }
    }
}
}

我已经检查了我的项目列表不为空,项目计数不为 0,我已将我的回收站视图的布局高度和宽度更改为 0dp,匹配父项并包装内容。我调试了它并调用了 bindView 方法,但我只得到一个空屏幕,正如您在此处看到的那样:

感谢任何帮助!

【问题讨论】:

  • 您检查过您的 tvDishName 视图可见性吗?
  • 请同时分享您的 ViewPager 适配器源代码和dish_rv_item.xml 代码,有可能是项目布局和适配器问题
  • 尝试在开发者选项中显示布局边界,看看是否真的添加了视图...

标签: android kotlin android-recyclerview android-constraintlayout android-viewbinding


【解决方案1】:

@hakim 在 cmets 上发布的内容让我意识到了问题所在。由于我的视图寻呼机位于 BottomSheetDialogFragment 内,因此我必须更改 XML 的高度和宽度才能使其正常工作。所以,我将我的 BottomSheetDialogFragment 的 XML 更改为这个,它就像一个魅力:

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat 
     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="match_parent"
     android:orientation="vertical">

<com.google.android.material.tabs.TabLayout
    android:id="@+id/tab_categories"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<androidx.viewpager2.widget.ViewPager2
    android:id="@+id/pager_content"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
</androidx.appcompat.widget.LinearLayoutCompat>

当我将宽度更改为 match_parent 并将高度更改为 wrap_content 时,我能够可视化回收站视图上的所有项目。非常感谢大家的帮助!

【讨论】:

    【解决方案2】:

    根据您的 Fragment XML,您的 RecyclerView 具有 android:layout_height="wrap_content"。但最初 RecyclerView 是空的,这使得它的高度为零。因此,您必须将其更改为 android:layout_height="0dp" 并添加 app:layout_constraintBottom_toBottomOf="parent"

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-15
    • 1970-01-01
    • 2022-08-12
    • 2017-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多