【发布时间】:2020-10-22 14:18:03
【问题描述】:
我的 RecyclerView ListAdapter 没有显示特定项目:
从图中可以看出,除了索引 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 处的项目具有值,就像所有其他项目一样。
任何帮助将不胜感激!
【问题讨论】:
-
我们是否可以看到问题列表的来源?
-
嗨@beastlyCoder,每个问题都使用retorfit查询并添加到LoadingFragment中的问题列表中。然后在 UnicornFragment 中传递。
标签: java android android-studio android-fragments kotlin