Skip to content

Commit

Permalink
[FEAT/#9] ListAdapter로MusicAdapter 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
b1urrrr committed Dec 15, 2022
1 parent 19cc37d commit 5f8c92f
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,66 @@ package org.sopt.sample.presentation.main.music
import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import org.sopt.sample.data.dto.response.ResponseMusicDto
import org.sopt.sample.databinding.HeaderMusicBinding
import org.sopt.sample.databinding.ItemMusicBinding

class MusicAdapter(context: Context) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
class MusicAdapter(context: Context) :
ListAdapter<ResponseMusicDto, RecyclerView.ViewHolder>(diffUtil) {
private val inflater by lazy { LayoutInflater.from(context) }
private var musicList: List<ResponseMusicDto> = emptyList()

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return MusicViewHolder(ItemMusicBinding.inflate(inflater, parent, false))
return when (viewType) {
VIEW_TYPE_HEADER -> MusicHeaderViewHolder(
HeaderMusicBinding.inflate(inflater, parent, false)
)
VIEW_TYPE_ITEM -> MusicViewHolder(
ItemMusicBinding.inflate(inflater, parent, false)
)
else -> throw ClassCastException("Unknown View Type : $viewType")
}
}

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
if (holder is MusicViewHolder) holder.setMusic(musicList[position])
if (holder is MusicViewHolder) holder.setMusic(getItem(position - 1))
}

override fun getItemCount(): Int {
return musicList.size
return super.getItemCount() + 1
}

fun setMusicList(musicList: List<ResponseMusicDto>) {
this.musicList = musicList
notifyDataSetChanged()
override fun getItemViewType(position: Int): Int {
return if (position == 0) VIEW_TYPE_HEADER
else VIEW_TYPE_ITEM
}

class MusicViewHolder(private val binding: ItemMusicBinding) : RecyclerView.ViewHolder(binding.root) {
class MusicViewHolder(private val binding: ItemMusicBinding) :
RecyclerView.ViewHolder(binding.root) {
fun setMusic(music: ResponseMusicDto) {
binding.data = music
}
}

class MusicHeaderViewHolder(private val binding: HeaderMusicBinding) :
RecyclerView.ViewHolder(binding.root)

companion object {
val diffUtil = object : DiffUtil.ItemCallback<ResponseMusicDto>() {
override fun areItemsTheSame(
oldItem: ResponseMusicDto,
newItem: ResponseMusicDto
): Boolean = oldItem == newItem

override fun areContentsTheSame(
oldItem: ResponseMusicDto,
newItem: ResponseMusicDto
): Boolean = oldItem.id == newItem.id
}

private const val VIEW_TYPE_HEADER = 0
private const val VIEW_TYPE_ITEM = 1
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import dagger.hilt.android.AndroidEntryPoint
import org.sopt.sample.R
import org.sopt.sample.util.UiState
import org.sopt.sample.databinding.FragmentMusicBinding
import org.sopt.sample.util.UiState
import org.sopt.sample.util.binding.BindingFragment
import org.sopt.sample.util.extension.showSnackbar

Expand All @@ -33,7 +33,7 @@ class MusicFragment : BindingFragment<FragmentMusicBinding>(R.layout.fragment_mu
viewModel.stateMessage.observe(viewLifecycleOwner) {
when (it) {
UiState.Success -> viewModel.musicList.value?.let { it ->
musicAdapter.setMusicList(it)
musicAdapter.submitList(it)
}
is UiState.Failure -> requireContext().showSnackbar(
binding.root,
Expand Down
1 change: 0 additions & 1 deletion app/src/main/res/layout/fragment_home.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
android:id="@+id/rv_follower"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:overScrollMode="never"
android:paddingBottom="30dp"
android:clipToPadding="false"
tools:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
Expand Down
13 changes: 2 additions & 11 deletions app/src/main/res/layout/fragment_music.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,12 @@
android:orientation="vertical"
android:paddingHorizontal="20dp">

<TextView
android:id="@+id/txt_music_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="@string/music_list"
android:textColor="@color/grey_800"
android:textSize="18sp"
android:textStyle="bold" />

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_music"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:paddingBottom="10dp"
android:clipToPadding="false"
tools:listitem="@layout/item_music" />
</LinearLayout>

Expand Down
22 changes: 22 additions & 0 deletions app/src/main/res/layout/header_music.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_marginTop="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp">

<TextView
android:id="@+id/txt_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:text="@string/music_list"
android:textColor="@color/grey_800"
android:textSize="18sp"
android:textStyle="bold"/>

</androidx.constraintlayout.widget.ConstraintLayout>

0 comments on commit 5f8c92f

Please sign in to comment.