美文网首页
Kotlin中fragment的替换

Kotlin中fragment的替换

作者: 残非 | 来源:发表于2019-10-31 08:23 被阅读0次

首先是XML的布局

<?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:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.goketech.smartcommunity.MainActivity">
<android.support.design.widget.BottomNavigationView
    android:id="@+id/nav_view"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="0dp"
    android:layout_marginStart="0dp"
    android:background="?android:attr/windowBackground"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:menu="@menu/bottom_nav_menu"/>
<FrameLayout
        android:id="@+id/layout_page"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@id/nav_view"></FrameLayout>

</android.support.constraint.ConstraintLayout>

上面是fragment,下面是button按钮的使用,因为我用的是Android studio自带的模板,它会自动创建一个带底部导航的项目

MainActivity的创建
首先要创建两个fragment,这个想必大家都会吧,在这里就不交大家使用了
接下来,上代码:

class MainActivity : AppCompatActivity() {
//创建一个事务
private var fragmentTransaction: FragmentTransaction? = null
//private lateinit var textMessage: TextView
private val onNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { tm ->
    fragmentTransaction = supportFragmentManager.beginTransaction()
    when (tm.itemId) {
        R.id.navigation_home -> {
//点击home按钮,出现第一个fragment的页面
            fragmentTransaction!!.replace(R.id.layout_page, lingliFragment!!).commit()
            //textMessage.setText(R.string.title_home)
            return@OnNavigationItemSelectedListener true
        }
        R.id.navigation_dashboard -> {
//点击home按钮,出现第二个fragment的页面
            fragmentTransaction!!.replace(R.id.layout_page, shouFragment!!).commit()
            //textMessage.setText(R.string.title_dashboard)
            return@OnNavigationItemSelectedListener true
        }
        R.id.navigation_notifications -> {
//点击home按钮,出现第一个fragment的页面
           fragmentTransaction!!.replace(R.id.layout_page, homeFragment!!).commit()
            //textMessage.setText(R.string.title_notifications)
            return@OnNavigationItemSelectedListener true
        }
    }
    false
}

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    val navView: BottomNavigationView = findViewById(R.id.nav_view)

    navView.setOnNavigationItemSelectedListener(onNavigationItemSelectedListener)
    
    initFragment()
    //在这里初始化一个页面
    fragmentTransaction!!.replace(R.id.layout_page,lingliFragment!!).commit()
    
    

}
  //把数据做成全局变量
private var lingliFragment: LingliFragment? = null
private var shouFragment: ShouFragment? = null

fun initFragment() {
//创建fragment
    lingliFragment = LingliFragment()
    shouFragment = ShouFragment()
}
}

相关文章

网友评论

      本文标题:Kotlin中fragment的替换

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