在片段上调用底部导航 kotlin

时间:2021-05-10 13:10:33

标签: android kotlin android-fragments

下面的 kotlin 代码允许您修改底部导航中显示的片段,显示主片段但当我单击转到第二个和第三个片段时却没有显示,这是什么原因?片段是 3 个,一个是主片段,然后是其他 2 个片段,但是这些片段没有显示,在单击时调用 startfragment 但它显示一个空视图。

MainActivity.kt

class MainActivity : AppCompatActivity() {

    private val bottleFragment = BottleFragment()
    private val homeFragment = HomeFragment()
    private val profileInfoFragment = ProfileInfoFragment()
    private val fragmentHistory = mutableListOf<Fragment>(homeFragment)
    private var activeFragment: Fragment = homeFragment
    var ID_AUTH_FIREBASE = ""
    var ADMIN_KEY = ""
    var email = ""

    private val bottomNavigationListener = BottomNavigationView.OnNavigationItemSelectedListener {
        when (it.itemId) {
            R.id.navigation_home -> {
                startFragment(homeFragment)
                return@OnNavigationItemSelectedListener true
            }
            R.id.navigation_profile -> {
                System.out.println("Sono qui nel profilo")
                startFragment(bottleFragment)
                return@OnNavigationItemSelectedListener true
            }
            R.id.navigation_bottle -> {
                startFragment(profileInfoFragment)
                return@OnNavigationItemSelectedListener true
            }
            else -> {
                return@OnNavigationItemSelectedListener false
            }
        }
    }

    @SuppressLint("MissingPermission")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        navigation.setOnNavigationItemSelectedListener(bottomNavigationListener)
        startFragment(homeFragment)
        checkPermissions()
        buttonlogout.setOnClickListener { logout() }
        buttonsave.setOnClickListener { save() }
        window.setFlags(
            WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN
        );
        email = intent.getStringExtra("Email")
        ID_AUTH_FIREBASE = intent.getStringExtra("ID_AUTH_FIREBASE")
        ADMIN_KEY = intent.getStringExtra("ADMIN_KEY")
        val datiPersona = APISupport.getDatiPersona(ID_AUTH_FIREBASE, email, ADMIN_KEY)
        textNome.setText(datiPersona[0])
        txtCognome.setText(datiPersona[1])
        txtResidenza.setText(datiPersona[2])
        txtTelefono.setText(datiPersona[3])
        txtEmail.setText(email)
        hometext.text = datiPersona[0] + " " + datiPersona[1]
        txtcfdesc.setText(datiPersona[5])
        txtnumeropolizaedit.setText(datiPersona[6])
        val url: URL = URL(APISupport.geturlLogo(ID_AUTH_FIREBASE, email, ADMIN_KEY))
        val bmp: Bitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream())
        imageLogo.setImageBitmap(bmp)
        txtDataFineEdit.setText(datiPersona[8])
        txtDataInizioEdit.setText(datiPersona[7])

    }


    override fun onResume() {
        super.onResume()
        checkPermissions()
    }

    override fun onPause() {
        super.onPause()
        checkPermissions()
    }

    fun save() {
        var value = APISupport.savedatipersona(
            ID_AUTH_FIREBASE,
            email,
            ADMIN_KEY,
            txtCognome.text.toString(),
            textNome.text.toString(),
            txtTelefono.text.toString(),
            txtResidenza.text.toString(),
            txtDataInizioEdit.text.toString(),
            txtDataFineEdit.text.toString(),
            txtnumeropolizaedit.text.toString(),
            txtcfdesc.text.toString()
        )
    }

    fun logout() {
        val intent = Intent(this, SplashActivity::class.java)
        startActivity(intent)
    }

    fun startFragment(fragment: Fragment, removeLast: Boolean = false) {

        if (fragment == homeFragment)
            startHomeBackground()
        else
            stopHomeBackground()

        if (removeLast)
            fragmentHistory.removeAt(fragmentHistory.lastIndex)
        if (fragmentHistory.last() != fragment)
            fragmentHistory.add(fragment)
        activeFragment = fragment
    }

bottom_navigation.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/navigation_home"
        android:icon="@drawable/profile_nav_selector"
        android:tooltipText="Profilo"
        android:title="Profilo" />

    <item
        android:id="@+id/navigation_profile"
        android:icon="@drawable/home_nav_selector"
        android:tooltipText="Veicoli"
        android:title="Veicoli" />

    <item
        android:id="@+id/navigation_bottle"
        android:icon="@drawable/bottle_nav_selector"
        android:tooltipText="Info"
        android:title="Info" />

</menu>

MainAcitivity.xml

 <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="0dp"
        android:layout_height="85dp"
        android:background="#FFFAFA"
        android:backgroundTint="@android:color/transparent"
        app:itemBackground="@android:color/transparent"
        app:itemIconSize="28dp"
        app:itemIconTint="@color/bottom_navigation_color"
        app:labelVisibilityMode="unlabeled"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/bottom_navigation" />

0 个答案:

没有答案