Skip to content

Commit

Permalink
[MOD/#17] listAdapter 관련하여 코드 1차 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
0zlrlo committed May 19, 2023
1 parent 45308e0 commit 4df675c
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.release.keyneez.domain.model

data class Activity(
val id: Int,
val background: Int,
val category: String,
val title: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,11 @@ import android.view.View
import com.google.android.material.tabs.TabLayoutMediator
import com.release.keyneez.R
import com.release.keyneez.databinding.FragmentExploreBinding
import com.release.keyneez.databinding.FragmentExplorePopularBinding
import com.release.keyneez.presentation.main.search.SearchActivity
import com.release.keyneez.util.binding.BindingFragment
import com.release.keyneez.util.extension.setOnSingleClickListener

class ExploreFragment : BindingFragment<FragmentExploreBinding>(R.layout.fragment_explore) {
private var _binding: FragmentExplorePopularBinding? = null
get() = _binding!!

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

Expand Down Expand Up @@ -43,11 +39,6 @@ class ExploreFragment : BindingFragment<FragmentExploreBinding>(R.layout.fragmen
}
}

override fun onDestroyView() {
super.onDestroyView()
_binding = null
}

companion object {
@JvmStatic
fun newInstance() = ExploreFragment()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import com.release.keyneez.util.binding.BindingFragment

class PopularFragment :
BindingFragment<FragmentExplorePopularBinding>(R.layout.fragment_explore_popular) {
private var _binding: FragmentExplorePopularBinding? = null
get() = _binding!!

private val viewModel: PopularViewModel by viewModels()
private lateinit var popularAdapter: PopularAdapter
Expand All @@ -32,11 +30,6 @@ class PopularFragment :
binding.rvExplorePopular.adapter = popularAdapter
}

override fun onDestroyView() {
super.onDestroyView()
_binding = null
}

companion object {
@JvmStatic
fun newInstance() = PopularFragment()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,31 @@ class PopularViewModel : ViewModel() {
private fun getPopularActivityList() {
val mainList = listOf(
Activity(
id = 1,
background = R.drawable.img_explore_background,
title = "행주산성\n맛집 투어",
category = "진로",
date = "%s-%s",
liked = true
),
Activity(
id = 1,
background = R.drawable.img_explore_background,
title = "행주산성\n맛집 투어",
category = "진로",
date = "%s-%s",
liked = true
),
Activity(
id = 1,
background = R.drawable.img_explore_background,
title = "행주산성\n맛집 투어",
category = "진로",
date = "%s-%s",
liked = true
),
Activity(
id = 1,
background = R.drawable.img_explore_background,
title = "행주산성\n맛집 투어",
category = "진로",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package com.release.keyneez.presentation.main.explore.recent

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import coil.load
import com.release.keyneez.databinding.ItemExploreContentBinding
import com.release.keyneez.domain.model.Activity
import com.release.keyneez.util.DiffCallback

class RecentAdapter : RecyclerView.Adapter<RecentAdapter.RecentViewHolder>() {
class RecentAdapter : ListAdapter<Activity, RecyclerView.ViewHolder>(diffUtil) {
var data = listOf<Activity>()

class RecentViewHolder(private val binding: ItemExploreContentBinding) :
Expand All @@ -23,15 +25,27 @@ class RecentAdapter : RecyclerView.Adapter<RecentAdapter.RecentViewHolder>() {
}
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecentViewHolder {
val binding =
ItemExploreContentBinding.inflate(LayoutInflater.from(parent.context), parent, false)
return RecentViewHolder(binding)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return RecentViewHolder(
ItemExploreContentBinding.inflate(
LayoutInflater.from(parent.context),
parent,
false
)
)
}

override fun getItemCount(): Int = data.size
override fun getItemCount(): Int = super.getItemCount()

override fun onBindViewHolder(holder: RecentViewHolder, position: Int) {
holder.bind(data[position])
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
if (holder is RecentViewHolder) holder.bind(getItem(position))
}

companion object {
private val diffUtil =
DiffCallback<Activity>(
onItemsTheSame = { old, new -> old.id == new.id },
onContentsTheSame = { old, new -> old == new }
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import com.release.keyneez.util.binding.BindingFragment

class RecentFragment :
BindingFragment<FragmentExploreRecentBinding>(R.layout.fragment_explore_recent) {
private var _binding: FragmentExploreRecentBinding? = null
get() = _binding!!

private val viewModel: RecentViewModel by viewModels()
private lateinit var recentAdapter: RecentAdapter
Expand All @@ -32,11 +30,6 @@ class RecentFragment :
binding.rvExploreRecent.adapter = recentAdapter
}

override fun onDestroyView() {
super.onDestroyView()
_binding = null
}

companion object {
@JvmStatic
fun newInstance() = RecentFragment()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,31 @@ class RecentViewModel : ViewModel() {
private fun getRecentActivityList() {
val mainList = listOf(
Activity(
id = 1,
background = R.drawable.img_explore_background,
title = "행주산성\n맛집 투어",
category = "진로",
date = "%s-%s",
liked = true
),
Activity(
id = 1,
background = R.drawable.img_explore_background,
title = "행주산성\n맛집 투어",
category = "진로",
date = "%s-%s",
liked = true
),
Activity(
id = 1,
background = R.drawable.img_explore_background,
title = "행주산성\n맛집 투어",
category = "진로",
date = "%s-%s",
liked = true
),
Activity(
id = 1,
background = R.drawable.img_explore_background,
title = "행주산성\n맛집 투어",
category = "진로",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ abstract class BindingFragment<T : ViewDataBinding>(
@LayoutRes private val layoutRes: Int,
) : Fragment() {
private var _binding: T? = null
protected val binding get() = requireNotNull(_binding) { getString(R.string.binding_error) }
protected val binding get() = requireNotNull(_binding) { getString(R.string.binding_error_msg) }

override fun onCreateView(
inflater: LayoutInflater,
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
<string name="app_name">Keyneez</string>

<!-- error -->
<string name="binding_error">Binding not initialized to reference the view.</string>
<string name="binding_error_msg">Binding not initialized to reference the view.</string>
<string name="unknown_error_msg">알 수 없는 오류가 발생했습니다.</string>
<string name="view_type_error_msg">Unknown View Type : %d</string>

<!-- main -->
<string name="main_menu_home">홈</string>
Expand Down

0 comments on commit 4df675c

Please sign in to comment.