Omar Hussein
Omar Hussein

Reputation: 1147

Android expandable cardView for existing recyclerView adapter

Alrighty, So I have an existing custom recycler view adapter that populates a recycler view using provided items, and sets attributes in a layout i have as follows, ill try to remove irrelevant items from code

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?

}
}

Now, I want to implement this library to use instead expandable card views so that I can only show for example the name and amount of transactions, and have the rest be expanded, this library can be found here https://github.com/AleSpero/ExpandableCardView , the library asks me to make a new layout with an inner_view attribute, I have done so, my layout is called transaction_list_expandable_view.xml and it looks like this

<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>

Now comes the problem, my custom TransactionAdapter only handles one layout, which is the one I called transaction_list_inner_view, how can I have this adapter instead handle both inner and expandable views and get the desired result? (a list of cards with relevant titles that expands to reveal the rest of the details belonging to them)

Sorry for the long question and code, thanks in advance for any help.

Upvotes: 1

Views: 852

Answers (1)

Guerneen4
Guerneen4

Reputation: 728

After checking the code of the library you're using, i think that you shouldn't be inflating the inner view manually (in your adapter) as that's the responsibility of the ExpandableCardView you should inflate the transaction_list_expandable_view.xml in your adapter. It would look like : val view = inflater.inflate(R.layout.transaction_list_expandable_view, p0, false)

For populating your inner view, i'm not sure whether the inner view is already inflated in the expandable one at the time of instantiating the CustomViewHolder. if it's the case just the switching of inner => expandable views above should do the trick.

Upvotes: 1

Related Questions