我可能会遗漏一些非常简单的东西,但我绝对可以使用一些帮助。
我有一个活动,我想在其中加载具有不同内容和不同工具栏样式的不同片段(即具有自定义View和CollapsingToolbarLayout的工具栏(其中也包含自定义布局))。同时,我希望有一个抽屉实例,可以通过单个活动(即MainActivity)访问。
我已经通过示例找到了类似的东西,但所有这些都使用了新的活动来填充他们的内容,或者他们只是隐藏/显示不适合我的工具栏内容,因为我自定义工具栏每个片段的视图,因此我无法找出布局视图的顺序。我所经历的一些解决方案就像:
答案 0 :(得分:1)
编辑:
这不是一个干净的代码,但它是一个有效的代码。我只是想告诉你。您可以使用类似BaseFragment的东西并构建更好的代码。在MainFragment中,您可以使用导航抽屉,在DetailFragment中,您可以使用工具栏上的后退箭头。如果需要,您也可以在DetailFragment中使用导航视图。您也可以在任何您喜欢的片段中使用折叠工具栏。
MainActivity
class MainActivity : AppCompatActivity() {
private var actionBarToggle : ActionBarDrawerToggle? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setUpNavigationList()
if (savedInstanceState == null){
supportFragmentManager
.beginTransaction()
.replace(R.id.frame_container, MainFragment())
.addToBackStack(null)
.commit()
}
}
override fun onOptionsItemSelected(item: MenuItem?): Boolean {
when(item!!.itemId){
android.R.id.home -> onBackPressed()
}
return true
}
fun setActionBarDrawerToggle(toolbar: Toolbar){
actionBarToggle = ActionBarDrawerToggle(this, drawer_layout, toolbar, R.string.drawer_open, R.string.drawer_close)
actionBarToggle?.syncState()
drawer_layout.closeDrawer(Gravity.START)
}
fun lockDrawer(lock: Boolean){
val lockMode = if (lock) DrawerLayout.LOCK_MODE_LOCKED_CLOSED else DrawerLayout.LOCK_MODE_UNLOCKED
drawer_layout.setDrawerLockMode(lockMode)
actionBarToggle?.isDrawerIndicatorEnabled = lock
}
private fun setUpNavigationList(){
val items = arrayListOf("Detail Fragment")
val adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, items)
list_navigation.adapter = adapter
list_navigation.setOnItemClickListener { parent, view, position, id ->
when (position){
0 -> {
supportFragmentManager
.beginTransaction()
.replace(R.id.frame_container, DetailFragment())
.addToBackStack(null)
.commit()
}
}
drawer_layout.closeDrawer(Gravity.START)
}
}
}
MainFragment
class MainFragment() : Fragment() {
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
return inflater!!.inflate(R.layout.fragment_main, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
(activity as AppCompatActivity).setSupportActionBar(toolbar_detail)
(activity as MainActivity).lockDrawer(false)
(activity as MainActivity).setActionBarDrawerToggle(toolbar_main)
}
}
DetailFragment
class DetailFragment() : Fragment() {
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
return inflater!!.inflate(R.layout.fragment_detail, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
(activity as MainActivity).setSupportActionBar(toolbar_detail)
(activity as MainActivity).lockDrawer(true)
(activity as MainActivity).supportActionBar!!.setDisplayHomeAsUpEnabled(true)
}
}
主要活动布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.coskun.drawer.MainActivity">
<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="@+id/list_navigation"
android:layout_width="150dp"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
android:layout_gravity="start"/>
</android.support.v4.widget.DrawerLayout>
MainFragment Layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.coskun.drawer.DetailFragment">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_main"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:background="@color/colorPrimary"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/main_fragment"/>
</LinearLayout>
DetailFragment Layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.coskun.drawer.DetailFragment">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_detail"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:background="@color/colorAccent"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/detail_fragment"/>
</LinearLayout>
以前的答案:
You can define navigation drawer logic in activity and you can use separate toolbars for each fragment. You just need to sync state navigation drawer state with fragment toolbars.
fun setActionBarDrawerToggle(toolbar: Toolbar){
actionBarToggle = ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close)
actionBarToggle?.syncState()
drawerLayout.closeDrawer(Gravity.START)
}
fun lockDrawer(lock: Boolean){
val lockMode = if (lock) DrawerLayout.LOCK_MODE_LOCKED_CLOSED else DrawerLayout.LOCK_MODE_UNLOCKED
drawerLayout.setDrawerLockMode(lockMode)
actionBarToggle?.isDrawerIndicatorEnabled = lock
}
您可以查看此代码。我在每个片段中将工具栏设置为操作栏,让它们为自己的菜单充气并与活动抽屉状态同步。如果这有帮助,请告诉我。