Android/Kotlin

ViewPager 예제

lipnus 2019. 3. 14. 17:14
반응형

ViewPager 



MainActivity

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

initViewPager()
}


private fun initViewPager() {

val adapter = MyPagerAdapter(supportFragmentManager)

var firstFragmet: MyFragment = MyFragment.newInstance("찻번째 프레그먼트")
var secondFragmet: MyFragment = MyFragment.newInstance("두번째 프레그먼트")
var thirdFragmet: MyFragment = MyFragment.newInstance("두번째 프레그먼트")

adapter.addFragment(firstFragmet, "첫번째")
adapter.addFragment(secondFragmet, "두번째")
adapter.addFragment(thirdFragmet, "세번째")

viewpager!!.adapter = adapter
tabs!!.setupWithViewPager(viewpager)
}
}



MyPagerAdapter

class MyPagerAdapter(manager: FragmentManager) : FragmentPagerAdapter(manager) {

private val mFragmentList: ArrayList<Fragment> = ArrayList() //프레그먼트
private val mFragmentTitleList: ArrayList<String> = ArrayList() //프레그먼트의 이름

override fun getItem(position: Int): Fragment {
return mFragmentList.get(position)
}

override fun getCount(): Int {
return mFragmentList.size
}

fun addFragment(fragment: Fragment, title: String) {
mFragmentList.add(fragment)
mFragmentTitleList.add(title)
}

override fun getPageTitle(position: Int): CharSequence? {
return mFragmentTitleList.get(position)
}
}



MyFragment

class MyFragment : Fragment() {

companion object {
fun newInstance(message: String): MyFragment {

val f = MyFragment()
val bdl = Bundle(1)
bdl.putString(EXTRA_MESSAGE, message)
f.arguments = bdl
return f
}
}


override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)

}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {

var view: View? = inflater.inflate(R.layout.fragment_home, container, false)
val message = arguments!!.getString(EXTRA_MESSAGE)
var textView: TextView = view!!.findViewById(R.id.text)
textView!!.text = message

return view
}
}

- 스태틱매소드인 newInstance 을 이용해 MyFragment의 인스턴스를 찍어낸다.

- Myfragment의 argument에 Bundle을 넣어줘서 초기화 데이터를 전송할 수 있다.

onCreateView 에서  arguments 로 받아서 사용. 

  argument는 Myfragment의 인자이다. 

  구글문서의 코드 참조: https://developer.android.com/guide/components/fragments?hl=ko



fragment_my.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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=".MyFragment">

<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test" app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent" android:textSize="100dp"/>

</android.support.constraint.ConstraintLayout>




MyFragment2

class MyFragment2 : Fragment() {

companion object {
fun newInstance(): MyFragment2 {
return MyFragment2()
}
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
var view: View? = inflater.inflate(R.layout.fragment_other, container, false)
return view
}
}

최소부분만 구현.




XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:orientation="vertical">

<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:tabGravity="fill"
app:tabMode="fixed" tools:layout_editor_absoluteY="188dp" tools:layout_editor_absoluteX="0dp"/>

<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent" app:layout_constraintTop_toBottomOf="@+id/tabs"
android:layout_marginTop="252dp" app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="8dp"
app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="8dp"
android:layout_marginBottom="8dp" app:layout_constraintBottom_toBottomOf="parent"/>

</android.support.constraint.ConstraintLayout>


//Tablayout
implementation 'com.android.support:design:28.0.0'




Google Fragment 문서https://developer.android.com/guide/components/fragments?hl=ko


아래 예제는 동일한 xml레이아웃에 내용만 바꿔넣는 형태.

참고http://www.kotlincodes.com/kotlin/viewpager-in-kotlin-android/


반응형