androidkotlinswipeandroid-viewpager2

how can i setup swipe fucntion in androidstudio kotlin


so when I started my project first I modified the MainActivity class inside the OnCreate to initialize the functions for its activity_main.xml (imagebuttons recyclerview background change with switch etc.) that was my main activity. Then I had an idea of adding swipe feature to go from the main layout to second layout by swiping left to right. I created the second layout the same way as the Mainactivity was created. I created the class and then connected the xml with the class (tools:context=".FoldersActivity") and initialized the functions.

I came across ViewPager2 in the Internet but I could not find any solutions to make what I needed. Thanks.


Solution

  • First add ViewPager2 in activity_main This will act as the container that allows users to swipe between the layouts

    activity_main

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    

    Instead of creating separate activities for each layout, you'll create two fragments. MainFragment for your main layout.FoldersFragment for the second layout.

    MainFragment.kt

    class MainFragment : Fragment(R.layout.fragment_main) {
        override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
            super.onViewCreated(view, savedInstanceState)
            // Initialize your functions for Main Layout here
        }
    }
    

    FoldersFragment.kt

    class FoldersFragment : Fragment(R.layout.fragment_folders) {
        override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
            super.onViewCreated(view, savedInstanceState)
            // Initialize your functions for Folders Layout here
        }
    }
    

    Now we can set up the ViewPager2 to handle swiping between the fragments. Create an adapter that tells ViewPager2 which fragment to display.

    MainActivty.kt

    class MainActivity : AppCompatActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
    
            // Reference to the ViewPager2 in activity_main.xml
            val viewPager: ViewPager2 = findViewById(R.id.viewPager)
    
            // List of fragments to swipe between
            val fragments = listOf(MainFragment(), FoldersFragment())
    
            // Set up the adapter
            val adapter = ViewPagerAdapter(this, fragments)
            viewPager.adapter = adapter
        }
    }
    

    ViewPagerAdapter.kt

    class ViewPagerAdapter(
        activity: AppCompatActivity,
        private val fragments: List<Fragment>
    ) : FragmentStateAdapter(activity) {
    
        // Returns the number of fragments
        override fun getItemCount(): Int = fragments.size
    
        // Returns the fragment to display for the current page
        override fun createFragment(position: Int): Fragment {
            return fragments[position]
        }
    }
    

    enter image description here