使用Kotlin Fragments 进行底部导航

在底部导航中有三个不同的片段用于三个不同的选项卡
使用Kotlin Fragments 进行底部导航_第1张图片

1)创建片段布局1:fragment_home.xml




    

    

2)创建片段布局2:fragment_dashboard.xml




    

    

3)创建片段布局3:fragment_notification.xml




    

    

4)为HomeFragment创建Fragment文件:FragmentHome.kt

package fragment

import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import com.example.chirag.kotlindemo.R

class FragmentHome : Fragment() {
    /**
     * 初始化newInstance以传递paameters 
     */
    companion object {
        fun newInstance(): FragmentHome {
            var fragmentHome = FragmentHome()
            var args = Bundle()
            fragmentHome.arguments = args
            return fragmentHome
        }

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }


    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        var rootView = inflater!!.inflate(R.layout.fragment_home, container, false)
        return rootView
    }

    override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        var editTextHome = view!!.findViewById(R.id.editTextHome) as EditText
    }
}

5)为DashboardFragment创建Fragment文件:FragmentDashboard.kt

package fragment

import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.example.chirag.kotlindemo.R

class FragmentDashboard : Fragment(){

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        var rootView = inflater!!.inflate(R.layout.fragment_dashboard, container, false)
        return rootView
    }
}

6)为notificationFragment创建Fragment文件:FragmentNotification.kt

package fragment

import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.example.chirag.kotlindemo.R

class FragmentNotification : Fragment(){

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        var rootView = inflater!!.inflate(R.layout.fragment_notification, container, false)
        return rootView
    }
}

7)最后创建MainActivity布局文件:activity_bottom_navigation.xml




    

    

    


8)在res / menu / navigation.xml下创建底部导航选项的菜单文件:




    

    

    


9)创建活动BottomNavigationActivity.kt

package com.example.chirag.kotlindemo

import android.os.Bundle
import android.support.design.widget.BottomNavigationView
import android.support.v4.app.Fragment
import android.support.v7.app.AppCompatActivity
import android.view.MenuItem
import android.widget.FrameLayout
import fragment.FragmentDashboard
import fragment.FragmentHome
import fragment.FragmentNotification


class BottomNavigationActivity : AppCompatActivity() {

    private var content: FrameLayout? = null

    private val mOnNavigationItemSelectedListener = object : BottomNavigationView.OnNavigationItemSelectedListener {

        override fun onNavigationItemSelected(item: MenuItem): Boolean {
            when (item.itemId) {
                R.id.navigation_home -> {

                    val fragment = FragmentHome.Companion.newInstance()
                    addFragment(fragment)

                    return true
                }
                R.id.navigation_dashboard -> {
                    val fragment = FragmentDashboard()
                    addFragment(fragment)
                    return true
                }
                R.id.navigation_notifications -> {
                    var fragment = FragmentNotification()
                    addFragment(fragment)
                    return true
                }
            }
            return false
        }

    }

    /**
     * 在容器中添加/替换片段[framelayout] 
     */
    private fun addFragment(fragment: Fragment) {
        supportFragmentManager
                .beginTransaction()
                .setCustomAnimations(R.anim.design_bottom_sheet_slide_in, R.anim.design_bottom_sheet_slide_out)
                .replace(R.id.content, fragment, fragment.javaClass.getSimpleName())
                .addToBackStack(fragment.javaClass.getSimpleName())
                .commit()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_bottom_navigation)

        content = findViewById(R.id.content) as FrameLayout
        val navigation = findViewById(R.id.navigation) as BottomNavigationView
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)


        val fragment = FragmentHome.Companion.newInstance()
        addFragment(fragment)
    }

}

你可能感兴趣的:(Android开发)