There are times that we need to create List with different view items. For example like the one posted above.
Yes. We can create it with Recycler View and using more than one List Item view.
- Create the List Items
For above app, I created two kind of List Item layout :
listitem_income. xml
listitem_expense.xml - Create POJO/Model
Based on this ListItem layout, I also created POJO/Model class to hold the content for the ListItem
enum class incomeCategory { Salary, Freelance, Bonus }
class Income(
val title: incomeCategory,
val wallet: String,
val amount: Int
)enum class expenseCategory { Groceries, Transportation, Food, Misc }
class Expense(
val category: expenseCategory,
val wallet: String,
val amount: Int
)
3. Create RecyclerView Adapter
And the magic is happened here.
First of all, we need to create multiple ViewHolder class inside Adapter. For this case, I created two type of View Holder inside the RecyclerView.Adapter class
class IncomeViewHolder(val binding: LisitemIncomeBinding) :
RecyclerView.ViewHolder(binding.root)
class ExpenseViewHolder(val binding: ListitemExpenseBinding) :
RecyclerView.ViewHolder(binding.root)