일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
Tags
- Android Jetpack
- Android ViewPager2
- 카카오 알고리즘
- 안드로이드 갤러리 접근
- Android Interceptor
- Android 12
- WebView
- OkHttp Interceptor
- MVP Architecture
- 안드로이드
- android recyclerview
- Android DataBinding
- 영어독립365
- 영어공부
- Android WebView
- Android Navigation
- Android ProgressBar
- scope function
- Kotlin
- DataBinding
- 습관만들기
- Android 12 대응
- 66챌린지
- 안드로이드 카카오 로그인
- Android
- 안드로이드 fcm
- Java
- Kotlin FCM
- 알고리즘 자바
- 프로그래머스 알고리즘
Archives
- Today
- Total
Developer Geek
[Android] ViewPager2 and TabLayout Sample in Kotlin 본문
반응형
ViewPager2 and TabLayout Sample
개요
앱 설명
ViewPager2와 TabLayout을 사용해 탭 클릭을 통해서 뷰가 스와이프되고, 스와이프를 뷰가 변하고 활성화되는 탭도 바뀌는 앱을 만들어보려고 한다.
실행영상
Code
build.gradle(Module): ViewBinding 추가
ViewBinding 사용을 위해 build.gradle(:Module)
파일에서 android { }
태그의 속성 값으로 아래와 같이 ViewBinding
을 허용해준다.
android {
...
buildFeatures{
viewBinding true
}
...
}
activity_main.xml
TabLayout
: 탭 생성을 위한 ViewViewPager2
: 탭과 연결 될 View
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/viewPager"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tabLayout" />
</androidx.constraintlayout.widget.ConstraintLayout>
fragment_first.xml: ViewPager2
에 연결될 FragmentFirst Layout
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragments.FirstFragment">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FIRST FRAGMENT"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
FirstFragment.kt
class FirstFragment : Fragment() {
private lateinit var binding: FragmentFirstBinding
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = FragmentFirstBinding.inflate(inflater, container, false)
return binding.root
}
}
fragment_second.xml: ViewPager2
에 연결될 SecondFragment Layout
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragments.SecondFragment">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SECOND FRAGMENT"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
SecondFragment.kt
class SecondFragment : Fragment() {
private lateinit var binding: FragmentSecondBinding
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = FragmentSecondBinding.inflate(inflater, container, false)
return binding.root
}
}
fragment_third.xml: ViewPager2
에 연결될 ThirdFragment Layout
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragments.ThirdFragment">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="THIRD FRAGMENT"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
ThirdFragment.kt
class ThirdFragment : Fragment() {
private lateinit var binding: FragmentThirdBinding
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = FragmentThirdBinding.inflate(inflater, container, false)
return binding.root
}
}
ViewPagerAdapter.kt
private const val NUM_TABS = 3
class ViewPagerAdapter(fragmentManager: FragmentManager, lifecycle: Lifecycle) :
FragmentStateAdapter(fragmentManager, lifecycle) {
override fun getItemCount(): Int = NUM_TABS
override fun createFragment(position: Int): Fragment {
return when (position) {
0 -> FirstFragment()
1 -> SecondFragment()
else -> ThirdFragment()
}
}
}
MainActivity.kt
class MainActivity : AppCompatActivity() {
private val tabTitleArray = arrayOf("First Tab", "Second Tab", "Third Tab")
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.apply {
viewPager.adapter = ViewPagerAdapter(supportFragmentManager, lifecycle)
/** [TabLayoutMediator]: [TabLayout]과 [ViewPager2]를 연동을 도와주는 객체이다. */
TabLayoutMediator(tabLayout, viewPager) { tab, position ->
tab.text = tabTitleArray[position]
}.attach()
}
}
}
질문과 잘못된 점에 대해 말씀해주시는 건 언제나 환영입니다.
zero5.two4@gmail.com
반응형
'안드로이드 > View' 카테고리의 다른 글
[Android] ViewPager2 Basic Sample in Kotlin (0) | 2022.08.10 |
---|---|
[Android] ViewPager Basic Sample in Kotlin (0) | 2022.08.08 |
[Android] 달력 예제 in Kotlin (0) | 2022.07.08 |
[Android] WebView란 (0) | 2022.06.27 |
안드로이드 막대 프로그레스바 구현 예제 in Kotlin (0) | 2022.06.25 |
Comments