我们项目框架中通常会使用首页,消息,我的作为底部按钮进行切换,今天分享下功能的具体实现
注:点击查看BaseActivity和BaseFragment
目录
第一步:创建布局文件和子Fragment
1.创建底部导航栏
这里嵌套了一个Linerlayout,资源文件大家自定义就好
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/ima_bootom_title_main"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical"
android:background="@color/blue">
<ImageView
android:layout_width="wrap_content"
android:layout_height="25dp"
android:src="@mipmap/main_home"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="首页"
android:textSize="14sp"
android:textColor="@color/white"/>
</LinearLayout>
<LinearLayout
android:id="@+id/ima_bootom_title_my"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical"
android:background="@color/xian">
<ImageView
android:layout_width="wrap_content"
android:layout_height="25dp"
android:src="@mipmap/main_my"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我的"
android:textSize="14sp"
android:textColor="@color/white"/>
</LinearLayout>
</LinearLayout>
2.创建布局a_main
创建一个LinearLayout 命名ly,用来作为Fragment的容器,底部引用导航栏
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical">
<LinearLayout
android:id="@+id/ly"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical">
</LinearLayout>
<include layout="@layout/g_bottom_title" />
</LinearLayout>
3.创建Mainfragment喝MyFragment两个子布局,xml布局使用默认布局就好
class MainFragment : BaseFragment() {
override val layout = R.layout.f_main
}
class MyFragment : BaseFragment() {
override val layout = R.layout.f_my
}
第二步 创建MainActivity,添加布局
class MainActivity : BaseActivity() {
override val layout = R.layout.a_main
private var mainFragment = MainFragment()
private var myFragment = MyFragment()
private var fragments = ArrayList<BaseFragment>()
private var lastfragment: Int = 0//用于记录上个选择的Fragment
companion object {
var isForeground = false
}
@RequiresApi(Build.VERSION_CODES.M)
override fun initView() {
fragments.add(mainFragment)
fragments.add(myFragment)
switchFragment(lastfragment, 0)
}
override fun initEvent() {
ima_bootom_title_main.setOnClickListener {
if (lastfragment != 0) {
switchFragment(lastfragment, 0)
lastfragment = 0
ima_bootom_title_main.setBackgroundResource(R.color.blue)
ima_bootom_title_my.setBackgroundResource(R.color.xian)
}
}
ima_bootom_title_my.setOnClickListener {
if (lastfragment != 1) {
switchFragment(lastfragment, 1)
lastfragment = 1
ima_bootom_title_main.setBackgroundResource(R.color.xian)
ima_bootom_title_my.setBackgroundResource(R.color.blue)
}
}
}
//切换Fragment
private fun switchFragment(lament: Int, index: Int) {
val transaction: FragmentTransaction = supportFragmentManager.beginTransaction()
transaction.hide(fragments[lament])//隐藏上个Fragment
if (!fragments[index].isAdded) {
transaction.add(R.id.ly, fragments[index])
}
transaction.show(fragments[index]).commitAllowingStateLoss()
}
override fun onResume() {
isForeground = true
super.onResume()
}
override fun onPause() {
isForeground = false
super.onPause()
}
}