我在尝试显示一个包含多个ViewHolder的回收器时遇到麻烦,我正在测试并发送一个自定义列表,该列表仅包含数据以显示第一个ViewType的列表。
这是BottomSheetClass
class PaymentStepsFragment : SuperBottomSheetFragment() {
val payments = ArrayList<Any>()
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
super.onCreateView(inflater, container, savedInstanceState)
return inflater.inflate(R.layout.fragment_steps_payment, container, false)
}
private fun RecyclerAnimator(recyclerView: RecyclerView, adapter: PurchaseViewAdapter) {
val itemAnimator = DefaultItemAnimator()
itemAnimator.addDuration = 1000
itemAnimator.removeDuration = 1000
recyclerView.itemAnimator = itemAnimator
recyclerView.adapter = adapter
recyclerView.layoutManager = LinearLayoutManager(context)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
payments.add(Purchase(0,R.drawable.ic_cart,"",""))
payments.add(Purchase(0,R.drawable.ic_favorite,"",""))
payments.add(Purchase(0,R.drawable.ic_arrow_back,"",""))
val recycler = view.findViewById<RecyclerView>(R.id.payment_rv)
val adapter = PurchaseViewAdapter(payments, context!!)
RecyclerAnimator(recycler, adapter)
}
override fun getCornerRadius() = context!!.resources.getDimension(R.dimen.sheet_rounded_corner)
override fun getStatusBarColor() = Color.RED
}
和我的RecyclerViewAdapter。
constructor(list: List<Any>, context: Context) : this() {
this.list = list
this.context = context
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return when(viewType){
0 -> {
context = parent.context
val menuItemLayoutView = LayoutInflater.from(parent.context).inflate(R.layout.fragment_card_payment, parent, false)
PaymentViewHolder(menuItemLayoutView)
}
else -> {
context = parent.context
val menuItemLayoutView = LayoutInflater.from(parent.context).inflate(R.layout.fragment_card_payment, parent, false)
PaymentViewHolder(menuItemLayoutView)
}
}
}
override fun getItemViewType(position: Int): Int {
val data = list?.get(position) as Purchase
return data.type
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
when(holder.itemViewType){
0 -> {
val holder_payment = holder as PaymentViewHolder
val menuItem = list!![position] as Purchase
holder_payment.paymentTimeline.marker = ContextCompat.getDrawable(context!!,menuItem.resource)
}
else -> {
val holder_payment = holder as PaymentViewHolder
val menuItem = list!![position] as Purchase
holder_payment.paymentTimeline.marker = ContextCompat.getDrawable(context!!,menuItem.resource)
}
}
}
但是,当我发送要显示的信息时,它总是将列表返回为空,我不知道问题出在哪里,它发送的数据似乎发送得很好。
RecyclerViewAdapter的构造函数将打印获得的列表,但是该列表不会显示要在RecyclerView中膨胀的DesiredViewHolders ...
根据要求,我在“RecyclerView”中添加了“布局”
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/view_bottom_sheet_attributes"
tools:background="@color/colorWhite">
<androidx.appcompat.widget.AppCompatTextView
android:layout_width="0dp"
android:layout_height="50dp"
android:id="@+id/text_attributes_heading"
android:gravity="center"
android:paddingLeft="12dp"
android:text="TEMPORAL"
android:textSize="18sp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<View
android:layout_width="0sp"
android:layout_height="1dp"
android:background="@color/colorDivider"
app:layout_constraintTop_toBottomOf="@id/text_attributes_heading"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<androidx.appcompat.widget.AppCompatImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/image_toggle"
android:layout_marginRight="5dp"
app:srcCompat="@drawable/ic_expand_less_black_24dp"
app:layout_constraintTop_toTopOf="@id/text_attributes_heading"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="@id/text_attributes_heading"/>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="0dp"
android:layout_height="0dp"
android:id="@+id/payment_rv"
android:fillViewport="true"
android:padding="5dp"
android:clipToPadding="false"
app:layout_constraintTop_toBottomOf="@id/text_attributes_heading"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toTopOf="@id/button_apply">
</androidx.recyclerview.widget.RecyclerView>
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:background="@color/colorDivider"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/payment_rv"/>
<com.google.android.material.button.MaterialButton
android:layout_width="match_parent"
android:layout_height="45dp"
android:id="@+id/button_apply"
android:layout_margin="5dp"
android:text="@string/purchase"
style="@style/Widget.MaterialComponents.Button"
android:insetTop="0dp"
android:insetBottom="0dp"
app:cornerRadius="0dp"
app:layout_constraintTop_toBottomOf="@id/payment_rv"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
还有ViewHolder的类型,似乎没有达到重写功能...
最佳答案
检查您的布局XML,看起来RecyclerView
View 被其他 View 隐藏了
关于java - Kotlin RecyclerView在BottomSheet中具有多个ViewHolders,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56064497/