Navigation和BottomNavigationView结
2021-09-26 本文已影响0人
小杨不想努力了
-
创建TaoBaoActivity
package com.example.mvvmdemo.BottonNavigationView.Activity
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.setupWithNavController
import com.example.mvvmdemo.R
import kotlinx.android.synthetic.main.activity_taobao_main.*
class TaoBaoActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_taobao_main)
initView()
}
private fun initView() {
//方法1:去fragment管理器里通过id找到NavHostFragment
val navHostFragment: NavHostFragment = supportFragmentManager.findFragmentById(R.id.taoBao_navigation_host_fragment) as NavHostFragment
bottomNavigationView.setupWithNavController(navHostFragment.navController)
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/taoBao_navigation_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/taobao_navigation_config" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:itemIconTint="@color/Orange"
app:itemTextColor="@color/Orange"
app:labelVisibilityMode="auto"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/taobao_bottom_navigation_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>
2. 创建Fragment
(1)创建BaseFragment,然后子Fragment继承即可
package com.example.mvvmdemo.base
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
abstract class BaseFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val rootView: View = inflater.inflate(getLayoutResId(), container, false)
//初始化View
initView(rootView)
return rootView
}
//可以选择性继承实现
open fun initView(rootView: View) {
}
//子类传入View,必须实现
abstract fun getLayoutResId(): Int
}
(2)创建其它Fragment
HomeFragment
package com.example.mvvmdemo.BottonNavigationView.Fragment
import com.example.mvvmdemo.R
import com.example.mvvmdemo.base.BaseFragment
class HomeFragment : BaseFragment() {
override fun getLayoutResId(): Int {
return R.layout.fragment_taobao_home
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="首页"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
OnSellFragment
package com.example.mvvmdemo.BottonNavigationView.Fragment
import com.example.mvvmdemo.R
import com.example.mvvmdemo.base.BaseFragment
class OnSellFragment: BaseFragment() {
override fun getLayoutResId(): Int {
return R.layout.fragment_taobao_on_sell
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="特惠"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
SelectedFragment
package com.example.mvvmdemo.BottonNavigationView.Fragment
import com.example.mvvmdemo.R
import com.example.mvvmdemo.base.BaseFragment
class SelectedFragment : BaseFragment() {
override fun getLayoutResId(): Int {
return R.layout.fragment_taobao_selected
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="精选"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
SearchFragment
package com.example.mvvmdemo.BottonNavigationView.Fragment
import com.example.mvvmdemo.R
import com.example.mvvmdemo.base.BaseFragment
class SearchFragment : BaseFragment() {
override fun getLayoutResId(): Int {
return R.layout.fragment_taobao_search
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="搜索"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
-
创建Menu文件
在res目录下创建menu子目录,创建taobao_bottom_navigation_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/taoBao_home"
android:icon="@mipmap/home_normal"
android:title="首页" />
<item
android:id="@+id/taoBao_select"
android:icon="@mipmap/select_normal"
android:title="精选" />
<item
android:id="@+id/taoBao_onSell"
android:icon="@mipmap/red_packet_normal"
android:title="特惠" />
<item
android:id="@+id/taoBao_search"
android:icon="@mipmap/search_normal"
android:title="搜索" />
</menu>
4.创建Navigation文件
在res目录下创建navigation子目录,创建taobao_navigation_config.xml
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/taobao_navigation_config"
app:startDestination="@id/taoBao_home">
<fragment
android:id="@+id/taoBao_home"
android:name="com.example.mvvmdemo.BottonNavigationView.Fragment.HomeFragment"
android:label="HomeFragment" />
<fragment
android:id="@+id/taoBao_select"
android:name="com.example.mvvmdemo.BottonNavigationView.Fragment.OnSellFragment"
android:label="OnSellFragment" />
<fragment
android:id="@+id/taoBao_onSell"
android:name="com.example.mvvmdemo.BottonNavigationView.Fragment.SearchFragment"
android:label="SearchFragment" />
<fragment
android:id="@+id/taoBao_search"
android:name="com.example.mvvmdemo.BottonNavigationView.Fragment.SelectedFragment"
android:label="SelectedFragment" />
</navigation>
记得在TaoBaoActivity的布局文件的 androidx.fragment.app.FragmentContainerView 标签加入如下内容,才会和taobao_navigation_config,xml产生关联
<androidx.fragment.app.FragmentContainerView
android:name="androidx.navigation.fragment.NavHostFragment"
app:defaultNavHost="true"
app:navGraph="@navigation/taobao_navigation_config" />
在TaoBaoActivity中添加如下代码对Fragment进行管理
private fun initView() {
//去fragment管理器里通过id找到NavHostFragment
val navHostFragment: NavHostFragment = supportFragmentManager.findFragmentById(R.id.taoBao_navigation_host_fragment) as NavHostFragment
bottomNavigationView.setupWithNavController(navHostFragment.navController)
}