Android 首页动态切换Fragment

我们项目框架中通常会使用首页,消息,我的作为底部按钮进行切换,今天分享下功能的具体实现

注:点击查看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()
    }


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值