generated from NOW-SOPT-ANDROID/now-sopt-android-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from NOW-SOPT-ANDROID/feat/week-xml2
[Week2] XML 필수 과제
- Loading branch information
Showing
39 changed files
with
714 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.sopt.now.data | ||
|
||
import androidx.annotation.DrawableRes | ||
|
||
sealed class ItemData { | ||
|
||
data class MyProfile( | ||
@DrawableRes val profileImage: Int?, | ||
val name: String, | ||
val description: String?, | ||
) : ItemData() | ||
|
||
|
||
data class Friend( | ||
@DrawableRes val profileImage: Int?, | ||
val name: String, | ||
val description: String?, | ||
) : ItemData() | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package com.sopt.now.ui.adapter | ||
|
||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import androidx.recyclerview.widget.RecyclerView | ||
import androidx.recyclerview.widget.RecyclerView.ViewHolder | ||
import com.sopt.now.data.ItemData | ||
import com.sopt.now.databinding.ItemFriendBinding | ||
import com.sopt.now.databinding.ItemMyProfileBinding | ||
|
||
class ItemAdapter(private val items: MutableList<ItemData>) : RecyclerView.Adapter<ViewHolder>() { | ||
|
||
companion object { | ||
private const val VIEW_TYPE_MY_PROFILE = 0 | ||
private const val VIEW_TYPE_FRIEND = 1 | ||
} | ||
|
||
interface ItemClick { | ||
fun onItemClick(view: View, position: Int) | ||
} | ||
|
||
private var itemClick: ItemClick? = null | ||
|
||
|
||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { | ||
val inflater = LayoutInflater.from(parent.context) | ||
return when (viewType) { | ||
VIEW_TYPE_MY_PROFILE -> { | ||
val binding = ItemMyProfileBinding.inflate(inflater, parent, false) | ||
MyProfileViewHolder(binding) | ||
} | ||
|
||
else -> { | ||
val binding = ItemFriendBinding.inflate(inflater, parent, false) | ||
FriendViewHolder(binding) | ||
} | ||
} | ||
} | ||
|
||
override fun onBindViewHolder(holder: ViewHolder, position: Int) { | ||
when (val item = items[position]) { | ||
is ItemData.MyProfile -> { | ||
(holder as MyProfileViewHolder).profileImage.setImageResource(item.profileImage!!) | ||
holder.name.text = item.name | ||
holder.description.text = item.description | ||
} | ||
|
||
is ItemData.Friend -> { | ||
(holder as FriendViewHolder).profileImage.setImageResource(item.profileImage!!) | ||
holder.name.text = item.name | ||
holder.description.text = item.description | ||
holder.itemView.setOnClickListener { | ||
itemClick?.onItemClick(it, position) | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
override fun getItemCount(): Int { | ||
return items.size | ||
} | ||
|
||
override fun getItemViewType(position: Int): Int { | ||
return when (items[position]) { | ||
is ItemData.MyProfile -> VIEW_TYPE_MY_PROFILE | ||
is ItemData.Friend -> VIEW_TYPE_FRIEND | ||
} | ||
} | ||
|
||
private class MyProfileViewHolder(binding: ItemMyProfileBinding) : | ||
ViewHolder(binding.root) { | ||
|
||
val profileImage = binding.ivProfile | ||
val name = binding.tvName | ||
val description = binding.tvDescription | ||
} | ||
|
||
private class FriendViewHolder(binding: ItemFriendBinding) : | ||
ViewHolder(binding.root) { | ||
|
||
val profileImage = binding.ivProfile | ||
val name = binding.tvName | ||
val description = binding.tvDescription | ||
|
||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.sopt.now.ui.home | ||
|
||
import android.os.Bundle | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import androidx.fragment.app.Fragment | ||
import androidx.fragment.app.viewModels | ||
import com.sopt.now.data.ItemData | ||
import com.sopt.now.databinding.FragmentHomeBinding | ||
import com.sopt.now.ui.adapter.ItemAdapter | ||
import com.sopt.now.ui.home.viewModel.HomeViewModel | ||
|
||
class HomeFragment : Fragment() { | ||
private var _binding: FragmentHomeBinding? = null | ||
private val binding | ||
get() = requireNotNull(_binding) { | ||
"바인딩 객체를 생성하지 않음" | ||
} | ||
|
||
private val viewModel by viewModels<HomeViewModel>() | ||
|
||
override fun onCreateView( | ||
inflater: LayoutInflater, | ||
container: ViewGroup?, | ||
savedInstanceState: Bundle?, | ||
): View { | ||
_binding = FragmentHomeBinding.inflate(inflater, container, false) | ||
return binding.root | ||
} | ||
|
||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
super.onViewCreated(view, savedInstanceState) | ||
val items = viewModel.friendList | ||
val friendAdapter = ItemAdapter(items) | ||
binding.rvFriends.adapter = friendAdapter | ||
setFriendList(items) | ||
} | ||
|
||
override fun onDestroy() { | ||
_binding = null | ||
super.onDestroy() | ||
} | ||
|
||
private fun setFriendList(friendList: List<ItemData>) { | ||
friendList.toList() | ||
} | ||
|
||
} |
76 changes: 76 additions & 0 deletions
76
app/src/main/java/com/sopt/now/ui/home/viewModel/HomeViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package com.sopt.now.ui.home.viewModel | ||
|
||
import androidx.lifecycle.ViewModel | ||
import com.sopt.now.R | ||
import com.sopt.now.data.ItemData | ||
|
||
class HomeViewModel : ViewModel() { | ||
val friendList = mutableListOf( | ||
|
||
ItemData.MyProfile( | ||
profileImage = R.drawable.img_arin, | ||
name = "김아린", | ||
description = "업보 청산 중..", | ||
), | ||
ItemData.Friend( | ||
profileImage = R.drawable.img_profile0, | ||
name = "이의경", | ||
description = "김아린 과제 벼락치기 하지 마라", | ||
), | ||
ItemData.Friend( | ||
profileImage = R.drawable.img_profile1, | ||
name = "최준서", | ||
description = "오운완 ㅋㅋ", | ||
), | ||
ItemData.Friend( | ||
profileImage = R.drawable.img_profile2, | ||
name = "이연진", | ||
description = "아리니 넘 기엽당..", | ||
), | ||
ItemData.Friend( | ||
profileImage = R.drawable.img_profile3, | ||
name = "손민재", | ||
description = "점심 뭐 먹지?", | ||
), | ||
ItemData.Friend( | ||
profileImage = R.drawable.img_profile4, | ||
name = "홍해인", | ||
description = "난 눈물의 여왕이야", | ||
), | ||
ItemData.Friend( | ||
profileImage = R.drawable.img_profile5, | ||
name = "백현우", | ||
description = "눈물의여왕시작하지말걸공부가안된다", | ||
), | ||
ItemData.Friend( | ||
profileImage = R.drawable.img_profile6, | ||
name = "이서경", | ||
description = "저는 환연 과몰입러예요", | ||
), | ||
ItemData.Friend( | ||
profileImage = R.drawable.img_profile7, | ||
name = "이주원", | ||
description = "너가 자기야 미안해 했잖아?", | ||
), | ||
ItemData.Friend( | ||
profileImage = R.drawable.img_profile8, | ||
name = "김광태", | ||
description = "내일 뭐 해?", | ||
), | ||
ItemData.Friend( | ||
profileImage = R.drawable.img_profile9, | ||
name = "정현규", | ||
description = "내봬누", | ||
), | ||
ItemData.Friend( | ||
profileImage = R.drawable.img_profile10, | ||
name = "성해은", | ||
description = "벌써 스물 아홉이야", | ||
), | ||
ItemData.Friend( | ||
profileImage = R.drawable.img_profile11, | ||
name = "정규민", | ||
description = "오마카세 사줄게", | ||
), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.