Skip to content

Commit

Permalink
[nunu/#13] feat: 3주차-3 완료
Browse files Browse the repository at this point in the history
- [HomeFragment]화면Panel 부분 Viewpager로 구현해보고 Indicator 추가해보기
- 오픈소스 라이브러리인 CircleIndicator 활용해서 슬라이드 만들어보기
- 일정 시간 후 다음 화면으로 넘어가는 자동 슬라이드 구현해보기
  • Loading branch information
Ssamssamukja committed Apr 22, 2024
1 parent 6c959f1 commit 402d8d7
Show file tree
Hide file tree
Showing 15 changed files with 416 additions and 144 deletions.
2 changes: 1 addition & 1 deletion .idea/deploymentTargetDropDown.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions .idea/libraries/Gradle__me_relex_circleindicator_2_1_6_aar.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/modules/app/UMC_6th.app.androidTest.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/modules/app/UMC_6th.app.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/modules/app/UMC_6th.app.main.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/modules/app/UMC_6th.app.unitTest.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions UMC_6th/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ dependencies {
implementation("androidx.compose.ui:ui-tooling-preview")
implementation("androidx.compose.material3:material3")
implementation("androidx.viewpager2:viewpager2:1.0.0")
implementation ("me.relex:circleindicator:2.1.6")
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
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 FragmentHomeMainBanner : Fragment() {
// 여기에 Fragment의 구현 내용을 작성합니다.
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// 여기에서 Fragment의 레이아웃을 인플레이트합니다.
return inflater.inflate(R.layout.fragment_home_main_banner, container, false)
}

// 필요한 경우 다른 Fragment 생명주기 메소드를 오버라이드합니다.
}
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 FragmentHomeMainBanner2 : Fragment() {
// 여기에 Fragment의 구현 내용을 작성합니다.
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// 여기에서 Fragment의 레이아웃을 인플레이트합니다.
return inflater.inflate(R.layout.fragment_home_main_banner2, container, false)
}

// 필요한 경우 다른 Fragment 생명주기 메소드를 오버라이드합니다.
}
49 changes: 43 additions & 6 deletions UMC_6th/app/src/main/java/com/example/umc_6th/HomeFragment.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
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.databinding.FragmentHomeBinding
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.example.umc_6th.adapter.HomePagerAdapter
import com.example.umc_6th.adapter.ViewPagerAdapter
import com.example.umc_6th.databinding.FragmentHomeBinding
import me.relex.circleindicator.CircleIndicator3
import android.os.Looper


class HomeFragment : Fragment(R.layout.fragment_home) {
class HomeFragment : Fragment() {
// 여기에 Fragment의 구현 내용을 작성합니다.
private var _binding: FragmentHomeBinding? = null
private val binding get() = _binding!!
private lateinit var handler: Handler
private lateinit var runnable: Runnable
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
Expand All @@ -34,14 +43,42 @@ class HomeFragment : Fragment(R.layout.fragment_home) {
}
val adapter = ViewPagerAdapter(this)
binding.homeViewPager.adapter = adapter

val mainAdapter = HomePagerAdapter.HomeMainViewPagerAdapter(this)
val viewpager: ViewPager2 = binding.homeMainViewPager
viewpager.adapter = mainAdapter

val indicator: CircleIndicator3 = binding.indicator
indicator.setViewPager(viewpager)

// Handler 및 Runnable 설정
handler = Handler(Looper.getMainLooper())
runnable = object : Runnable {
override fun run() {
val itemCount = adapter.itemCount
val nextItem = (binding.homeMainViewPager.currentItem + 1) % itemCount
binding.homeMainViewPager.currentItem = nextItem
handler.postDelayed(this, 3000) // 3초 후에 다음 페이지로 넘어감
}
}
startAutoSlide()
}

private fun startAutoSlide() {
handler.postDelayed(runnable, 3000) // 3초에 한번씩 페이지 이동
}

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]
private fun stopAutoSlide() {
handler.removeCallbacks(runnable) // 자동 슬라이드 중지
}

override fun onDestroyView() {
super.onDestroyView()
stopAutoSlide() // 뷰가 사라질 때 자동 슬라이드 중지
_binding = null
}



class FragmentHomeBanner : Fragment(R.layout.fragment_home_banner1) {
// 필요한 경우 여기에 로직 추가
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.viewpager2.adapter.FragmentStateAdapter
import com.example.umc_6th.AlbumDetailFragment
import com.example.umc_6th.AlbumSongsFragment
import com.example.umc_6th.AlbumVideoFragment
import com.example.umc_6th.HomeFragment


class AlbumPagerAdapter(fragment: Fragment) : FragmentStateAdapter(fragment) {
Expand All @@ -17,4 +18,11 @@ class AlbumPagerAdapter(fragment: Fragment) : FragmentStateAdapter(fragment) {
override fun getItemCount(): Int = fragments.size

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

}

class ViewPagerAdapter(fragment: Fragment) : FragmentStateAdapter(fragment) {
private val fragments = arrayOf(HomeFragment.FragmentHomeBanner())
override fun getItemCount(): Int = fragments.size
override fun createFragment(position: Int): Fragment = fragments[position]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.example.umc_6th.adapter

import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.example.umc_6th.FragmentHomeMainBanner
import com.example.umc_6th.FragmentHomeMainBanner2

class HomePagerAdapter {
class HomeMainViewPagerAdapter(fragment: Fragment) : FragmentStateAdapter(fragment) {
private val fragments = listOf(
FragmentHomeMainBanner(),
FragmentHomeMainBanner2()
)

override fun getItemCount(): Int = fragments.size

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

0 comments on commit 402d8d7

Please sign in to comment.