当我转到另一个片段,然后将其更改为“后退”图标时,如何从NavigationView中删除Hamburger图标? ///////////////////////////////////////////////////// ///////////////////////////////////////////////////// ///////////////////////////////////////////////////// ////
我一直在添加以下代码,以便他们可以将汉堡包图标替换为返回图标:
navigation.menu.getItem(1).setOnMenuItemClickListener() { //I miss an error
toogle.isDrawerIndicatorEnabled = false
}
navigation.menu.getItem(1).setOnMenuItemClickListener {
supportActionBar?.setHomeButtonEnabled(true)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
supportActionBar?.setHomeAsUpIndicator(R.drawable.ic_return) //I miss an error
}
navigation.menu.getItem(2).setOnMenuItemClickListener {
supportActionBar?.setHomeButtonEnabled(true)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
supportActionBar?.setHomeAsUpIndicator(R.drawable.ic_return) //I miss an error
}
navigation.menu.getItem(3).setOnMenuItemClickListener {
supportActionBar?.setHomeButtonEnabled(true)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
supportActionBar?.setHomeAsUpIndicator(R.drawable.ic_return) //I miss an error
}
NavigationActivity.kt:
package com.example.gonzalo.proyecto_android_2018.activities
import android.support.v4.app.Fragment
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.design.widget.NavigationView
import android.support.v4.view.GravityCompat
import android.support.v7.app.ActionBarDrawerToggle
import android.support.v7.widget.Toolbar
import android.view.MenuItem
import com.example.gonzalo.proyecto_android_2018.R
import com.example.gonzalo.proyecto_android_2018.fragments.*
import kotlinx.android.synthetic.main.activity_navigation.*
class NavigationActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener{
lateinit var tb:Toolbar
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_navigation)
//SETUP TOOLBAR
tb = toolbar as Toolbar
//tb.title = "LIGHT BUS"
//SETUP NAVIGATIONVIEW
setupNavigation()
setupFragment(HomeFragment())
navigation.menu.getItem(0).isChecked = true
}
private fun setupNavigation() {
//THERE IS NO TOGY CLASS, I SAY IT NOT TO BE CONFUSED WITH THE VARIABLE "toogle"
val toogle = ActionBarDrawerToggle(this, drawer, tb, R.string.drawerAbierto, R.string.drawerCerrado)
toogle.isDrawerIndicatorEnabled = true //HERE I SAY THAT THE SANDWICHITO SYMBOL APPEARS TO OPEN AND CLOSE THE NAVIGATIONVIEW
drawer.addDrawerListener(toogle)
toogle.syncState() //HERE I SAY TO SYNCHRONIZE THE STATE OPEN OR CLOSED. EXAMPLE, MUST CLOSE WHEN OPEN AND MUST BE OPEN WHEN CLOSED
navigation.setNavigationItemSelectedListener(this) //HERE I SAY TO THIS NAVIGATION (navigation) THAT IT WILL BE ALREADY ADDED WITHIN NAVIGATION ACTIVITY
}
private fun setupFragment(fragment: Fragment){ //Replace the "R.id.frameLayout" with the id of the "fragment" that will be placed in the "activity_navigation.xml"
supportFragmentManager.beginTransaction().replace(R.id.frameLayout, fragment).commit()
}
override fun onNavigationItemSelected(item: MenuItem): Boolean {
when(item.itemId){
R.id.home -> setupFragment(HomeFragment())
R.id.mi_cuenta -> setupFragment(MiCuentaFragment())
R.id.pagos -> setupFragment(PagosFragment())
R.id.promociones -> setupFragment(PromocionesFragment())
R.id.regalo_amigo -> setupFragment(RegaloAmigoFragment())
R.id.ayuda -> setupFragment(AyudaFragment())
}
drawer.closeDrawer(GravityCompat.START) //HERE I SAY, AFTER YOU HAVE CHANGED FRAGMENT, CLOSE THE DRAWER
return true
}
override fun onBackPressed() {
if(drawer.isDrawerOpen(GravityCompat.START)){
drawer.closeDrawer(GravityCompat.START)
}
else{
super.onBackPressed()
}
}
}
activity_navigation.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer"
android:fitsSystemWindows="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar"/>
<FrameLayout
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize">
</FrameLayout>
</android.support.constraint.ConstraintLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navigation"
app:itemTextColor="#009688"
app:menu="@menu/main_menu"
app:headerLayout="@layout/nav_header"
android:fitsSystemWindows="true"
android:layout_gravity = "start"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
</android.support.v4.widget.DrawerLayout>