From c5e41ccd5ff3036500952059ff9e03817fd4d288 Mon Sep 17 00:00:00 2001 From: DaBin <84564695+dabinKim-0318@users.noreply.github.com> Date: Wed, 17 Aug 2022 16:21:27 +0900 Subject: [PATCH] =?UTF-8?q?[REFACTOR]=20#140=20-=20inflater=20=EC=A4=91?= =?UTF-8?q?=EB=B3=B5=20=EC=83=9D=EC=84=B1=20=EB=B0=A9=EC=A7=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ui/home/adapter/MyLikePolicyHomeAdapter.kt | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/com/fork/spoonfeed/presentation/ui/home/adapter/MyLikePolicyHomeAdapter.kt b/app/src/main/java/com/fork/spoonfeed/presentation/ui/home/adapter/MyLikePolicyHomeAdapter.kt index f902d7c..635cefb 100644 --- a/app/src/main/java/com/fork/spoonfeed/presentation/ui/home/adapter/MyLikePolicyHomeAdapter.kt +++ b/app/src/main/java/com/fork/spoonfeed/presentation/ui/home/adapter/MyLikePolicyHomeAdapter.kt @@ -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 @@ -14,8 +15,10 @@ class MyLikePolicyAdapter( private val clickListener: (ResponseUserLikePolicyData.Data.Policy) -> Unit ) : ListAdapter(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 @@ -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) } @@ -54,7 +58,7 @@ class MyLikePolicyAdapter( } override fun onBindViewHolder(holder: MyLikePolicyAdapter.MyLikePolicyHomeViewHolder, position: Int) { - holder.onBind(getItem(position)) + holder.onBind(getItem(position), clickListener) }