1. 程式人生 > >(kotlin)TabLayout+viewPager+fragment

(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<String>()
    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<String>? = 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<String>) {
        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() {

    }

 

}