谁能帮助我使用绑定适配器在 RecyclerView 中使用自定义视图(AAChartCore-Kotlin、ChartView)(显示一些speedcubing会话的数据)?
我已经尝试将 ChartView 添加到 RecyclerView 项目列表并为它实现一个 BindingAdapter,我调用 aa_drawChartWithChartModel(...),它应该在图表中显示数据,将项目的 chartModel 传递给它,但我不知道这是不是正确的。
目前,图表似乎已加载到视图中,但它是不可见的。
list_item_session.xml
<!-- ... -->
</LinearLayout>
<!-- ... -->
</HorizontalScrollView>
<!-- ... -->
</androidx.constraintlayout.widget.ConstraintLayout>
<!-- ... -->
<com.github.aachartmodel.aainfographics.aachartcreator.AAChartView
android:id="@+id/sessionChart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/margin_big"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/sessionEndDateTime"
app:layout_constraintTop_toTopOf="parent"
app:sessionChartView="@{session}">
</com.github.aachartmodel.aainfographics.aachartcreator.AAChartView>
</androidx.constraintlayout.widget.ConstraintLayout>
</HorizontalScrollView>
</LinearLayout>
会话适配器.kt
class SessionAdapter(private val clickListener: SessionListener) : ListAdapter<DataItem, RecyclerView.ViewHolder>(SessionsDiffCallback()) {
/* PROPERTIES */
// Coroutines
private val adapterScope = CoroutineScope(Dispatchers.Default)
/* COMPANION OBJECT */
companion object
{
private const val ITEM_VIEW_TYPE_PADDING = 0
private const val ITEM_VIEW_TYPE_SESSION = 1
}
/* ADAPTER METHODS */
fun submitListWithPadding(list: List<Session>?) {
adapterScope.launch {
val items = when(list) {
null -> listOf(DataItem.PaddingItem)
else -> list.map { DataItem.SessionItem(it) } + listOf(DataItem.PaddingItem)
}
withContext(Dispatchers.Main) {
submitList(items)
}
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return when(viewType) {
ITEM_VIEW_TYPE_PADDING -> TextViewHolder.from(parent)
ITEM_VIEW_TYPE_SESSION -> ViewHolder.from(parent)
else -> throw ClassCastException("Unknown viewType $viewType")
}
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
when(holder) {
is ViewHolder -> {
val sessionItem = getItem(position) as DataItem.SessionItem
holder.bind(clickListener, sessionItem.session)
}
}
}
override fun getItemViewType(position: Int): Int {
return when(getItem(position)) {
is DataItem.PaddingItem -> ITEM_VIEW_TYPE_PADDING
is DataItem.SessionItem -> ITEM_VIEW_TYPE_SESSION
}
}
/* VIEW HOLDER FOR SESSION ITEMS */
class ViewHolder private constructor(val binding: ListItemSessionBinding) : RecyclerView.ViewHolder(binding.root)
{
fun bind(clickListener: SessionListener, item: Session) {
binding.session = item
binding.clickListener = clickListener
binding.executePendingBindings()
}
companion object {
fun from(parent: ViewGroup): ViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
val binding = ListItemSessionBinding.inflate(layoutInflater, parent, false)
return ViewHolder(binding)
}
}
}
/* TEXT VIEW HOLDER FOR PADDING ITEM */
class TextViewHolder(view: View) : RecyclerView.ViewHolder(view)
{
companion object
{
fun from(parent: ViewGroup): TextViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
val view = layoutInflater.inflate(R.layout.list_item_padding, parent, false)
return TextViewHolder(view)
}
}
}
}
绑定适配器.kt
/* ... */
@BindingAdapter("sessionChartView")
fun AAChartView.setSessionChartView(item: Session?) {
item?.let {
aa_drawChartWithChartModel(it.chartModel)
}
}
数据项.kt
sealed class DataItem
{
abstract val id: Long
data class SessionItem(val session: Session) : DataItem()
{
override val id = session.id
}
object PaddingItem : DataItem()
{
override val id = Long.MIN_VALUE
}
}
SessionsDiffCallback.kt
class SessionsDiffCallback : DiffUtil.ItemCallback<DataItem>()
{
override fun areItemsTheSame(oldItem: DataItem, newItem: DataItem): Boolean {
return oldItem.id == newItem.id
}
override fun areContentsTheSame(oldItem: DataItem, newItem: DataItem): Boolean {
return oldItem == newItem
}
}