【问题标题】:Android expandable cardView for existing recyclerView adapter现有recyclerView适配器的Android可扩展cardView
【发布时间】:2019-08-29 18:38:51
【问题描述】:

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

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 的那种,我怎样才能让这个适配器同时处理内部和可扩展视图并获得所需的结果? (带有相关标题的卡片列表会展开以显示属于它们的其余详细信息)

抱歉,问题和代码太长,提前感谢您的帮助。

【问题讨论】:

    标签: android android-recyclerview material-design android-cardview


    【解决方案1】:

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

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

    【讨论】:

    • 你说得对,比我想象的要简单得多,非常感谢您为我指出。
    猜你喜欢
    • 1970-01-01
    • 2018-10-01
    • 1970-01-01
    • 2018-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-12
    • 1970-01-01
    相关资源
    最近更新 更多