当前位置:   article > 正文

TabLayout使用以及自定义tab标签

TabLayout使用以及自定义tab标签
<?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">


    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tab"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:tabTextAppearance="@style/myTab"

        style="@style/myTabLayout"
        tools:ignore="SpeakableTextPresentCheck" />

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/vp"

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"

        app:layout_constraintTop_toBottomOf="@+id/tab" />


</androidx.constraintlayout.widget.ConstraintLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
package com.example.myapplication


import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.Lifecycle
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.example.myapplication.databinding.TestVp2Binding
import com.google.android.material.tabs.TabLayoutMediator


class VP2Demo : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)


        val inflate = TestVp2Binding.inflate(layoutInflater)
        setContentView(inflate.root)


        val mutableListOf = mutableListOf<Fragment>()
        val fragment = Fragment(R.layout.test_a)

        val fragment2 = Fragment(R.layout.test_view)


        val fragment3 = Fragment(R.layout.test_net)


        val childAt = inflate.vp.getChildAt(0)
        (childAt as? RecyclerView)?.setOverScrollMode(View.OVER_SCROLL_NEVER)
        mutableListOf.add(fragment)
        mutableListOf.add(fragment2)
        mutableListOf.add(fragment3)

        inflate.vp.adapter = MyVpAdapter(mutableListOf, supportFragmentManager, lifecycle)
        val titles = mutableListOf<String>()
        titles.add("新闻")
        titles.add("新闻2")
        titles.add("新闻3")



        TabLayoutMediator(
            inflate.tab, inflate.vp
        ) { p0, index ->
            run {
                p0.setText(titles[index])
                p0.setIcon(R.drawable.zc)
                if(index==1){
                    val textView = TextView(baseContext)
                    textView.text = "我是自定义的标签哈哈~~~"
                    p0.setCustomView(textView)
                }

            }
        }.attach()




    }


    class MyVpAdapter(list: MutableList<Fragment>, fm: FragmentManager, lf: Lifecycle) :
        FragmentStateAdapter(fm, lf) {

        private var listS = emptyList<Fragment>()

        init {
            listS = list

        }

        override fun getItemCount(): Int {
            return listS.size
        }

        override fun createFragment(position: Int): Fragment {

            return listS[position]
        }

    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  <style name="myTab" parent="@android:style/TextAppearance.Widget.TabWidget">
        <item name="textAllCaps">false</item>
        <item name="android:textSize">30sp</item>



    </style>
    <style name="myTabLayout" parent="Widget.Design.TabLayout">
        <item name="tabIndicatorColor">#00FF00</item>
        <item name="tabSelectedTextColor">#FF0000</item>

        <item name="tabTextColor">#0000FF</item>
        <item name="tabRippleColor">@color/yellow</item>

    </style>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/代码探险家/article/detail/940073
推荐阅读
相关标签
  

闽ICP备14008679号