일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- Kotlin FCM
- 안드로이드 카카오 로그인
- 안드로이드 갤러리 접근
- 카카오 알고리즘
- Kotlin
- WebView
- Android ViewPager2
- Android DataBinding
- Android ProgressBar
- android recyclerview
- 프로그래머스 알고리즘
- Java
- MVP Architecture
- DataBinding
- OkHttp Interceptor
- 알고리즘 자바
- 습관만들기
- 66챌린지
- Android Interceptor
- Android
- Android Jetpack
- Android Navigation
- 영어독립365
- Android WebView
- scope function
- Android 12 대응
- 영어공부
- 안드로이드
- Android 12
- 안드로이드 fcm
Archives
- Today
- Total
Developer Geek
[Android] ViewPager2 Basic Sample in Kotlin 본문
반응형
ViewPager2 Basic Sample
개요
ViewPager2란
- 화면 슬라이드는 하나의 전체 화면에서 다른 전체 화면으로 전환하는 것으로, 설정 마법사 또는 슬라이드쇼와 같은 UI에서 일반적으로 사용된다.
- ViewPager2 객체에는 페이지 간 전환을 위한 스와이프 동작이 내장되어 있으며 기본적으로 화면 슬라이드 애니메이션을 표시하므로 직접 애니메이션을 만들 필요가 없다.
- ViewPager2는 표시할 새 페이지의 요소로
FragmentStateAdapter
객체를 사용한다.실행 영상
프로젝트 구조
Code
ViewPagerAdapter.kt
class ViewPagerAdapter(fa: FragmentActivity) : FragmentStateAdapter(fa) {
private val fragments = ArrayList<Fragment>()
fun add(fragment: Fragment) {
fragments.add(fragment)
}
override fun getItemCount(): Int = fragments.size
override fun createFragment(position: Int): Fragment = fragments[position]
}
Create 3 Fragments For 3 Pages
FirstFragment.kt
class FirstFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_first, container, false)
}
}
fragment_first.xml
<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"
android:background="#4CAF50"
tools:context=".FirstFragment">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Page 1"
android:textColor="@color/white"
android:textSize="60sp"
android:textStyle="bold"
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() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_second, container, false)
}
}
fragment_second.xml
<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"
android:background="#00BCD4"
tools:context=".SecondFragment">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Page 2"
android:textColor="@color/white"
android:textSize="60sp"
android:textStyle="bold"
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() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_third, container, false)
}
}
fragment_third.xml
<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"
android:background="#FF9800"
tools:context=".ThirdFragment">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Page 3"
android:textColor="@color/white"
android:textSize="60sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
activity_main.xml
<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">
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.kt
class MainActivity : AppCompatActivity() {
private var binding: ActivityMainBinding? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val activityMainBinding = ActivityMainBinding.inflate(layoutInflater)
setContentView(activityMainBinding.root)
binding = activityMainBinding
// Set up the adapter
val viewPagerAdapter = ViewPagerAdapter(this)
// Add Fragments
viewPagerAdapter.add(FirstFragment())
viewPagerAdapter.add(SecondFragment())
viewPagerAdapter.add(ThirdFragment())
// Set the adapter
binding?.viewpager?.adapter = viewPagerAdapter
}
}
반응형
'안드로이드 > View' 카테고리의 다른 글
[Android] RecyclerView 아이템 여백 설정 (1) | 2022.09.24 |
---|---|
[Android] RecyclerView Multi-ViewHolder Basic Sample: 채팅 화면 (0) | 2022.08.30 |
[Android] ViewPager Basic Sample in Kotlin (0) | 2022.08.08 |
[Android] ViewPager2 and TabLayout Sample in Kotlin (0) | 2022.07.28 |
[Android] 달력 예제 in Kotlin (0) | 2022.07.08 |
Comments