Android 现有 recyclerView 适配器的可扩展 cardView

Android expandable cardView for existing recyclerView adapter

好吧,所以我有一个现有的自定义回收器视图适配器,它使用提供的项目填充回收器视图,并在我拥有的布局中设置属性如下,我将尝试从代码中删除不相关的项目

class TransactionAdapter(val context: Context, var transactions: List<Transaction>) :
androidx.recyclerview.widget.RecyclerView.Adapter<TransactionAdapter.CustomViewHolder>() {
override fun onCreateViewHolder(p0: ViewGroup, p1: Int): CustomViewHolder {
    val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
    val view = inflater.inflate(R.layout.transaction_list_inner_view, p0, false)
    return CustomViewHolder(view)
}



override fun onBindViewHolder(p0: CustomViewHolder, p1: Int) {
    p0.transactionNameTextView?.text = transactions[p1].title
    p0.transactionAmountTextView?.text = transactions[p1].amount
    if (!transactions[p1].location.isNullOrEmpty()) {
        p0.transactionLocationTextView?.text = transactions[p1].location
    } else {
        p0.transactionLocationTextView?.text = "N/A"
    }
    p0.transactionTimeTextView?.text = transactions[p1].createdAt
    p0.transactionDeleteButton?.setOnClickListener { println("working delete") }
}


class CustomViewHolder(v: View) : androidx.recyclerview.widget.RecyclerView.ViewHolder(v) {
    val transactionNameTextView: TextView? = v.findViewById(R.id.transactionNameTextView) as TextView?
    val transactionAmountTextView: TextView? = v.findViewById(R.id.transactionAmountTextView) as TextView?
    val transactionLocationTextView: TextView? = v.findViewById(R.id.transactionLocationTextView) as TextView?
    val transactionTimeTextView: TextView? = v.findViewById(R.id.transactionTimeTextView) as TextView?
    val transactionDeleteButton: AppCompatButton? = v.findViewById(R.id.transactionDeleteButton) as AppCompatButton?

}
}

现在,我想实现这个库来使用可扩展的卡片视图,这样我就可以只显示交易的名称和金额,其余的都可以扩展,这个库可以在这里找到https://github.com/AleSpero/ExpandableCardView ,图书馆要求我制作一个带有 inner_view 属性的新布局,我已经这样做了,我的布局被称为 transaction_list_expandable_view.xml 它看起来像这样

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                               xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
                                               android:layout_height="wrap_content"
                                               xmlns:app="http://schemas.android.com/apk/res-auto"
                                               tools:ignore="ExtraText">
<com.alespero.expandablecardview.ExpandableCardView
        android:id="@+id/transaction"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:title="testt"
        app:inner_view="@layout/transaction_list_inner_view"
        app:expandOnClick="true"
        app:animationDuration="300"
        app:startExpanded="false"
/>
</androidx.constraintlayout.widget.ConstraintLayout>

现在问题来了,我的自定义 TransactionAdapter 只处理一种布局,也就是我称为 transaction_list_inner_view 的布局,我怎样才能让这个适配器同时处理内部视图和可扩展视图并获得所需的结果? (具有相关标题的卡片列表,展开以显示属于它们的其余详细信息)

抱歉问题和代码太长,在此先感谢您的帮助。

检查了您正在使用的库的代码后,我认为您不应该(在您的适配器中)手动膨胀内部视图,因为这是 ExpandableCardView 的责任,您应该膨胀transaction_list_expandable_view.xml 在你的适配器中。 它看起来像: val view = inflater.inflate(R.layout.transaction_list_expandable_view, p0, false)

为了填充您的内部视图,我不确定在实例化 CustomViewHolder 时内部视图是否已经在可扩展视图中膨胀。 如果是这种情况,只需切换上面的内部 => 可扩展视图就可以解决问题。