@@ -8,13 +8,14 @@ import android.view.View
88import android.view.ViewGroup
99import android.widget.TextView
1010import com.blogspot.e_kanivets.moneytracker.R
11+ import com.blogspot.e_kanivets.moneytracker.databinding.ViewReportItemBinding
12+ import com.blogspot.e_kanivets.moneytracker.databinding.ViewReportItemExpBinding
1113import com.blogspot.e_kanivets.moneytracker.entity.RecordReportItem
12- import kotlinx.android.synthetic.main.view_report_item_exp.view.*
1314
1415class RecordReportAdapter (
15- private var items : MutableList <RecordReportItem >,
16- private var data : HashMap <RecordReportItem .ParentRow , List <RecordReportItem .ChildRow >>,
17- private val context : Context
16+ private var items : MutableList <RecordReportItem >,
17+ private var data : HashMap <RecordReportItem .ParentRow , List <RecordReportItem .ChildRow >>,
18+ private val context : Context
1819) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
1920
2021 private var red: Int = ContextCompat .getColor(context, R .color.red)
@@ -31,8 +32,16 @@ class RecordReportAdapter(
3132 }
3233
3334 override fun onCreateViewHolder (parent : ViewGroup , viewType : Int ) = when (viewType) {
34- TYPE_PARENT -> ParentViewHolder (LayoutInflater .from(context).inflate(R .layout.view_report_item_exp, parent, false ), ::changeItems, context)
35- TYPE_CHILD -> ChildViewHolder (LayoutInflater .from(context).inflate(R .layout.view_report_item, parent, false ))
35+ TYPE_PARENT -> ParentViewHolder (
36+ ViewReportItemExpBinding .inflate(LayoutInflater .from(context), parent, false ),
37+ ::changeItems,
38+ context
39+ )
40+
41+ TYPE_CHILD -> ChildViewHolder (
42+ ViewReportItemBinding .inflate(LayoutInflater .from(context), parent, false )
43+ )
44+
3645 else -> summaryViewHolder
3746 }
3847
@@ -47,6 +56,7 @@ class RecordReportAdapter(
4756 holder.tvTotal.text = row.amount
4857 holder.tvTotal.setTextColor(if (row.amount.first() != ' -' ) green else red)
4958 }
59+
5060 is ParentViewHolder -> {
5161 val row = items[posWithoutSummary] as RecordReportItem .ParentRow
5262 holder.tvCategory.text = row.category
@@ -56,7 +66,10 @@ class RecordReportAdapter(
5666 }
5767 }
5868
59- fun setData (items : MutableList <RecordReportItem >, data : HashMap <RecordReportItem .ParentRow , List <RecordReportItem .ChildRow >>) {
69+ fun setData (
70+ items : MutableList <RecordReportItem >,
71+ data : HashMap <RecordReportItem .ParentRow , List <RecordReportItem .ChildRow >>
72+ ) {
6073 this .items = items
6174 this .data = data
6275 notifyDataSetChanged()
@@ -81,7 +94,7 @@ class RecordReportAdapter(
8194
8295 private fun closeParentRow (parentRow : RecordReportItem .ParentRow , position : Int ) {
8396 val item = items.filterIndexed { index, _ -> index > position }
84- .find { it is RecordReportItem .ParentRow }
97+ .find { it is RecordReportItem .ParentRow }
8598
8699 val lastChildInd = if (item != null ) items.indexOf(item) else items.size
87100
@@ -109,31 +122,45 @@ class RecordReportAdapter(
109122
110123 private fun getPositionWithoutSummary (position : Int ) = position - 1
111124
112- class ParentViewHolder (view : View , changeItems : ((Int ) -> Unit ), context : Context ) : RecyclerView.ViewHolder(view) {
125+ class ParentViewHolder (
126+ binding : ViewReportItemExpBinding ,
127+ changeItems : ((Int ) -> Unit ),
128+ context : Context ,
129+ ) : RecyclerView.ViewHolder(binding.root) {
113130
114- var tvCategory: TextView = view .tvCategory
115- var tvTotal: TextView = view .tvTotal
131+ var tvCategory: TextView = binding .tvCategory
132+ var tvTotal: TextView = binding .tvTotal
116133
117134 private var isOpen: Boolean = false
118135
119136 init {
120- view .setOnClickListener {
137+ binding.root .setOnClickListener {
121138 if (isOpen) {
122- view.lowerDivider.visibility = View .GONE
123- view.ivArrow.setImageDrawable(ContextCompat .getDrawable(context, R .drawable.ic_arrow_downward_outline))
139+ binding.lowerDivider.visibility = View .GONE
140+ binding.ivArrow.setImageDrawable(
141+ ContextCompat .getDrawable(
142+ context,
143+ R .drawable.ic_arrow_downward_outline
144+ )
145+ )
124146 } else {
125- view.lowerDivider.visibility = View .VISIBLE
126- view.ivArrow.setImageDrawable(ContextCompat .getDrawable(context, R .drawable.ic_arrow_upward_outline))
147+ binding.lowerDivider.visibility = View .VISIBLE
148+ binding.ivArrow.setImageDrawable(
149+ ContextCompat .getDrawable(
150+ context,
151+ R .drawable.ic_arrow_upward_outline
152+ )
153+ )
127154 }
128155 isOpen = ! isOpen
129156 changeItems(adapterPosition)
130157 }
131158 }
132159 }
133160
134- class ChildViewHolder (view : View ) : RecyclerView.ViewHolder(view ) {
135- val tvCategory: TextView = view .tvCategory
136- val tvTotal: TextView = view .tvTotal
161+ class ChildViewHolder (binding : ViewReportItemBinding ) : RecyclerView.ViewHolder(binding.root ) {
162+ val tvCategory: TextView = binding .tvCategory
163+ val tvTotal: TextView = binding .tvTotal
137164 }
138165
139166 companion object {
@@ -142,5 +169,4 @@ class RecordReportAdapter(
142169 private const val TYPE_PARENT = 1
143170 private const val TYPE_CHILD = 2
144171 }
145-
146172}
0 commit comments