Skip to content

Commit

Permalink
[REFACTOR] dnd-side-project#140 - inflater 중복 생성 방지
Browse files Browse the repository at this point in the history
  • Loading branch information
dabinKim-0318 committed Aug 17, 2022
1 parent bb2da22 commit c5e41cc
Showing 1 changed file with 11 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.fork.spoonfeed.presentation.ui.home.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
Expand All @@ -14,8 +15,10 @@ class MyLikePolicyAdapter(
private val clickListener: (ResponseUserLikePolicyData.Data.Policy) -> Unit
) : ListAdapter<ResponseUserLikePolicyData.Data.Policy, MyLikePolicyAdapter.MyLikePolicyHomeViewHolder>(SimpleDiffUtil()) {

inner class MyLikePolicyHomeViewHolder(private val binding: ItemInterastedPolicyBinding) : RecyclerView.ViewHolder(binding.root) {
fun onBind(data: ResponseUserLikePolicyData.Data.Policy) {
private lateinit var inflater: LayoutInflater

class MyLikePolicyHomeViewHolder(private val binding: ItemInterastedPolicyBinding) : RecyclerView.ViewHolder(binding.root) {
fun onBind(data: ResponseUserLikePolicyData.Data.Policy, clickListener: (ResponseUserLikePolicyData.Data.Policy) -> Unit) {
binding.apply {
tvItemPolicyTitle.text = data.name
tvItemCategory.text = data.category
Expand All @@ -34,15 +37,16 @@ class MyLikePolicyAdapter(
tvItemCategory.setBackgroundResource(R.drawable.bg_dwelling_blue_radius_4dp)
}

root.setOnClickListener {
clickListener(data)
}
root.setOnClickListener { clickListener(data) }
}
}
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyLikePolicyAdapter.MyLikePolicyHomeViewHolder {
val binding = ItemInterastedPolicyBinding.inflate(LayoutInflater.from(parent.context), parent, false)
if (!::inflater.isInitialized) //변수를 객체로 엑세스하여 객체에 대한 속성 참조
inflater = LayoutInflater.from(parent.context)
val binding = ItemInterastedPolicyBinding.inflate(inflater, parent, false)

return MyLikePolicyHomeViewHolder(binding)
}

Expand All @@ -54,7 +58,7 @@ class MyLikePolicyAdapter(
}

override fun onBindViewHolder(holder: MyLikePolicyAdapter.MyLikePolicyHomeViewHolder, position: Int) {
holder.onBind(getItem(position))
holder.onBind(getItem(position), clickListener)

}

Expand Down

0 comments on commit c5e41cc

Please sign in to comment.