Skip to content

Commit

Permalink
[nunu/#13] 3주차-2 구현
Browse files Browse the repository at this point in the history
- HomeFragment에서 banner 부분 Viewpager 구현하기
- AlbumFragment 에 TabLayout + Viewpager 추가 구현하기
- AlbumFragment Viewpager 에 AlbumSongsFragment, AlbumDetailFragment, AlbumVideoFragment 구현하기
- [LockerFragment] 화면 TabLayout과 Viewpager로 구현하기

+디자인 수정 필요
  • Loading branch information
Ssamssamukja committed Apr 22, 2024
1 parent 297ae20 commit a2518aa
Show file tree
Hide file tree
Showing 11 changed files with 819 additions and 621 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 @@ -64,6 +64,7 @@ dependencies {
implementation("androidx.compose.ui:ui-graphics")
implementation("androidx.compose.ui:ui-tooling-preview")
implementation("androidx.compose.material3:material3")
implementation("androidx.viewpager2:viewpager2:1.0.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
37 changes: 15 additions & 22 deletions UMC_6th/app/src/main/java/com/example/umc_6th/AlbumFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ import android.widget.ImageView
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.example.umc_6th.adapter.AlbumPagerAdapter
import com.example.umc_6th.databinding.FragmentAlbumBinding
import com.google.android.material.tabs.TabLayoutMediator

class AlbumFragment: Fragment() {
class AlbumFragment: Fragment(R.layout.fragment_album) {
// 여기에 Fragment의 구현 내용을 작성합니다.

private var _binding: FragmentAlbumBinding? = null
Expand All @@ -34,27 +37,17 @@ class AlbumFragment: Fragment() {
requireActivity().supportFragmentManager.popBackStack()
}

// 취향 mix 이미지 변경
binding.imgSongMixOffTag.setOnClickListener(){
binding.imgSongMixOffTag.visibility=View.GONE
binding.imgSongMixOnTag.visibility=View.VISIBLE
}
binding.imgSongMixOnTag.setOnClickListener(){
binding.imgSongMixOnTag.visibility=View.GONE
binding.imgSongMixOffTag.visibility=View.VISIBLE
}

binding.imgSongPlay01.setOnClickListener(){
goToSongActivity(binding.txSongTitle01.text.toString(), binding.txSongArtist01.text.toString())
}
}

fun goToSongActivity(songTitle: String, songArtist: String) {
val intent = Intent(activity, SongActivity::class.java).apply {
putExtra("songTitle", songTitle)
putExtra("songArtist", songArtist)
}
startActivity(intent)
val adapter = AlbumPagerAdapter(this)
binding.albumViewPager.adapter = adapter

TabLayoutMediator(binding.tlAlbum, binding.albumViewPager) { tab, position ->
tab.text = when (position) {
0 -> "수록곡"
1 -> "상세정보"
2 -> "영상"
else -> null
}
}.attach()
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.example.umc_6th

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.example.umc_6th.databinding.FragmentAlbumBinding
import com.example.umc_6th.databinding.FragmentAlbumSongsBinding

class AlbumSongsFragment : Fragment(R.layout.fragment_album_songs) {
private var _binding: FragmentAlbumSongsBinding? = null
private val binding get() = _binding!!

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
_binding = FragmentAlbumSongsBinding.inflate(inflater, container, false)
return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// 취향 mix 이미지 변경
binding.imgSongMixOffTag.setOnClickListener(){
binding.imgSongMixOffTag.visibility=View.GONE
binding.imgSongMixOnTag.visibility=View.VISIBLE
}
binding.imgSongMixOnTag.setOnClickListener(){
binding.imgSongMixOnTag.visibility=View.GONE
binding.imgSongMixOffTag.visibility=View.VISIBLE
}

binding.imgSongPlay01.setOnClickListener(){
goToSongActivity(binding.txSongTitle01.text.toString(), binding.txSongArtist01.text.toString())
}
}

private fun goToSongActivity(songTitle: String, songArtist: String) {
val intent = Intent(activity, SongActivity::class.java).apply {
putExtra("songTitle", songTitle)
putExtra("songArtist", songArtist)
}
startActivity(intent)
}

override fun onDestroyView() {
super.onDestroyView()
// 메모리 누수를 방지하기 위해 binding 객체를 null로 설정
_binding = null
}
}
32 changes: 24 additions & 8 deletions UMC_6th/app/src/main/java/com/example/umc_6th/HomeFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,46 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.fragment.app.Fragment
import com.example.umc_6th.databinding.FragmentHomeBinding
import androidx.viewpager2.adapter.FragmentStateAdapter


class HomeFragment : Fragment() {
class HomeFragment : Fragment(R.layout.fragment_home) {
// 여기에 Fragment의 구현 내용을 작성합니다.
private var _binding: FragmentHomeBinding? = null
private val binding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_home, container, false)
_binding = FragmentHomeBinding.inflate(inflater, container, false)

val imgSecondAlbum1 = view.findViewById<ImageView>(R.id.imgSecondAlbum1)
imgSecondAlbum1.setOnClickListener {
return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

binding.imgSecondAlbum1.setOnClickListener {
val transaction = requireActivity().supportFragmentManager.beginTransaction()
transaction.replace(R.id.main_container, AlbumFragment())
transaction.addToBackStack(null) // 백 스택에 추가
transaction.commit()
}
val adapter = ViewPagerAdapter(this)
binding.homeViewPager.adapter = adapter
}

return view
}
class ViewPagerAdapter(fragment: Fragment) : FragmentStateAdapter(fragment) {
private val fragments = arrayOf(FragmentHomeBanner())
override fun getItemCount(): Int = fragments.size
override fun createFragment(position: Int): Fragment = fragments[position]
}


class FragmentHomeBanner : Fragment(R.layout.fragment_home_banner1) {
// 필요한 경우 여기에 로직 추가
}
// 필요한 경우 다른 Fragment 생명주기 메소드를 오버라이드합니다.
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.example.umc_6th.adapter

import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.example.umc_6th.AlbumDetailFragment
import com.example.umc_6th.AlbumSongsFragment
import com.example.umc_6th.AlbumVideoFragment


class AlbumPagerAdapter(fragment: Fragment) : FragmentStateAdapter(fragment) {
private val fragments = listOf(
AlbumSongsFragment(),
AlbumDetailFragment(),
AlbumVideoFragment()
)

override fun getItemCount(): Int = fragments.size

override fun createFragment(position: Int): Fragment = fragments[position]
}
Loading

0 comments on commit a2518aa

Please sign in to comment.