Skip to content

Commit

Permalink
feat/#5: 리싸이클러뷰 연결
Browse files Browse the repository at this point in the history
  • Loading branch information
hyeeum committed Apr 10, 2024
1 parent 39cc771 commit 03a21c4
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 9 deletions.
9 changes: 9 additions & 0 deletions app/src/main/java/com/sopt/now/Friend.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.sopt.now

import androidx.annotation.DrawableRes

data class Friend(
@DrawableRes val profileImage: Int,
val name: String,
val selfDescription: String,
)
28 changes: 28 additions & 0 deletions app/src/main/java/com/sopt/now/FriendAdapter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.sopt.now

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.sopt.now.databinding.ItemFriendBinding

class FriendAdapter() : RecyclerView.Adapter<FriendViewHolder>() {
// 임시의 빈 리스트
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 onBindViewHolder(holder: FriendViewHolder, position: Int) {
holder.onBind(friendList[position])
}

override fun getItemCount() = friendList.size

fun setFriendList(friendList: List<Friend>) {
this.friendList = friendList.toList()
notifyDataSetChanged()
}
}
14 changes: 14 additions & 0 deletions app/src/main/java/com/sopt/now/FriendViewHolder.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.sopt.now

import androidx.recyclerview.widget.RecyclerView
import com.sopt.now.databinding.ItemFriendBinding

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
}
}
}
38 changes: 32 additions & 6 deletions app/src/main/java/com/sopt/now/MyPageFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,44 @@ import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import com.sopt.now.databinding.FragmentMyPageBinding

class MyPageFragment : Fragment() {
private lateinit var binding:FragmentMyPageBinding

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}

private val mockFriendList = listOf<Friend>(
Friend(
profileImage = R.drawable.ic_home_white_24,
name = "이의경",
selfDescription = "다들 빨리 끝내고 뒤풀이 가고 싶지? ㅎㅎ 아직 반도 안왔어 ^&^",
),
Friend(
profileImage = R.drawable.ic_home_white_24,
name = "우상욱",
selfDescription = "나보다 안드 잘하는 사람 있으면 나와봐",
),
Friend(
profileImage = R.drawable.ic_home_white_24,
name = "배지현",
selfDescription = "표정 풀자 ^^",
),
)
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_my_page, container, false)
binding=FragmentMyPageBinding.inflate(inflater)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val friendAdapter = FriendAdapter()
binding.rvFriends.run {
adapter = friendAdapter
layoutManager = LinearLayoutManager(requireContext())
}
friendAdapter.setFriendList(mockFriendList)
}

}
10 changes: 7 additions & 3 deletions app/src/main/res/layout/fragment_my_page.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".MyPageFragment">

<!-- TODO: Update blank fragment layout -->
<TextView
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_friends"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="mypage" />
android:orientation="vertical"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:listitem="@layout/item_friend"
android:paddingVertical="5dp"/>

</FrameLayout>
43 changes: 43 additions & 0 deletions app/src/main/res/layout/item_friend.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?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">

<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/ic_person_white_24"
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"
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_marginEnd="20dp"
android:maxLines="1"
android:text="친구의 대화명"
app:layout_constraintBottom_toBottomOf="@id/iv_profile"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/iv_profile" />

</androidx.constraintlayout.widget.ConstraintLayout>

0 comments on commit 03a21c4

Please sign in to comment.