ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 안드로이드 앱 개발 연습 - 6 | ViewPager & TabLayout
    Archive/캡스톤디자인 2022. 3. 27. 01:10

    ViewPager with Fragment

    FragmentAdapter.kt

    class FragmentAdapter(fragmentActivity: FragmentActivity) : FragmentStateAdapter(fragmentActivity) {
        var fragmentList = listOf<Fragment>()
        override fun getItemCount(): Int {
            return fragmentList.size
        }
    
        override fun createFragment(position: Int): Fragment {
            return fragmentList.get(position)
        }
    
    }

    MainActivity.kt

    class MainActivity : AppCompatActivity() {
        val binding by lazy { ActivityMainBinding.inflate(layoutInflater) }
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(binding.root)
    
            val fragmentList = listOf(FragmentA(), FragmentB(), FragmentC(), FragmentD())
            val adapter = FragmentAdapter(this)
            adapter.fragmentList = fragmentList
            binding.viewPager.adapter = adapter
    
            val tabTitles = listOf<String>("A", "B", "C", "D")
            TabLayoutMediator(binding.tabLayout, binding.viewPager) { tab, position ->
                tab.text = tabTitles[position]
            }.attach()
        }
    }

    TabLayoutMediator로 tabLayout과 viewPager를 연결


    ViewPager with View

    item_viewpager.xml

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout 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/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Title"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>

    CustomPagerAdapter.kt

    class CustomPagerAdapter: RecyclerView.Adapter<Holder>() {
        var textList = listOf<String>()
    
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Holder {
            val binding = ItemViewpagerBinding.inflate(LayoutInflater.from(parent.context), parent, false)
            return Holder(binding)
        }
    
        override fun onBindViewHolder(holder: Holder, position: Int) {
            val text = textList[position]
            holder.setText(text)
        }
    
        override fun getItemCount(): Int {
            return textList.size
        }
    }
    
    class Holder(val binding: ItemViewpagerBinding): RecyclerView.ViewHolder(binding.root) {
        fun setText(text: String) {
            binding.textView.text = text
        }
    }

    MainActivity.kt

    class MainActivity : AppCompatActivity() {
        val binding by lazy { ActivityMainBinding.inflate(layoutInflater) }
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(binding.root)
    
            val textList = listOf("Title: View A", "Title: View B", "Title: View C", "Title: View D")
            val customAdapter = CustomPagerAdapter()
            customAdapter.textList = textList
            binding.viewPager.adapter = customAdapter
    
            val tabTitles = listOf("View A", "View B", "View C", "View D")
            TabLayoutMediator(binding.tabLayout, binding.viewPager) { tab, position ->
                tab.text = tabTitles[position]
            }.attach()
        }
    }

     

    댓글