스터디/안드로이드
[Android/Kotlin] viewpager2를 통한 가로 슬라이드 구현
혜유우
2023. 1. 20. 00:11
ViewPager2
스와이프할 수 있는 형식으로 view 또는 fragment를 표시할 수 있다
1. build.gradle에 라이브러리 추가
implementation 'me.relex:circleindicator:2.1.6'
2. 스와이프 할 때마다 보여질 fragment
package com.mobile.mydrawer
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
class FirstFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_first, container, false)
}
}
package com.mobile.mydrawer
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
class SecondFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_second, container, false)
}
}
package com.mobile.mydrawer
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
class ThirdFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_third, container, false)
}
}
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#BAF3FA"
tools:context=".FirstFragment" />
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#EEF3BC"
tools:context=".FirstFragment" />
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#CFBFEC"
tools:context=".FirstFragment" />
3. MainActivity
FrameLayout 위에 viewpager2, indicator 추가
★FragmentStatementAdapter 상속
★createFragment의 파라미터가 position
package com.mobile.mydrawer
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.viewpager.widget.PagerAdapter
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
pager.adapter = PagerAdapter(this)
pager.orientation = ViewPager2.ORIENTATION_HORIZONTAL
pager.offscreenPageLimit = 3 //초기화해서 메모리를 미리 만들어놓음
pager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback(){
override fun onPageSelected(position: Int) {
super.onPageSelected(position)
showToast("페이지 선택 : ${position}")
indicator.animatePageSelected(position)
}
})
//frameLayout은 위에 중첩해서 올릴 수 있다
indicator.setViewPager(pager)
indicator.createIndicators(3, 0)
button.setOnClickListener{
pager.currentItem = 0
}
button2.setOnClickListener{
pager.currentItem = 1
}
button3.setOnClickListener{
pager.currentItem = 2
}
}
fun showToast(message:String){
Toast.makeText(applicationContext, message, Toast.LENGTH_LONG).show()
}
inner class PagerAdapter : FragmentStateAdapter{
constructor(activity: FragmentActivity):super(activity) //생성자
override fun getItemCount() = 3
override fun createFragment(position: Int): Fragment {
when(position) {
0 -> {
return FirstFragment()
}
1 -> {
return SecondFragment()
}
2 -> {
return ThirdFragment()
}
else -> {
return FirstFragment()
}
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<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">
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintStart_toStartOf="parent">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="첫번째" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="두번째" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="세번째" />
</LinearLayout>
<FrameLayout
android:layout_width="411dp"
android:layout_height="646dp"
android:layout_marginEnd="6dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/linearLayout">
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
</androidx.viewpager2.widget.ViewPager2>
<me.relex.circleindicator.CircleIndicator3
android:id="@+id/indicator"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_gravity="bottom"/>
</FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>