{"id":36967,"date":"2024-11-01T09:53:42","date_gmt":"2024-11-01T09:53:42","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=36967"},"modified":"2024-11-01T11:42:47","modified_gmt":"2024-11-01T11:42:47","slug":"course-on-kotlin-android-app-development-view-pager-2-screen-composition-with-swipe-navigation","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/36967\/","title":{"rendered":"course on Kotlin Android App Development, View Pager 2 &#8211; Screen Composition with Swipe Navigation"},"content":{"rendered":"<p><body><\/p>\n<p>\n        When developing Android apps, a feature that allows users to swipe through multiple pages is very useful.<br \/>\n        To facilitate this, Google provides a component called ViewPager2.<br \/>\n        In this tutorial, we will implement a simple app using ViewPager2 and explain the basic concepts and code in detail.\n    <\/p>\n<h2>1. What is ViewPager2?<\/h2>\n<p>\n        ViewPager2 is a UI component that allows users to swipe through multiple pages.<br \/>\n        The main features of ViewPager2 are as follows:\n    <\/p>\n<ul>\n<li>Support for vertical and horizontal swiping<\/li>\n<li>Support for Fragment and RecyclerView<\/li>\n<li>Ability to set page transition effects<\/li>\n<\/ul>\n<h2>2. Setting up ViewPager2<\/h2>\n<p>\n        To use ViewPager2, you need to add a dependency to your Gradle file.<br \/>\n        Add the following line to your <code>build.gradle<\/code> file:\n    <\/p>\n<pre>\n        dependencies {\n            implementation \"androidx.viewpager2:viewpager2:1.1.0\"\n        }\n    <\/pre>\n<p>\n        Let&#8217;s write the XML layout file needed to use ViewPager2.\n    <\/p>\n<h3>2.1. XML Layout<\/h3>\n<p>\n        Below is an example of a basic layout that includes ViewPager2.\n    <\/p>\n<pre>\n        &lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\n        &lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\n            xmlns:app=\"http:\/\/schemas.android.com\/apk\/res-auto\"\n            xmlns:tools=\"http:\/\/schemas.android.com\/tools\"\n            android:layout_width=\"match_parent\"\n            android:layout_height=\"match_parent\"\n            tools:context=\".MainActivity\"&gt;\n\n            &lt;androidx.viewpager2.widget.ViewPager2\n                android:id=\"@+id\/viewPager\"\n                android:layout_width=\"match_parent\"\n                android:layout_height=\"match_parent\"\n                app:layout_constraintTop_toTopOf=\"parent\"\n                app:layout_constraintBottom_toBottomOf=\"parent\"\n                app:layout_constraintStart_toStartOf=\"parent\"\n                app:layout_constraintEnd_toEndOf=\"parent\"\/&gt;\n        &lt;\/androidx.constraintlayout.widget.ConstraintLayout&gt;\n    <\/pre>\n<h2>3. Implementing ViewPager2 Adapter<\/h2>\n<p>\n        ViewPager2 requires an adapter to display pages.<br \/>\n        Below is a simple example of adapter implementation. This adapter uses custom Fragments to create each page.\n    <\/p>\n<h3>3.1. Creating Fragment<\/h3>\n<p>\n        First, create a Fragment class that represents each page.\n    <\/p>\n<pre>\n        class CustomFragment : Fragment() {\n\n            override fun onCreateView(\n                inflater: LayoutInflater, container: ViewGroup?,\n                savedInstanceState: Bundle?\n            ): View? {\n                val view = inflater.inflate(R.layout.fragment_custom, container, false)\n                return view\n            }\n        }\n    <\/pre>\n<h3>3.2. Implementing Fragment Adapter<\/h3>\n<p>\n        Next, implement the adapter by inheriting from FragmentStateAdapter.\n    <\/p>\n<pre>\n        class ViewPagerAdapter(fragmentActivity: FragmentActivity) : FragmentStateAdapter(fragmentActivity) {\n\n            override fun getItemCount(): Int {\n                return 3 \/\/ Number of pages\n            }\n\n            override fun createFragment(position: Int): Fragment {\n                return CustomFragment() \/\/ Return a different Fragment for each page\n            }\n        }\n    <\/pre>\n<h2>4. Setting up MainActivity<\/h2>\n<p>\n        Now, let&#8217;s connect ViewPager2 and the adapter in MainActivity.\n    <\/p>\n<pre>\n        class MainActivity : AppCompatActivity() {\n\n            private lateinit var viewPager: ViewPager2\n\n            override fun onCreate(savedInstanceState: Bundle?) {\n                super.onCreate(savedInstanceState)\n                setContentView(R.layout.activity_main)\n\n                viewPager = findViewById(R.id.viewPager)\n                viewPager.adapter = ViewPagerAdapter(this)\n            }\n        }\n    <\/pre>\n<h2>5. Configuring Fragment Layout<\/h2>\n<p>\n        Now, let&#8217;s configure the layout to be used in each Fragment.<br \/>\n        In this example, we will simply display text.\n    <\/p>\n<pre>\n        &lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\n        &lt;LinearLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\n            android:layout_width=\"match_parent\"\n            android:layout_height=\"match_parent\"\n            android:orientation=\"vertical\"\n            android:gravity=\"center\"&gt;\n\n            &lt;TextView\n                android:id=\"@+id\/textView\"\n                android:layout_width=\"wrap_content\"\n                android:layout_height=\"wrap_content\"\n                android:text=\"Hello, ViewPager2!\"\n                android:textSize=\"24sp\"\/&gt;\n\n        &lt;\/LinearLayout&gt;\n    <\/pre>\n<h2>6. Adding Swipe Page Transition Effects<\/h2>\n<p>\n        ViewPager2 applies swipe animations by default, but you can customize it to apply various page transition effects.\n    <\/p>\n<pre>\n        class ViewPagerAdapter(fragmentActivity: FragmentActivity) : FragmentStateAdapter(fragmentActivity) {\n\n            \/\/ ... existing code ...\n\n            override fun createFragment(position: Int): Fragment {\n                \/\/ You can return a different Fragment based on position to vary page content\n                return when (position) {\n                    0 -> FirstFragment()\n                    1 -> SecondFragment()\n                    else -> ThirdFragment()\n                }\n            }\n        }\n    <\/pre>\n<p>\n        Here, you can define your desired page transition effect and apply it using ViewPager2&#8217;s<br \/>\n        <code>setPageTransformer<\/code> method.\n    <\/p>\n<pre>\n        viewPager.setPageTransformer { page, position ->\n            page.alpha = 0f\n            if (position >= -1 && position <= 1) { \/\/ [-1,1]\n                page.alpha = 1 - Math.abs(position)\n            }\n        }\n    <\/pre>\n<h2>7. Conclusion<\/h2>\n<p>\n        In this tutorial, we learned how to configure multiple swipable pages using ViewPager2.<br \/>\n        You can enhance user experience by utilizing various page transition effects.<br \/>\n        You can develop a richer app by adding additional features or integrating with other UI components.\n    <\/p>\n<p>\n        Enjoy the joy of Android development!\n    <\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>When developing Android apps, a feature that allows users to swipe through multiple pages is very useful. To facilitate this, Google provides a component called ViewPager2. In this tutorial, we will implement a simple app using ViewPager2 and explain the basic concepts and code in detail. 1. What is ViewPager2? ViewPager2 is a UI component &hellip; <a href=\"https:\/\/atmokpo.com\/w\/36967\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;course on Kotlin Android App Development, View Pager 2 &#8211; Screen Composition with Swipe Navigation&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[143],"tags":[],"class_list":["post-36967","post","type-post","status-publish","format-standard","hentry","category-kotlin-android-app-development"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>course on Kotlin Android App Development, View Pager 2 - Screen Composition with Swipe Navigation - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/atmokpo.com\/w\/36967\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"course on Kotlin Android App Development, View Pager 2 - Screen Composition with Swipe Navigation - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"When developing Android apps, a feature that allows users to swipe through multiple pages is very useful. To facilitate this, Google provides a component called ViewPager2. In this tutorial, we will implement a simple app using ViewPager2 and explain the basic concepts and code in detail. 1. What is ViewPager2? ViewPager2 is a UI component &hellip; \ub354 \ubcf4\uae30 &quot;course on Kotlin Android App Development, View Pager 2 &#8211; Screen Composition with Swipe Navigation&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/36967\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:53:42+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:42:47+00:00\" \/>\n<meta name=\"author\" content=\"root\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:site\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:label1\" content=\"\uae00\uc4f4\uc774\" \/>\n\t<meta name=\"twitter:data1\" content=\"root\" \/>\n\t<meta name=\"twitter:label2\" content=\"\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04\" \/>\n\t<meta name=\"twitter:data2\" content=\"3\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/36967\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36967\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"course on Kotlin Android App Development, View Pager 2 &#8211; Screen Composition with Swipe Navigation\",\"datePublished\":\"2024-11-01T09:53:42+00:00\",\"dateModified\":\"2024-11-01T11:42:47+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36967\/\"},\"wordCount\":318,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Kotlin Android app development\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/36967\/\",\"url\":\"https:\/\/atmokpo.com\/w\/36967\/\",\"name\":\"course on Kotlin Android App Development, View Pager 2 - Screen Composition with Swipe Navigation - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:53:42+00:00\",\"dateModified\":\"2024-11-01T11:42:47+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36967\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/36967\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/36967\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"course on Kotlin Android App Development, View Pager 2 &#8211; Screen Composition with Swipe Navigation\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/atmokpo.com\/w\/#website\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/atmokpo.com\/w\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"contentUrl\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"width\":400,\"height\":400,\"caption\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\"},\"image\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/bebubo4\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\",\"name\":\"root\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"caption\":\"root\"},\"sameAs\":[\"http:\/\/atmokpo.com\/w\"],\"url\":\"https:\/\/atmokpo.com\/w\/author\/root\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"course on Kotlin Android App Development, View Pager 2 - Screen Composition with Swipe Navigation - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/atmokpo.com\/w\/36967\/","og_locale":"ko_KR","og_type":"article","og_title":"course on Kotlin Android App Development, View Pager 2 - Screen Composition with Swipe Navigation - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"When developing Android apps, a feature that allows users to swipe through multiple pages is very useful. To facilitate this, Google provides a component called ViewPager2. In this tutorial, we will implement a simple app using ViewPager2 and explain the basic concepts and code in detail. 1. What is ViewPager2? ViewPager2 is a UI component &hellip; \ub354 \ubcf4\uae30 \"course on Kotlin Android App Development, View Pager 2 &#8211; Screen Composition with Swipe Navigation\"","og_url":"https:\/\/atmokpo.com\/w\/36967\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:53:42+00:00","article_modified_time":"2024-11-01T11:42:47+00:00","author":"root","twitter_card":"summary_large_image","twitter_creator":"@bebubo4","twitter_site":"@bebubo4","twitter_misc":{"\uae00\uc4f4\uc774":"root","\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04":"3\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/36967\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/36967\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"course on Kotlin Android App Development, View Pager 2 &#8211; Screen Composition with Swipe Navigation","datePublished":"2024-11-01T09:53:42+00:00","dateModified":"2024-11-01T11:42:47+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/36967\/"},"wordCount":318,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Kotlin Android app development"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/36967\/","url":"https:\/\/atmokpo.com\/w\/36967\/","name":"course on Kotlin Android App Development, View Pager 2 - Screen Composition with Swipe Navigation - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:53:42+00:00","dateModified":"2024-11-01T11:42:47+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/36967\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/36967\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/36967\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"course on Kotlin Android App Development, View Pager 2 &#8211; Screen Composition with Swipe Navigation"}]},{"@type":"WebSite","@id":"https:\/\/atmokpo.com\/w\/#website","url":"https:\/\/atmokpo.com\/w\/","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","description":"","publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/atmokpo.com\/w\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Organization","@id":"https:\/\/atmokpo.com\/w\/#organization","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","url":"https:\/\/atmokpo.com\/w\/","logo":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/","url":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","contentUrl":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","width":400,"height":400,"caption":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8"},"image":{"@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/bebubo4"]},{"@type":"Person","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7","name":"root","image":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","caption":"root"},"sameAs":["http:\/\/atmokpo.com\/w"],"url":"https:\/\/atmokpo.com\/w\/author\/root\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/36967","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/comments?post=36967"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/36967\/revisions"}],"predecessor-version":[{"id":36968,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/36967\/revisions\/36968"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=36967"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=36967"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=36967"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}