尽管调用了 OnBindViewHolder,但 RecyclewView Adapter 中的项目未显示
Items in RecyclewView Adapter are not being shown despite OnBindViewHolder being called
我有一个片段,它位于具有 Recycler View 的视图寻呼机中。尽管实际上调用了 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 方法,但我只得到一个空屏幕,正如您在此处看到的那样:
感谢任何帮助!
根据您的 Fragment XML,您的 RecyclerView 有 android:layout_height="wrap_content"
。但最初 RecyclerView 是空的,这使得它的高度为零。因此,您必须将其更改为 android:layout_height="0dp"
并添加 app:layout_constraintBottom_toBottomOf="parent"
@hakim 在评论中发表的内容让我意识到了这个问题。由于我的视图寻呼机位于 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 时,我能够在回收站视图中看到所有项目。非常感谢大家的帮助!
我有一个片段,它位于具有 Recycler View 的视图寻呼机中。尽管实际上调用了 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 方法,但我只得到一个空屏幕,正如您在此处看到的那样:
感谢任何帮助!
根据您的 Fragment XML,您的 RecyclerView 有 android:layout_height="wrap_content"
。但最初 RecyclerView 是空的,这使得它的高度为零。因此,您必须将其更改为 android:layout_height="0dp"
并添加 app:layout_constraintBottom_toBottomOf="parent"
@hakim 在评论中发表的内容让我意识到了这个问题。由于我的视图寻呼机位于 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 时,我能够在回收站视图中看到所有项目。非常感谢大家的帮助!