Skip to content

Commit

Permalink
mod/#5: PR 적용 - ViewHolder
Browse files Browse the repository at this point in the history
  • Loading branch information
hyeeum committed Apr 26, 2024
1 parent 3d31950 commit ed096f8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 39 deletions.
41 changes: 13 additions & 28 deletions app/src/main/java/com/sopt/now/HomeListAdapter.kt
Original file line number Diff line number Diff line change
@@ -1,49 +1,34 @@
package com.sopt.now

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import java.lang.RuntimeException
import com.sopt.now.databinding.ItemFriendBinding
import com.sopt.now.databinding.ItemUserBinding

class HomeListAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
private var homeListList: List<HomeList> = emptyList()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val adapterLayout : View?
val inflater = LayoutInflater.from(parent.context)
val bindingUser = ItemUserBinding.inflate(inflater, parent, false)
val bindingFriend = ItemFriendBinding.inflate(inflater, parent, false)
return when(viewType){
HomeList.VIEW_TYPE_USER -> {
adapterLayout = LayoutInflater.from(parent.context)
.inflate(R.layout.item_user,parent,false)
UserViewHolder(adapterLayout)
UserViewHolder(bindingUser)
}
else -> {
adapterLayout = LayoutInflater.from(parent.context)
.inflate(R.layout.item_friend,parent,false)
FriendViewHolder(adapterLayout)
FriendViewHolder(bindingFriend)
}
}
}

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
val item = homeListList.getOrNull(position)
if (item != null) {
when(item.viewType){
HomeList.VIEW_TYPE_USER -> {
with(holder as UserViewHolder) {
ivProfile.setImageResource(item.profileImage)
tvName.text = item.name
tvSelfDescription.text = item.selfDescription
setIsRecyclable(false)
}
}
else -> {
with(holder as FriendViewHolder) {
ivProfile.setImageResource(item.profileImage)
tvName.text = item.name
tvSelfDescription.text = item.selfDescription
setIsRecyclable(false)
}
}
when(homeListList.getOrNull(position)?.viewType){
HomeList.VIEW_TYPE_USER -> {
(holder as UserViewHolder).onBind(homeListList[position])
}
else -> {
(holder as FriendViewHolder).onBind(homeListList[position])
}
}
}
Expand Down
29 changes: 18 additions & 11 deletions app/src/main/java/com/sopt/now/HomeListViewHolder.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
package com.sopt.now

import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.sopt.now.databinding.ItemFriendBinding
import com.sopt.now.databinding.ItemUserBinding

class FriendViewHolder(private val view: View) : RecyclerView.ViewHolder(view) {
val ivProfile: ImageView = view.findViewById(R.id.iv_profile)
val tvName: TextView = view.findViewById(R.id.tv_name)
val tvSelfDescription: TextView = view.findViewById(R.id.tv_self_description)
class FriendViewHolder(private val binding:ItemFriendBinding) : RecyclerView.ViewHolder(binding.root) {
fun onBind(friendData: HomeList) {
binding.run {
ivProfile.setImageResource(friendData.profileImage)
tvName.text = friendData.name
tvSelfDescription.text = friendData.selfDescription
}
}
}

class UserViewHolder(private val view: View) : RecyclerView.ViewHolder(view) {
val ivProfile: ImageView = view.findViewById(R.id.iv_profile)
val tvName: TextView = view.findViewById(R.id.tv_name)
val tvSelfDescription: TextView = view.findViewById(R.id.tv_self_description)
class UserViewHolder(private val binding:ItemUserBinding) : RecyclerView.ViewHolder(binding.root) {
fun onBind(friendData: HomeList) {
binding.run {
ivProfile.setImageResource(friendData.profileImage)
tvName.text = friendData.name
tvSelfDescription.text = friendData.selfDescription
}
}
}

0 comments on commit ed096f8

Please sign in to comment.