(kotlin)TabLayout+viewPager+fragment

导入兼容包

implementation 'com.android.support:design:27.1.1'

一、main

private val mTitleIcons = intArrayOf(R.drawable.msg2, R.drawable.home1, R.drawable.management2)
private val mTitleIconsSelected = intArrayOf(R.drawable.msg1, R.drawable.home1, R.drawable.management1)
private val mTitleIconsUnselected = intArrayOf(R.drawable.msg2, R.drawable.home2, R.drawable.management2)

private fun init(){
    var list = ArrayList()
    for (i in mTitleNames.indices) {
        list.add(mTitleNames[i])
    }

    val mAdapter = MyFragmentPagerAdapter(supportFragmentManager)
    mAdapter.setTitle(list)
    viewPager.setAdapter(mAdapter)
    viewPager.setCurrentItem(1)
    viewPager.setOffscreenPageLimit(3)
    tabLayout.setupWithViewPager(viewPager)//关联

    for (i in mTitleIcons.indices) {
        //mTitleIcons[i]和mTitleNames[i]是放图片和文字的资源的数组
        tabLayout.getTabAt(i)!!.setIcon(mTitleIcons[i])//.setText(mTitleNames[i])
        //这个是设置选中和没选中的文字的颜色
        tabLayout.setTabTextColors(Color.parseColor("#666666"), Color.parseColor("#026767"))
    }
}

private fun event(){
    tabLayout.setOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
        override fun onTabSelected(tab: TabLayout.Tab) {
            tab.setIcon(mTitleIconsSelected[tab.position])
            tabLayout.setTabTextColors(Color.parseColor("#666666"), Color.parseColor("#026767"))
        }

        override fun onTabUnselected(tab: TabLayout.Tab) {
            tab.setIcon(mTitleIconsUnselected[tab.position])
            tabLayout.setTabTextColors(Color.parseColor("#666666"), Color.parseColor("#026767"))
        }

        override fun onTabReselected(tab: TabLayout.Tab) {

        }
    })

}

二、适配器

class MyFragmentPagerAdapter(fm: FragmentManager) : FragmentPagerAdapter(fm) {

    private val PAGER_COUNT = 3
    private val myFragment1: MyFragment1
    private val myFragment2: MyFragment2
    private val myFragment3: MyFragment3
    private var list: List? = null

    init {
        myFragment1 = MyFragment1()
        myFragment2 = MyFragment2()
        myFragment3 = MyFragment3()
    }

    override fun getItem(position: Int): Fragment? {
        var fragment: Fragment? = null
        when (position) {
            0 -> fragment = myFragment1
            1 -> fragment = myFragment2
            2 -> fragment = myFragment3
        }
        return fragment
    }

    override fun getCount(): Int {
        return PAGER_COUNT
    }

    override fun instantiateItem(container: ViewGroup, position: Int): Any {
        return super.instantiateItem(container, position)
    }

    override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
        super.destroyItem(container, position, `object`)
        println("position Destory$position")
    }

    fun setTitle(list: List) {
        this.list = list
    }

    override fun getPageTitle(position: Int): CharSequence? {
        return list!![position]
    }
}

三、Fragment

class MyFragment1 : Fragment(){

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view:View = inflater.inflate(R.layout.fragment_msg, container, false)
        return view
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        	super.onActivityCreated(savedInstanceState);
        	init();
       	http();
    }

    private void init() {
    swipeRefreshLayout=view.findViewById(R.id.swipeRefreshLayout);
           mRecyclerView =view.findViewById(R.id.recyclerView);
    mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(),1));
    mAdapter = new BatteryAdapter1(R.layout.battery,mDatas);
    mRecyclerView.setAdapter(mAdapter);
    }

    private fun event() {

    }

}

你可能感兴趣的:(kotlin)