Tuesday, December 31, 2019

View Pager2 - Kotlin

View Pager2 in Kotlin


ViewPager is an android widget that allows or helps you to change the screen without changing the activity. 
In simpler words, if you want to change the app screen without making an Activity then you can use ViewPager.

So lets start to create sample app by just follow few steps. 


To use View Pager2 in project, follow below mentioned steps
  • Step 1:  Add this dependency in app gradle.
implementation "androidx.viewpager2:viewpager2:1.0.0"

  • Step 2:  Add below code into activity_main.xml

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

  • Step3:  Create Three Fragment (FragmentOne, FragmentTwo, FragmentThree) like below.
class FragmentOne : Fragment(){
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_two, container, false)
    }

    companion object{
        fun newInstance() = FragmentOne()
    }
}
  • Step 4:  And finally add code into our Activity.
class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN)
        setContentView(R.layout.activity_main)

        val fragmentList : ArrayList<Fragment> =
            arrayListOf(FragmentOne.newInstance(), FragmentTwo.newInstance(),
                FragmentThree.newInstance())

        val pagerAdapter = ScreenSlidePagerAdapter(this,fragmentList)
        viewPager2.adapter = pagerAdapter


        viewPager2.registerOnPageChangeCallback(
            object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                Log.e("=> ","View pager selected position")
            }
        })
    }

    override fun onBackPressed() {
        if (viewPager2.currentItem == 0) {
            super.onBackPressed()
        } else {
            viewPager2.currentItem = viewPager2.currentItem - 1
        }
    }

    private inner class ScreenSlidePagerAdapter(
        fa : FragmentActivity,
        private val fragments:ArrayList<Fragment>)
        : FragmentStateAdapter(fa) {
        override fun getItemCount(): Int = fragments.size
        override fun createFragment(position: Int): Fragment = fragments[position]
    }
}
You can also download my full source code
https://github.com/sakib143/ViewPager2-Kotlin.git


1 comment:

  1. Casino Games | Dr.MD.com
    For the 원주 출장마사지 most complete casino 포천 출장안마 games available, 김해 출장샵 including a top-quality Vegas slot machine, we've 강릉 출장안마 assembled an all-new 김포 출장마사지 version of our classic games.‎Game · ‎Cash Out · ‎Casino · ‎Slots

    ReplyDelete