简体   繁体   中英

Single Activity and Multiple Fragment with different toolbar layouts

I have an app with Single Activity multiple Fragments architecture in my app. The problem is, there is one listing fragment with normal Toolbar, from here user can click and go to Details screen. Now in Details screen, I want the Activity to become Fullscreen with CoordinatorLayout and Collapsing Toolbar with StatusBar area covered in layout as transparent. And when user goes back to Listing screen. The Activity should disable fullscreen and again get the StatusBar color.

The problem here is I am setting the activity as fullscreen using following code:

window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

And when the user goes back, I am disabling the Fullscreen by the following code:

window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_VISIBLE

After disabling the fullscreen, in my previous fragment, I am getting UI bounds clipped off.

Thanks in advance.

Try this in your DetailsActivity -

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // remove title
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.main);
}

I would recommend using

android:theme="@style/AppTheme.NoActionBar" for your activity.

and in your XML activity build in custom toolbar

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar_top"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    android:background="@color/action_bar_bkgnd"
    app:theme="@style/ToolBarTheme" >


     <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Toolbar Title"
        android:layout_gravity="center"
        android:id="@+id/toolbar_title" />


    </android.support.v7.widget.Toolbar>

and in your activity class

Toolbar toolbarTop = (Toolbar) findViewById(R.id.toolbar_top);
TextView mTitle = (TextView) toolbarTop.findViewById(R.id.toolbar_title);

Now for your fragments , all you have to do is to show or hide this toolbar depends on the fragment you want to the same or another toolbar , And you can do that with OnResume and OnPause of the fragments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM