'페이저'에 해당되는 글 2건

  1. 2023.07.11 ViewPager2 with Fragment 프레그먼트 뷰페이저2
  2. 2023.07.11 ViewPager2 뷰페이저2
반응형

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

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?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"
    tools:context=".OneFragment">
 
    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FF0000"
        android:gravity="center_horizontal|center_vertical"
        android:text="One Fragment" />
 
</FrameLayout>
 

 

fragment_one.xml

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.example.myapplication
 
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
 
class OneFragment : Fragment() {
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_one, container, false)
    }
}
 

 

OneFragment.kt

같은 형태로 TextView 배경색만 바꿔서 TwoFragment, ThreeFragment도 만들어 준다.

 

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
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.example.myapplication
 
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
 
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
 
        val viewPager2: ViewPager2 = findViewById(R.id.viewpager)
        viewPager2.adapter = MyFragmentPagerAdapter(this)
 
        //viewPager2.orientation = ViewPager2.ORIENTATION_VERTICAL
        // 이 설정을 적용하면 스와이프 방향이 세로로 바뀐다.
    }
}
 
class MyFragmentPagerAdapter(activity: FragmentActivity) : FragmentStateAdapter(activity) {
    val fragments: List<Fragment>
    init {
        fragments = listOf(OneFragment(), TwoFragment(), ThreeFragment())
        Log.d("Sean""Fragments size: ${fragments.size}")
    }
 
    override fun getItemCount(): Int {
        return fragments.size
    }
 
    override fun createFragment(position: Int): Fragment {
        return fragments[position]
    }
}
 

 

MainActivity.kt

 

실행 화면

 

반응형
Posted by J-sean
:

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
: