ViewPager Android
Bài đăng này đã không được cập nhật trong 3 năm
This article will demostrate how to add viewpager to your app for swipe gesture and populating your MainActivity with a pager and adapter holding your various fragments.
Create a new Application and open the activity_main.xml and add a TabLayout and ViewPager
activity_main.xml
<?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">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:tabGravity="fill"
app:tabMode="fixed" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/purple_200" />
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Next we will set up our Fragments. For this demo i will create 3 seperate fragments Home, Profile and About. Create a new package and name it fragments then add 3 fragments.
HomeFragment.kt
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.example.viewpagerdemo.R
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"
class HomeFragment : Fragment() {
private var param1: String? = null
private var param2: String? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
param1 = it.getString(ARG_PARAM1)
param2 = it.getString(ARG_PARAM2)
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_home, container, false)
}
companion object {
@JvmStatic
fun newInstance() =
HomeFragment().apply {
}
}
}
fragment_home.xml
<?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=".fragments.HomeFragment">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="40sp"
android:gravity="center"
android:text="Home Fragment" />
</FrameLayout>
ProfileFragment.kt
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.example.viewpagerdemo.R
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"
class ProfileFragment : Fragment() {
private var param1: String? = null
private var param2: String? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
param1 = it.getString(ARG_PARAM1)
param2 = it.getString(ARG_PARAM2)
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_profile, container, false)
}
companion object {
@JvmStatic
fun newInstance() =
ProfileFragment().apply {
}
}
}
fragment_profile.xml
<?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=".fragments.ProfileFragment">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="40sp"
android:gravity="center"
android:text="Profile Fragment" />
</FrameLayout>
AboutFragment.kt
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.example.viewpagerdemo.R
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"
class AboutFragment : Fragment() {
private var param1: String? = null
private var param2: String? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
param1 = it.getString(ARG_PARAM1)
param2 = it.getString(ARG_PARAM2)
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_about, container, false)
}
companion object {
@JvmStatic
fun newInstance() =
AboutFragment().apply {
}
}
}
fragment_about.xml
<?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=".fragments.AboutFragment">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="40sp"
android:gravity="center"
android:text="About Screen" />
</FrameLayout>
Next lets create our custom adapter class 'PagerAdapter' in a new package 'adapter'.
PagerAdapter.kt
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
class PagerAdapter(manager: FragmentManager) : FragmentPagerAdapter(manager) {
private val mFragmentList: ArrayList<Fragment> = ArrayList()
private val mFragmentTitleList: ArrayList<String> = ArrayList()
override fun getItem(position: Int): Fragment {
return mFragmentList.get(position)
}
override fun getCount(): Int {
return mFragmentList.size
}
fun addFragment(fragment: Fragment, title: String) {
mFragmentList.add(fragment)
mFragmentTitleList.add(title)
}
override fun getPageTitle(position: Int): CharSequence? {
return mFragmentTitleList[position]
}
}
Now lets open the MainActivity and setup the tabs and viewpager adapter.
MainActivity.kt
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.viewpager.widget.ViewPager
import com.example.viewpagerdemo.adapter.PagerAdapter
import com.example.viewpagerdemo.fragments.AboutFragment
import com.example.viewpagerdemo.fragments.HomeFragment
import com.example.viewpagerdemo.fragments.ProfileFragment
import com.google.android.material.tabs.TabLayout
class MainActivity : AppCompatActivity() {
private lateinit var viewpager: ViewPager
private lateinit var tabs: TabLayout
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
initViews()
setupViewPager()
}
private fun initViews() {
tabs = findViewById(R.id.tabs)
viewpager = findViewById(R.id.viewpager)
}
private fun setupViewPager() {
val adapter = PagerAdapter(supportFragmentManager)
val homeFragment: HomeFragment = HomeFragment.newInstance()
val profileFragment: ProfileFragment = ProfileFragment.newInstance()
val aboutFragment: AboutFragment = AboutFragment.newInstance()
adapter.addFragment(homeFragment, "Home")
adapter.addFragment(profileFragment, "Profile")
adapter.addFragment(aboutFragment, "About")
viewpager.adapter = adapter
tabs.setupWithViewPager(viewpager)
}
}
Thats it we all set. One last thing is to set the parent theme to NoActionBar so our app is in full screen with no toolbar (Unless u prefer to have one).
Run App.....
All rights reserved