美文网首页工作生活
kotlin tablayout+viewpager+fragm

kotlin tablayout+viewpager+fragm

作者: 我妈叫我二乖 | 来源:发表于2019-07-02 21:32 被阅读0次
    WechatIMG140.png
    import android.os.Bundle
    import android.support.design.widget.TabLayout
    import android.support.v4.app.Fragment
    import android.support.v4.app.FragmentManager
    import android.support.v4.app.FragmentPagerAdapter
    import android.support.v7.app.AppCompatActivity
    import android.view.LayoutInflater
    import android.view.View
    import com.trace.reportsystem.R
    import com.trace.reportsystem.fragment.*
    import kotlinx.android.synthetic.main.activity_main.*
    import kotlinx.android.synthetic.main.layout_main_icon.view.*
    
    class MainActivity : AppCompatActivity() {
    
        private var homePagerFragment: BaseFragment = HomePagerFragment()
        private var transactionFragment: BaseFragment = TransactionFragment()
        private var taskFragment: BaseFragment = TaskFragment()
        private var mineFragment: BaseFragment = MineFragment()
    
        private var list: List<BaseFragment> = listOf(homePagerFragment,
                transactionFragment, taskFragment, mineFragment)
    
        var titles = listOf("首页", "工作", "项目", "我的")
    
        //选中时候的icon
        var select = listOf(R.mipmap.tabbar_activity_h,
                R.mipmap.tabbar_find_h, R.mipmap.tabbar_reserve_h,
                R.mipmap.tabbar_scene_h)
    
        //未选中时候的icon
        var unselect = listOf(R.mipmap.tabbar_activity_n,
                R.mipmap.tabbar_find_n, R.mipmap.tabbar_reserve_n,
                R.mipmap.tabbar_scene_n)
    
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
    
            setContentView(R.layout.activity_main)
    
            main_vp.adapter = MyViewPagerAdapter(supportFragmentManager, list)
    
            setCustomIcon()
    
            main_tl.addOnTabSelectedListener(object : TabLayout.ViewPagerOnTabSelectedListener(main_vp) {
                override fun onTabSelected(tab: TabLayout.Tab?) {
                    super.onTabSelected(tab)
    
                    var position: Int = tab!!.position
                    uploadView(tab.customView!!, titles[position], select[position],
                            resources.getColor(R.color.colorAccent))
    
                }
    
                override fun onTabUnselected(tab: TabLayout.Tab?) {
                    super.onTabUnselected(tab)
    
                    var position: Int = tab!!.position
                    uploadView(tab.customView!!, titles[position], unselect[position],
                            resources.getColor(R.color.colorPrimary))
                }
    
            })
        }
    
        private fun setCustomIcon() {
            for (i in 0 until titles.size) {
                main_tl.addTab(main_tl.newTab().setCustomView(makeTabView(titles[i],
                        if (i == 1) select[i] else unselect[i],
                        if (i == 1) resources.getColor(R.color.colorAccent)
                        else resources.getColor(R.color.colorPrimary))),
                        i == 1)
    
            }
    
        }
    
        private fun makeTabView(title: String, imgId: Int, colorId: Int): View? {
            val tabView = LayoutInflater.from(this).inflate(R.layout.layout_main_icon, null)
            uploadView(tabView, title, imgId, colorId)
            return tabView
        }
    
    
        private fun uploadView(tabView: View, title: String, imgId: Int, colorId: Int) {
            tabView.textview?.text = title
            tabView.textview?.setTextColor(colorId)
            tabView.imageview?.setImageResource(imgId)
        }
    
        class MyViewPagerAdapter(fm: FragmentManager?, var list: List<BaseFragment>) : FragmentPagerAdapter(fm) {
            override fun getItem(p: Int): Fragment {
                return list[p]
            }
    
            override fun getCount(): Int {
                return list.size
            }
    
        }
    
    }
    
    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.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"
        tools:context=".activity.MainActivity">
    
        <com.trace.reportsystem.layout.NoScrollViewPager
            android:id="@+id/main_vp"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_constraintBottom_toTopOf="@+id/main_tl" />
    
        <android.support.design.widget.TabLayout
            android:id="@+id/main_tl"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:tabIndicatorHeight="0dp"
            app:tabRippleColor="@color/translate"
            app:tabSelectedTextColor="@color/blue" />
    
    
    </android.support.constraint.ConstraintLayout>
    

    相关文章

      网友评论

        本文标题:kotlin tablayout+viewpager+fragm

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