ViewPager2 뷰페이저2

Android 2023. 7. 11. 16:53 |
반응형

안드로이드 앱 프로그래밍 with 코틀린 11-5 뷰페이저2

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    tools:context=".MainActivity">
 
    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>
 

 

activity_main.xml

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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">
 
    <TextView
        android:id="@+id/itemPagerTextView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal|center_vertical"
        android:text="TextView" />
</LinearLayout>
 

 

item_main.xml

 

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.example.myapplication
 
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.widget.ViewPager2
 
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
 
        val data = mutableListOf<String>()
        for(i in 1..3){
            data.add("Item: $i")
        }
 
        val viewPager2: ViewPager2 = findViewById(R.id.viewpager)
        viewPager2.adapter = MyPagerAdapter(data)
    }
}
 
class MyPagerViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    val itemPagerTextView: TextView = itemView.findViewById(R.id.itemPagerTextView)
}
 
class MyPagerAdapter(private val itemList: MutableList<String>): RecyclerView.Adapter<RecyclerView.ViewHolder>(){
    override fun getItemCount(): Int {
        return itemList.size
    }
 
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_main, parent, false)
        return MyPagerViewHolder(view)
    }
 
    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        (holder as MyPagerViewHolder).itemPagerTextView.text = itemList[position]
        when (position % 3) {
            0 -> (holder as MyPagerViewHolder).itemPagerTextView.setBackgroundColor(Color.RED)
            1 -> (holder as MyPagerViewHolder).itemPagerTextView.setBackgroundColor(Color.BLUE)
            2 -> (holder as MyPagerViewHolder).itemPagerTextView.setBackgroundColor(Color.GREEN)
        }
    }
}
 

 

MainActivity.kt

 

실행 화면

 

반응형
Posted by J-sean
: