gpt4 book ai didi

android - 在不同的 fragment 中用折叠工具栏替换工具栏

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:35:59 24 4
gpt4 key购买 nike

我可能在这里遗漏了一些非常简单的东西,但我绝对需要一些帮助。

我有一个 Activity ,我想在其中加载具有不同内容和不同工具栏样式的不同 fragment (即具有自定义 View 和 CollapsingToolbarLayout 的工具栏(其中也有自定义布局))。同时,我想要抽屉的一个实例,可通过单个 Activity (即 MainActivity)访问。

我已经通过这些示例找到了类似的东西,但它们都使用新 Activity 来填充它们的内容,或者它们只是隐藏/显示不适合我的情况的工具栏内容,因为我已经为每个 fragment ,因此我无法找出布局 View 的顺序。我经历过的一些解决方案如下:

  1. Collapsing Toolbar only for one Fragment in Navigation View

  2. Coordinator Layout with Toolbar in Fragments or Activity

  3. Collapsing Toolbar and DrawerLayout

长话短说

对于一个 fragment ,我想要如下所示的工具栏

enter image description here

对于其他 fragment

enter image description here

最佳答案

编辑:

这不是一个干净的代码,但它是一个工作代码。我只是想给你看。您可以使用类似 BaseFragment 的东西并构建更好的代码。在 MainFragment 中,您可以使用抽屉导航,在 DetailFragment 中,您可以使用工具栏上的后退箭头。如果你愿意,你也可以在 DetailFragment 中使用导航 View 。您也可以在您喜欢的任何 fragment 中使用折叠工具栏。

主要 Activity

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)
}

}


}

主要 fragment

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)
}


}

细节 fragment

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)

}

}

主要 Activity 布局

<?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>

主要 fragment 布局

<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布局

<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
}

你可以看看这段代码。我在每个 fragment 中将工具栏设置为操作栏,让它们扩展自己的菜单并与 Activity 抽屉状态同步。如果这有帮助,请告诉我。

关于android - 在不同的 fragment 中用折叠工具栏替换工具栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49249802/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com