gpt4 book ai didi

java - RecyclerView ListAdapter不显示特定项目

转载 作者:行者123 更新时间:2023-12-02 13:41:43 26 4
gpt4 key购买 nike

我的RecyclerView ListAdapter没有显示特定项目:
enter image description here
从图中可以看出,除了索引8中的项目以外,列表的所有项目都将显示。
我通过日志检查时,此项目没有任何问题。
而且,如果我提供了一个内容不同的新列表,则会显示一些项目,而没有显示。
这是我的代码:
片段:

class UnicornFragment : Fragment() {

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val binding : FragmentUnicornBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_unicorn, container, false)
binding.lifecycleOwner = this

val unicornList = UnicornFragmentArgs.fromBundle(arguments!!).unicornsList

val adapter = UnicornAdapter()
binding.unicornsView.adapter = adapter
Log.e("Unicorn Fragment", "${unicornList.toMutableList().size}")
adapter.submitList(unicornList.toMutableList())

return binding.root
}
}
适配器:
class UnicornAdapter : androidx.recyclerview.widget.ListAdapter<Unicorn, UnicornAdapter.UnicornViewHolder> (DiffCallback){

override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): UnicornViewHolder {
return UnicornViewHolder(ItemUnicornBinding.inflate(LayoutInflater.from(parent.context)))
}

class UnicornViewHolder(private var binding : ItemUnicornBinding) : RecyclerView.ViewHolder(binding.root){
fun bind(unicorn: Unicorn) {
binding.unicorn = unicorn

binding.executePendingBindings()

Log.e("UnicornAdapter", "Question : ${unicorn.questionText}, Answer : ${unicorn.answerText}")
}

}

override fun onBindViewHolder(holder: UnicornViewHolder, position: Int) {
holder.bind(getItem(position))
}

companion object DiffCallback : DiffUtil.ItemCallback<Unicorn>() {
override fun areItemsTheSame(oldItem: Unicorn, newItem: Unicorn): Boolean {
return oldItem === newItem
}

override fun areContentsTheSame(oldItem: Unicorn, newItem: Unicorn): Boolean {
return oldItem.questionText == newItem.questionText
}

}
}
fragment_unicorn.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<data>

</data>

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/unicorns_view"
android:layout_width="0dp"
android:layout_height="0dp"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />


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

<data>
<variable
name="unicorn"
type="com.example.unicorn.network.Unicorn" />
</data>

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary">

<TextView
android:id="@+id/question_text_view"
style="@style/MainHeading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:fontFamily="sans-serif-black"
android:gravity="start"
android:text="@{unicorn.questionText}"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="This is a sample Question" />

<TextView
android:id="@+id/answer_text_view"
style="@style/SubHeading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:gravity="start"
android:textSize="20sp"
android:text="@{unicorn.answerText}"
app:layout_constraintStart_toStartOf="@+id/question_text_view"
app:layout_constraintTop_toBottomOf="@+id/question_text_view"
tools:text="Sample Answer" />


</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
日志:
2020-07-01 19:17:51.093 16433-16433/com.example.unicorn E/UnicornAdapter: Question : 1. His brother was accused ……theft., Answer : c)   Of 
2020-07-01 19:17:51.141 16433-16433/com.example.unicorn E/UnicornAdapter: Question : 2. Avail yourself …….this opportunity., Answer : a) Of
2020-07-01 19:17:51.187 16433-16433/com.example.unicorn E/UnicornAdapter: Question : 3. He is bent ……mischief., Answer : b) On
2020-07-01 19:17:51.228 16433-16433/com.example.unicorn E/UnicornAdapter: Question : 4. I cannot comply ……..your request, Answer : d) With
2020-07-01 19:17:51.275 16433-16433/com.example.unicorn E/UnicornAdapter: Question : 5. He is devoid ………all sense of decency. , Answer : b) Of
2020-07-01 19:17:51.324 16433-16433/com.example.unicorn E/UnicornAdapter: Question : 6. He is endowed …….a strong will., Answer : a) With
2020-07-01 19:17:51.369 16433-16433/com.example.unicorn E/UnicornAdapter: Question : 7. Portia was the heiress …….her father’s property, Answer : c) To
2020-07-01 19:17:51.412 16433-16433/com.example.unicorn E/UnicornAdapter: Question : 8. I congratulate you …….your brilliant success. , Answer : b) On
2020-07-01 19:17:54.585 16433-16433/com.example.unicorn E/UnicornAdapter: Question : 9. He is blind …….his drawbacks. , Answer : a) To
2020-07-01 19:17:54.679 16433-16433/com.example.unicorn E/UnicornAdapter: Question : 10. He did not agree ……..my proposal. , Answer : b) To
2020-07-01 19:17:58.187 16433-16433/com.example.unicorn E/UnicornAdapter: Question : 11. He as well as you ……guilty., Answer : b) Is
2020-07-01 19:17:58.260 16433-16433/com.example.unicorn E/UnicornAdapter: Question : 12. They are working to live. Identify the verb in this sentence., Answer : b) Are working
2020-07-01 19:17:58.328 16433-16433/com.example.unicorn E/UnicornAdapter: Question : 14. An example for infinite verb is:, Answer : b
可以看出,索引8中的项目具有值,就像所有其他项目一样。
任何帮助将不胜感激!

最佳答案

尝试在适配器 View 中覆盖getItemViewType以返回位置

@Override
public int getItemViewType(int position) { return position; }

关于java - RecyclerView ListAdapter不显示特定项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62688603/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com