티스토리 뷰
build.gradle (Module: app)
dependencies {
implementation 'androidx.viewpager2:viewpager2:1.0.0'
}
activity_main.xml
<?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">
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/viewPage2"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
viewpager_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="#FFBBAA"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="혀가 길지 않은 개발자"
android:textStyle="bold"
android:textSize="30dp"
android:gravity="center"/>
<TextView
android:id="@+id/txtCount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="1 page"
android:textSize="30dp"
android:textStyle="bold"
android:gravity="center" />
</LinearLayout>
ViewPagerAdapter.kt
package com.jwsoft.kotlinproject
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
class ViewPagerAdapter(strData: ArrayList<String>)
: RecyclerView.Adapter<ViewPagerAdapter.MyViewHolder>() {
val arrData = strData
inner class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var txtCount: TextView = itemView.findViewById(R.id.txtCount)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val context = parent.context
val inflater: LayoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
val view: View = inflater.inflate(R.layout.viewpager_item, parent, false)
val viewHolder = MyViewHolder(view)
return viewHolder
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
holder.txtCount.text = arrData[position]
}
override fun getItemCount(): Int {
return arrData.size
}
}
MainActivity.kt
package com.jwsoft.kotlinproject
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.viewpager2.widget.ViewPager2
class MainActivity : AppCompatActivity() {
private lateinit var viewPager2: ViewPager2
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var arrStr: ArrayList<String> = ArrayList()
for (i in 1..100) {
arrStr.add(i.toString())
}
var adapter = ViewPagerAdapter(arrStr)
viewPager2 = findViewById(R.id.viewPage2)
viewPager2.orientation = ViewPager2.ORIENTATION_HORIZONTAL
viewPager2.adapter = adapter
}
}
'Android > Kotlin' 카테고리의 다른 글
[Kotlin] ViewPager2 + FragmentStateAdapter (0) | 2020.07.06 |
---|---|
[Kotlin] CoordinatorLayout + AppBarLayout + Toolbar + RecyclerView (0) | 2020.07.04 |
[Kotlin] TabLayout (0) | 2020.07.02 |
[Kotlin] FrameLayout (0) | 2020.07.02 |
[Kotlin] Fragment (0) | 2020.07.01 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 자바
- CoordinatorLayout
- View
- JSONArray
- ArrayList
- 혀가 길지 않은 개발자
- ViewPager2
- recyclerview
- 안드로이드 #코틀린 #Android #Kotlin
- handler
- MVVM
- 코틀린
- Vue.js #Vue.js + javascript
- ViewModel
- activity
- XML
- TabLayout
- DataBinding
- Livedata
- Architecture Pattern
- Intent
- Kotlin
- Android
- Design Pattern
- JSONObject
- coroutine
- fragment
- James Kim
- 안드로이드
- java
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함