美文网首页
viewpager2+tablayout实现页面的切换

viewpager2+tablayout实现页面的切换

作者: 背锅TV丶伴奏大师 | 来源:发表于2023-08-17 14:38 被阅读0次

    1.布局文件

    <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                <com.google.android.material.tabs.TabLayout
                    android:id="@+id/tabLayout"
                    app:layout_constraintTop_toTopOf="parent"
                    android:layout_width="match_parent"
                    android:layout_height="40dp"
                    app:tabGravity="start"
                    app:tabBackground="@android:color/transparent"
                    app:tabRippleColor="@color/white"
                    app:tabUnboundedRipple="false"
                    app:tabMode="scrollable"/>
    
                <androidx.viewpager2.widget.ViewPager2
                    android:id="@+id/viewPager"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/tabLayout"/>
            </androidx.constraintlayout.widget.ConstraintLayout>
    

    2.子布局tab_item_layout.xml

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingHorizontal="8dp"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:orientation="vertical">
        <View
            android:id="@+id/tv_index"
            android:layout_width="20dp"
            android:layout_height="6dp"
            android:layout_marginTop="-6dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/tv_name"
            android:background="@drawable/shape_6ccdff_00a8ff_3r_bg"/>
        <TextView
            android:id="@+id/tv_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="14sp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:textColor="@color/black_333"
            android:gravity="center"
            android:maxLength="4"
            android:textStyle="bold"/>
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    3.交互代码

    private var mediator: TabLayoutMediator? = null
        private val mTitles = arrayOf("全部","待支付","已支付","已换绑","已完成","已取消")
        private fun initViewPager() {
            val fragments:MutableList<Fragment> = ArrayList()
            fragments.add(BuyRecordFragment.newInstance(null))
            fragments.add(BuyRecordFragment.newInstance("0"))
            fragments.add(BuyRecordFragment.newInstance("1"))
            fragments.add(BuyRecordFragment.newInstance("2"))
            fragments.add(BuyRecordFragment.newInstance("3"))
            fragments.add(BuyRecordFragment.newInstance("4"))
            mBinding.apply {
                //viewPager.offscreenPageLimit = titles.size
                viewPager.adapter = object : FragmentStateAdapter(supportFragmentManager,lifecycle) {
                    override fun getItemCount(): Int {
                        return fragments.size
                    }
                    override fun createFragment(position: Int): Fragment {
                        //FragmentStateAdapter内部自己会管理已实例化的fragment对象,所以不需要考虑复用的问题.
                        return fragments[position]
                    }
                }
                viewPager.registerOnPageChangeCallback(changeCallback)
                mediator = TabLayoutMediator(tabLayout, viewPager) { tab, position ->
                    //这里可以自定义TabView
                    val view = LayoutInflater.from(context).inflate(R.layout.tab_item_layout, null)
                    val tv_name =view.findViewById<TextView>(R.id.tv_name)
                    val tv_index =view.findViewById<View>(R.id.tv_index)
                    tv_name.text = mTitles[position]
                    tv_index.visibility=View.INVISIBLE
                    tab.customView = view
                }
                //要执行这一句才是真正将两者绑定起来
                mediator?.attach()
            }
        }
        private val changeCallback: ViewPager2.OnPageChangeCallback = object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                //可以来设置选中时tab的大小
                val tabCount: Int = mBinding.tabLayout.tabCount
                for (i in 0 until tabCount) {
                    val tab: TabLayout.Tab = mBinding.tabLayout.getTabAt(i)!!
                    val tabView = tab.customView
                    val tv_name = tabView?.findViewById<TextView>(R.id.tv_name)
                    val tv_index = tabView?.findViewById<View>(R.id.tv_index)
                    if (tab.position == position) {
                        if (tv_name != null) {
                            tv_name.textSize = 18f
                        }
                        if (tv_index != null) {
                            tv_index.visibility=View.VISIBLE
                        }
                    } else {
                        if (tv_name != null) {
                            tv_name.textSize = 14f
                        }
                        if (tv_index != null) {
                            tv_index.visibility=View.INVISIBLE
                        }
                    }
                }
            }
        }
        override fun onDestroy() {
            mediator?.detach()
            mBinding.viewPager.unregisterOnPageChangeCallback(changeCallback)
            super.onDestroy()
        }
    

    相关文章

      网友评论

          本文标题:viewpager2+tablayout实现页面的切换

          本文链接:https://www.haomeiwen.com/subject/edfrmdtx.html