Skip to content

Commit

Permalink
[nunu/#28] feat: 6주차 구현2
Browse files Browse the repository at this point in the history
- 리사이클러뷰를 사용해서 화면 만들기
- 리사이클러뷰 클릭 이벤트
  • Loading branch information
Ssamssamukja committed May 21, 2024
1 parent a92fc43 commit 9e62555
Show file tree
Hide file tree
Showing 19 changed files with 418 additions and 53 deletions.
1 change: 1 addition & 0 deletions UMC_6th/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ dependencies {
implementation("androidx.compose.material3:material3")
implementation("androidx.viewpager2:viewpager2:1.0.0")
implementation ("me.relex:circleindicator:2.1.6")
implementation ("com.google.code.gson:gson:2.9.0")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
Expand Down
8 changes: 8 additions & 0 deletions UMC_6th/app/src/main/java/com/example/umc_6th/Album.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.example.umc_6th

data class Album(
var title : String? = "",
var artist : String? = "",
var coverImage : Int? = null,
var songs: ArrayList<Song>? = null
)
28 changes: 23 additions & 5 deletions UMC_6th/app/src/main/java/com/example/umc_6th/LockerFragment.kt
Original file line number Diff line number Diff line change
@@ -1,21 +1,39 @@
package com.example.umc_6th

import android.os.Bundle
import android.os.Handler
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.example.umc_6th.adapter.LockerPagerAdapter
import com.example.umc_6th.databinding.FragmentHomeBinding
import com.example.umc_6th.databinding.FragmentLockerBinding
import com.google.android.material.tabs.TabLayoutMediator

class LockerFragment : Fragment() {
// 여기에 Fragment의 구현 내용을 작성합니다.
private var _binding: FragmentLockerBinding? = null
private val binding get() = _binding!!
private val information = arrayListOf("저장한곡", "음악파일")

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// 여기에서 Fragment의 레이아웃을 인플레이트합니다.
return inflater.inflate(R.layout.fragment_locker, container, false)
): View {
_binding = FragmentLockerBinding.inflate(inflater, container, false)

val lockerAdapter = LockerPagerAdapter(this)
binding.vpLocker.adapter = lockerAdapter
TabLayoutMediator(binding.tbLocker, binding.vpLocker) { tab, position ->
tab.text = information[position]
}.attach()

return binding.root
}

// 필요한 경우 다른 Fragment 생명주기 메소드를 오버라이드합니다.
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.example.umc_6th

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment

class LockerMusicFileFragment : Fragment() {
// 여기에 Fragment의 구현 내용을 작성합니다.
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// 여기에서 Fragment의 레이아웃을 인플레이트합니다.
return inflater.inflate(R.layout.fragment_locker_music_file, container, false)
}

// 필요한 경우 다른 Fragment 생명주기 메소드를 오버라이드합니다.
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.example.umc_6th

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.umc_6th.adapter.LockerAlbumRecyclerAdapter
import com.example.umc_6th.databinding.FragmentLockerSavedSongBinding
import com.google.gson.Gson

class LockerSavedSongFragment : Fragment() {
private var albumDatas = ArrayList<Album>()
lateinit var binding : FragmentLockerSavedSongBinding

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

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = FragmentLockerSavedSongBinding.inflate(inflater, container, false)

albumDatas.apply {
add(Album("Love wins all", "아이유 (IU)", R.drawable.img_album_lovewinsall))
add(Album("해야 (HEYA)", "IVE", R.drawable.img_album_heya))
add(Album("Supernova", "에스파 (aespa)", R.drawable.img_album_supernova))
add(Album("Lilac", "아이유 (IU)", R.drawable.img_album_exp2))
add(Album("Drama", "에스파 (aespa)", R.drawable.img_album_drama))
add(Album("Weekend", "태연 (Tae Yeon)", R.drawable.img_album_exp6))
}
val lockerAlbumRecyclerAdapter = LockerAlbumRecyclerAdapter(albumDatas)
binding.rvLockerSavedSong.adapter = lockerAlbumRecyclerAdapter
binding.rvLockerSavedSong.layoutManager = LinearLayoutManager(requireActivity())

lockerAlbumRecyclerAdapter.setItemClickListener(object : LockerAlbumRecyclerAdapter.OnItemClickListener {
override fun onRemoveAlbum(position: Int) {
lockerAlbumRecyclerAdapter.removeItem(position)
}

override fun onItemClick(album: Album) {
changeAlbumFragment(album)
}
})

return binding.root
}

private fun changeAlbumFragment(album: Album) {
(context as MainActivity).supportFragmentManager.beginTransaction()
.replace(R.id.main_container, AlbumFragment().apply {
arguments = Bundle().apply {
val gson = Gson()
val albumToJson = gson.toJson(album)
putString("album", albumToJson)
}
})
.commitAllowingStateLoss()
}

}
7 changes: 7 additions & 0 deletions UMC_6th/app/src/main/java/com/example/umc_6th/Song.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.umc_6th

data class Song(
var title : String? = "",
var artist : String? = "",
var coverImage : Int? = null,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.example.umc_6th.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.umc_6th.Album
import com.example.umc_6th.databinding.ItemLockerAlbumBinding

class LockerAlbumRecyclerAdapter(private val albumList: ArrayList<Album>) : RecyclerView.Adapter<LockerAlbumRecyclerAdapter.ViewHolder>() {
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): LockerAlbumRecyclerAdapter.ViewHolder {
val binding: ItemLockerAlbumBinding = ItemLockerAlbumBinding
.inflate(LayoutInflater.from(parent.context), parent, false)

return ViewHolder(binding)
}

override fun onBindViewHolder(holder: LockerAlbumRecyclerAdapter.ViewHolder, position: Int) {
holder.bind(albumList[position])
holder.itemView.setOnClickListener {
itemClickListener.onItemClick(albumList[position])
}

holder.binding.imgItemLockerAlbumMore.setOnClickListener {
itemClickListener.onRemoveAlbum(position)
}
}

override fun getItemCount(): Int = albumList.size

inner class ViewHolder(val binding: ItemLockerAlbumBinding): RecyclerView.ViewHolder(binding.root){

fun bind(album: Album){
binding.txItemLockerAlbumTitle.text = album.title
binding.txItemLockerAlbumArtist.text = album.artist
binding.imgItemLockerAlbumCover.setImageResource(album.coverImage!!)
}
}

interface OnItemClickListener {
fun onItemClick(album : Album)
abstract fun onRemoveAlbum(position: Int)
}

private lateinit var itemClickListener : OnItemClickListener

fun setItemClickListener(onItemClickListener: OnItemClickListener) {
this.itemClickListener = onItemClickListener
}

fun removeItem(position: Int){
albumList.removeAt(position)
notifyDataSetChanged()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.example.umc_6th.adapter

import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.example.umc_6th.LockerMusicFileFragment
import com.example.umc_6th.LockerSavedSongFragment

class LockerPagerAdapter (fragment : Fragment) : FragmentStateAdapter(fragment) {
override fun getItemCount(): Int = 2

override fun createFragment(position: Int): Fragment {
return when(position){
0 -> LockerSavedSongFragment()
else -> LockerMusicFileFragment()
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
84 changes: 42 additions & 42 deletions UMC_6th/app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,48 +28,6 @@
app:layout_constraintStart_toStartOf="parent">


<SeekBar
android:id="@+id/mainSeekBar"
android:layout_width="match_parent"
android:layout_height="10dp"
android:background="@null"
android:max="100000"
android:paddingStart="0dp"
android:paddingEnd="0dp"
android:progress="0"
android:progressBackgroundTint="@color/song_player_bg"
android:progressTint="@color/song_player"
android:thumb="@drawable/transparent_thumb"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<LinearLayout
android:id="@+id/layoutPlayLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

<TextView
android:id="@+id/txPlayTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="제목"
android:textColor="@color/black"
android:textSize="13sp" />

<TextView
android:id="@+id/txPlayArtist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="아티스트"
android:textSize="11sp" />
</LinearLayout>

<LinearLayout
android:id="@+id/layoutPlayRight"
android:layout_width="0dp"
Expand Down Expand Up @@ -110,6 +68,48 @@
android:src="@drawable/btn_miniplayer_go_list" />
</LinearLayout>

<LinearLayout
android:id="@+id/layoutPlayLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

<TextView
android:id="@+id/txPlayTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="제목"
android:textColor="@color/black"
android:textSize="13sp" />

<TextView
android:id="@+id/txPlayArtist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="아티스트"
android:textSize="11sp" />
</LinearLayout>

<SeekBar
android:id="@+id/mainSeekBar"
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="@null"
android:max="100000"
android:paddingStart="0dp"
android:paddingEnd="0dp"
android:progress="0"
android:progressBackgroundTint="@color/song_player_bg"
android:progressTint="@color/song_player"
android:thumb="@drawable/transparent_thumb"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>


Expand Down
Loading

0 comments on commit 9e62555

Please sign in to comment.