我正在将Android Studio 3.1.4与Kotlin
一起使用。我创建了一个选择NavigationDrawer
模板的新项目。我在MainFragment
上添加了两个片段SecondFragment
和Mainactivity
。启动activity
时,显示MainFragment
,然后我通过单击SecondFragment
中的Camera项目调用了NavigationDrawer
。当我按下后退按钮时,它转到MainFragment,但ActionBar仍显示后退箭头而不是汉堡包。
以前,我与android.support.v4.widget.DrawerLayout
一起从事过一个项目,并且一切正常。
下面是我的MainActiviy
代码:
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.app.AppCompatActivity
import android.view.Menu
import android.view.MenuItem
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.app_bar_main.*
class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
showMainFragment()
val toggle = ActionBarDrawerToggle(this, drawer_layout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close)
drawer_layout.addDrawerListener(toggle)
toggle.syncState()
nav_view.setNavigationItemSelectedListener(this)
}
override fun onBackPressed() {
if (drawer_layout.isDrawerOpen(GravityCompat.START)) {
drawer_layout.closeDrawer(GravityCompat.START)
} else if (supportFragmentManager.backStackEntryCount > 0) {
supportFragmentManager.popBackStack()
} else {
super.onBackPressed()
}
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.main, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
val itemId = item.itemId
if (itemId == android.R.id.home) {
onBackPressed()
}
when (item.itemId) {
R.id.action_settings -> return true
else -> return super.onOptionsItemSelected(item)
}
}
override fun onNavigationItemSelected(item: MenuItem): Boolean {
// Handle navigation view item clicks here.
when (item.itemId) {
R.id.nav_camera -> {
// Handle the camera action
val fragment = SecondFragment()
val fragmentTransaction = supportFragmentManager.beginTransaction()
fragmentTransaction.replace(R.id.fragment_container, fragment, "OK")
fragmentTransaction.addToBackStack(null)
fragmentTransaction.commit()
}
R.id.nav_gallery -> {
}
R.id.nav_slideshow -> {
}
R.id.nav_manage -> {
}
R.id.nav_share -> {
}
R.id.nav_send -> {
}
}
drawer_layout.closeDrawer(GravityCompat.START)
return true
}
private fun showMainFragment(){
val fragment = MainFragment()
val fragmentTransaction = supportFragmentManager.beginTransaction()
fragmentTransaction.replace(R.id.fragment_container, fragment)
fragmentTransaction.commit()
}
}
MainFragment
代码:
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.support.v7.app.AppCompatActivity
import kotlinx.android.synthetic.main.app_bar_main.*
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"
/**
* A simple [Fragment] subclass.
*
*/
class MainFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
var view : View = inflater.inflate(R.layout.fragment_main, container, false)
return view
}
SecondFragment
代码:
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.app.AppCompatActivity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"
class SecondFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
val view : View = inflater!!.inflate(R.layout.fragment_surah, container, false)
(activity as AppCompatActivity).supportActionBar!!.setDisplayHomeAsUpEnabled(true)
val toolbar = activity!!.findViewById<android.support.v7.widget.Toolbar>(R.id.toolbar)
(activity as AppCompatActivity).setSupportActionBar(toolbar)
toolbar.setNavigationIcon(R.drawable.abc_ic_ab_back_material)
return view
}
}
答案 0 :(得分:0)
在SecondFragment的onDestroy
上设置此行
(activity as AppCompatActivity).supportActionBar!!.setDisplayHomeAsUpEnabled(false)
答案 1 :(得分:0)
您永远不会将NavigationIcon设置回汉堡包。
也许尝试:
toolbar.setNavigationIcon(R.drawable.hamburger)
在您的MainFragment中?
答案 2 :(得分:0)
汉堡因为这个原因没有显示-
val工具栏=活动!!。findViewById(R.id.toolbar) (活动为AppCompatActivity).setSupportActionBar(工具栏) 工具栏.setNavigationIcon(R.drawable.abc_ic_ab_back_material)
要同时处理后退按钮和汉堡包,请在活动中添加以下代码。-
supportFragmentManager.addOnBackStackChangedListener(android.support.v4.app.FragmentManager.OnBackStackChangedListener {
if (supportFragmentManager.getBackStackEntryCount() > 0) {
supportActionBar?.setDisplayHomeAsUpEnabled(true); // show back button
toolbar?.setNavigationOnClickListener(View.OnClickListener { onBackPressed() })
} else {
//show hamburger
supportActionBar?.setDisplayHomeAsUpEnabled(false);
toogle.syncState();
toolbar?.setNavigationOnClickListener(View.OnClickListener {
drawer.openDrawer(GravityCompat.START);
})
setTitle(resources.getString(R.string.app_name))
}
})
并从第二个片段中删除以下代码-
(activity as AppCompatActivity).supportActionBar!!.setDisplayHomeAsUpEnabled(true)
val toolbar = activity!!.findViewById<android.support.v7.widget.Toolbar>(R.id.toolbar)
(activity as AppCompatActivity).setSupportActionBar(toolbar)
toolbar.setNavigationIcon(R.drawable.abc_ic_ab_back_material)