Skip to content

Commit

Permalink
feat/#5: 멀티뷰 리싸이클러뷰 생성
Browse files Browse the repository at this point in the history
  • Loading branch information
hyeeum committed Apr 13, 2024
1 parent 3205e16 commit 40c4f30
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 25 deletions.
8 changes: 7 additions & 1 deletion app/src/main/java/com/sopt/now/Friend.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,10 @@ data class Friend(
@DrawableRes val profileImage: Int,
val name: String,
val selfDescription: String,
)
val viewType : Int
) {
companion object {
const val VIEW_TYPE_USER = 0
const val VIEW_TYPE_FRIEND = 1
}
}
48 changes: 38 additions & 10 deletions app/src/main/java/com/sopt/now/FriendAdapter.kt
Original file line number Diff line number Diff line change
@@ -1,26 +1,54 @@
package com.sopt.now

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

class FriendAdapter() : RecyclerView.Adapter<FriendViewHolder>() {
class FriendAdapter() : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
// 임시의 빈 리스트
private var friendList: List<Friend> = emptyList()

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FriendViewHolder {
val inflater = LayoutInflater.from(parent.context)
val binding = ItemFriendBinding.inflate(inflater, parent, false)
return FriendViewHolder(binding)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val adapterLayout : View?
return when(viewType){
Friend.VIEW_TYPE_USER -> {
adapterLayout = LayoutInflater.from(parent.context)
.inflate(R.layout.item_user,parent,false)
UserViewHolder(adapterLayout)
}
Friend.VIEW_TYPE_FRIEND -> {
adapterLayout = LayoutInflater.from(parent.context)
.inflate(R.layout.item_friend,parent,false)
FriendViewHolder(adapterLayout)
}
else -> throw RuntimeException("알 수 없는 뷰타입입니다.")
}
}

override fun onBindViewHolder(holder: FriendViewHolder, position: Int) {
holder.onBind(friendList[position])
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
val item = friendList[position]
when(item.viewType){
Friend.VIEW_TYPE_USER -> {
(holder as UserViewHolder).ivProfile.setImageResource(item.profileImage)
holder.tvName.text = item.name
holder.tvSelfDescription.text = item.selfDescription
holder.setIsRecyclable(false)
}
Friend.VIEW_TYPE_FRIEND -> {
(holder as FriendViewHolder).ivProfile.setImageResource(item.profileImage)
holder.tvName.text = item.name
holder.tvSelfDescription.text = item.selfDescription
holder.setIsRecyclable(false)
}
else -> throw RuntimeException("알 수 없는 뷰타입입니다.")
}
}

override fun getItemCount() = friendList.size

override fun getItemViewType(position: Int): Int {
return friendList[position].viewType
}
fun setFriendList(friendList: List<Friend>) {
this.friendList = friendList.toList()
notifyDataSetChanged()
Expand Down
21 changes: 13 additions & 8 deletions app/src/main/java/com/sopt/now/FriendViewHolder.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
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 binding: ItemFriendBinding) : RecyclerView.ViewHolder(binding.root) {
fun onBind(friendData: Friend) {
binding.run {
ivProfile.setImageResource(friendData.profileImage)
tvName.text = friendData.name
tvSelfDescription.text = friendData.selfDescription
}
}
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 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)
}
15 changes: 12 additions & 3 deletions app/src/main/java/com/sopt/now/HomeFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,28 @@ class HomeFragment : Fragment() {

private val mockFriendList = listOf<Friend>(
Friend(
profileImage = R.drawable.ic_home_white_24,
profileImage = R.drawable.main,
name = "송혜음",
selfDescription = "멀티 뷰 리싸이클러뷰!",
0
),
Friend(
profileImage = R.drawable.main,
name = "이의경",
selfDescription = "다들 빨리 끝내고 뒤풀이 가고 싶지? ㅎㅎ 아직 반도 안왔어 ^&^",
1
),
Friend(
profileImage = R.drawable.ic_home_white_24,
profileImage = R.drawable.main,
name = "우상욱",
selfDescription = "나보다 안드 잘하는 사람 있으면 나와봐",
1
),
Friend(
profileImage = R.drawable.ic_home_white_24,
profileImage = R.drawable.main,
name = "배지현",
selfDescription = "표정 풀자 ^^",
1
),
)
override fun onCreateView(
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/sopt/now/LoginActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ class LoginActivity : AppCompatActivity() {
}
private fun sendData(id:String,pw:String,nick:String){
if (isLoginAvailable(id, pw)) {
var mypagefragment = MyPageFragment()
var bundle = Bundle()
val mypagefragment = MyPageFragment()
val bundle = Bundle()
bundle.putString("id",id)
bundle.putString("pw",pw)
bundle.putString("nick",nick)
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/item_friend.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
android:layout_marginStart="20dp"
android:scaleType="centerCrop"
android:layout_marginVertical="10dp"
android:src="@drawable/ic_person_white_24"
android:src="@drawable/main"
app:layout_constraintDimensionRatio="1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
Expand Down
48 changes: 48 additions & 0 deletions app/src/main/res/layout/item_user.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/color_sopt">

<ImageView
android:id="@+id/iv_profile"
android:layout_width="50dp"
android:layout_height="0dp"
android:layout_marginStart="20dp"
android:scaleType="centerCrop"
android:layout_marginVertical="10dp"
android:src="@drawable/main"
app:layout_constraintDimensionRatio="1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text="사용자의 이름"
android:textSize="16sp"
android:textStyle="bold"
android:maxLines="1"
android:ellipsize="end"
app:layout_constraintBottom_toBottomOf="@id/iv_profile"
app:layout_constraintStart_toEndOf="@id/iv_profile"
app:layout_constraintTop_toTopOf="@id/iv_profile" />

<TextView
android:id="@+id/tv_self_description"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="20dp"
android:maxLines="1"
android:ellipsize="end"
android:text="사용자의 대화명"
app:layout_constraintBottom_toBottomOf="@id/iv_profile"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/iv_profile"
app:layout_constraintStart_toEndOf="@+id/tv_name"/>

</androidx.constraintlayout.widget.ConstraintLayout>

0 comments on commit 40c4f30

Please sign in to comment.