kotlin tablayout+viewpager+fragment

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 = 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) : FragmentPagerAdapter(fm) {
        override fun getItem(p: Int): Fragment {
            return list[p]
        }

        override fun getCount(): Int {
            return list.size
        }

    }

}



    

    



你可能感兴趣的:(kotlin tablayout+viewpager+fragment)