Replace toolbar with collapsible toolbar in different fragments

I might be missing something very simple here, but I could definitely use some help.

I have an activity in which I want to load different fragments with different content and a different toolbar style (i.e. toolbars with a custom view and CollapsingToolbarLayout (this also has its own layout). At the same time I want to have one instance A box accessible through one activity (i.e. MainActivity).

I looked through examples to find something similar, but they all use new actions to fill their contents, or just hide / show the contents of the toolbar, which is not suitable in my case, since I have custom toolbar views for each fragment so I can't find out what the layout order should be. Some of the solutions I went through are similar to:

TL DR

For one snippet I want the Toolbar below

enter image description here

And for another fragment

enter image description here

+4
source share
1 answer

EDIT:

, . . - 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

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

. . , .

+1

Source: https://habr.com/ru/post/1694800/


All Articles