ActionBar SetTitle无法使用Collapsing ToolBar

时间:2017-03-07 20:14:20

标签: android android-fragments android-navigation-drawer android-collapsingtoolbarlayout

我有一个管理4个片段的Activity。我在活动中使用导航抽屉和折叠工具栏。当我切换片段时,我只是希望ActionBar标题成为我以编程方式设置它的内容,但由于某种原因它没有发生。

我知道它最初有效,因为在我的onCreate()方法中,我将标题设置为“Home”并显示正常。但是,当我开始导航时,代码不会执行。有谁知道为什么?

这是我的MainActivity XML:

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<include
    layout="@layout/app_bar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>


<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>

这是app_bar布局:

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:fitsSystemWindows="true"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/task_header"
            android:scaleType="centerCrop"
            android:fitsSystemWindows="true"
            app:layout_collapseMode="parallax"/>

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_height="?attr/actionBarSize"
            android:layout_width="match_parent" />

    </android.support.design.widget.CollapsingToolbarLayout>

</android.support.design.widget.AppBarLayout>

<FrameLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.markfeldman.tasktrack.MainActivity">
</FrameLayout>
</android.support.design.widget.CoordinatorLayout>

这是我的主要活动:

public class MainActivity extends AppCompatActivity implements Home
    .onFragmentClicked,NavigationView.OnNavigationItemSelectedListener {
private Fragment selectedFragment;
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle drawerToggle;
private String title;
private final String INSTANCE_STATE_SAVE_TITLE = "save_title";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    selectedFragment = new Home();
    if (savedInstanceState==null){
        title  = getString(R.string.fragment_home_title);
        getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.container,selectedFragment)
                .addToBackStack(title)//WORKS HERE
                .commit();
        if (getSupportActionBar()!=null){
            getSupportActionBar().setTitle(title);
        }
    }else{
        title = savedInstanceState.getString(INSTANCE_STATE_SAVE_TITLE);
        if (getSupportActionBar()!=null){
            getSupportActionBar().setTitle(title);
        }
    }
    title = getTitle().toString();
    drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
    drawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.drawer_open,R.string.drawer_close);
    drawerLayout.addDrawerListener(drawerToggle);
    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
    if (getSupportActionBar()!=null){
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
    }
    drawerToggle.syncState();
}

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    int id = item.getItemId();

    switch (id){
        case (R.id.home_title_nav_drawer) :{
            selectedFragment = new Home();
            title = getString(R.string.fragment_home_title);
            break;
        }
        case (R.id.cal_title_nav_drawer) :{
            selectedFragment = new Calendar();
            title = getString(R.string.fragment_cal_title);
            break;
        }
        case (R.id.contacts_title_nav_drawer) :{
            selectedFragment = new Contacts();
            title = getString(R.string.fragment_contacts_title);
            break;
        }
        case (R.id.tasks_title_nav_drawer) :{
            selectedFragment = new Tasks();
            title = getString(R.string.fragment_task_title);
            break;
        }
    }
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.replace(R.id.container,selectedFragment)
            .addToBackStack(title)
    if (getSupportActionBar()!=null){
        getSupportActionBar().setTitle(title);//NO LONGER WORKS
    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    // Sync the toggle state after onRestoreInstanceState has occurred.
    drawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    drawerToggle.onConfigurationChanged(newConfig);
}


@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putString(INSTANCE_STATE_SAVE_TITLE, title);
}

@Override
public void onBackPressed() {
    super.onBackPressed();
    int count = this.getSupportFragmentManager().getBackStackEntryCount();
    if(count==0){
        this.finish();
    }else if (count>0){
        title = getSupportFragmentManager().getBackStackEntryAt(count-1).getName();
        if (getSupportActionBar()!=null){
            getSupportActionBar().setTitle(title);//DOESN'T WORK
        }
    }
}

@Override
public void buttonClicked(View v) {
    int id = v.getId();
    switch (id){
        case (R.id.cal_button):{
            selectedFragment = new Calendar();
            title = getString(R.string.fragment_cal_title);
            break;
        }
        case (R.id.tasks_button):{
            selectedFragment = new Tasks();
            title = getString(R.string.fragment_task_title);
            break;
        }
        case (R.id.contacts_button):{
            selectedFragment = new Contacts();
            title = getString(R.string.fragment_contacts_title);
            break;
        }
    }
    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.container,selectedFragment)
            .addToBackStack(title)
            .commit();

    if (getSupportActionBar()!=null){
        getSupportActionBar().setTitle(title);//DOESN'T WORK
    }
}}
PS:正如一个单挑,Home.onFragmentClicked是我的一个Fragment的界面,它管理Button onClick并相应地替换Fragment。

0 个答案:

没有答案