简体   繁体   中英

Change collapsing toolbar title after Retrofit call

I'm trying to modify the toolbar title based on the Retrofit2 response, however there is no change.

getSupportActionBar().setTitle("here work");

final Call<Process> getProcess = WiimApi.getService(serverAddress).getProcess(id);

getProcess.enqueue(new Callback<Process>() {
    @Override
    public void onResponse(Call<Process> call, Response<Process> response) {
        mProcess = response.body();

        getSupportActionBar().setTitle(mProcess.getName()); // this not work
    }

    @Override
    public void onFailure(Call<Process> call, Throwable t) {
        // ... 
    }
});

mProcess.getName() is a string from JSON file. I have tested with a hardcoded string too (prevent mProcess.getName() wrong value) but no effect. Just before the call work like a charm.

Is there any way to update the toolbar title after getting the callback?

Update

Thanks for the help. Now I have found the real problem: Collapsing Toolbar. I did a blank application to replicate the error and the code works but when I run the same code with Collapsing toolbar ... Bingo! The complete code below:

MainActivity.java:

package com.example.retrofittitle;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        getData();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

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

        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void getData() {
        getSupportActionBar().setTitle("Here work ...");

        // https://raw.githubusercontent.com/LearnWebCode/json-example/master/
        final Call<Pet> getPet = MyApi.getService("https://raw.githubusercontent.com/LearnWebCode/json-example/master/").getPet();

        getPet.enqueue(new Callback<Pet>() {
            @Override
            public void onResponse(Call<Pet> call, Response<Pet> response) {
                Pet pet = response.body();

                getSupportActionBar().setTitle(pet.getName()); // Here not work
                TextView text = findViewById(R.id.log);
                text.setText("Done");
            }

            @Override
            public void onFailure(Call<Pet> call, Throwable t) {
                getSupportActionBar().setTitle("Failure Title");
            }
        });

    }
}

MyApi.java

package com.example.retrofittitle;

import retrofit2.Call;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.GET;

public class MyApi {
    public interface ApiService {
        @GET("pet-of-the-day.json")
        Call<Pet> getPet();
    }

    public static ApiService getService(String url) {
        retrofit2.Retrofit retrofit = new retrofit2.Retrofit.Builder()
                .baseUrl(url)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        return retrofit.create(ApiService.class);
    }
}

Pet.java

package com.example.retrofittitle;

public class Pet {
    private String name;
    private String species;
    private Integer age;
    private String photo;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSpecies() {
        return species;
    }

    public void setSpecies(String species) {
        this.species = species;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getPhoto() {
        return photo;
    }

    public void setPhoto(String photo) {
        this.photo = photo;
    }
}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/log"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        android:text="Requesting..."
        android:layout_gravity="center" />

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="180dp"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleGravity="top"
            app:expandedTitleMarginTop="?attr/actionBarSize"
            app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">

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

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

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

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

Solution

After discovery of the real problem I search and found solution here: https://github.com/henrytao-me/smooth-app-bar-layout/issues/32

Setting CollapsingToolbarLayout widget id (collapsing_toolbar) and code for activity:

CollapsingToolbarLayout mCollapsingToolbarLayout = findViewById(R.id.collapsing_toolbar);
mCollapsingToolbarLayout.setTitle(pet.getName());

The name is now changed :D

I have tried getSupportActionBar().setTitle("hello"); inside retrofit callback it is working fine for me.

Your problem is that on retrofit call everytime onFailure is called. Hence getSupportActionBar().setTitle("hello") is not called.

try getSupportActionBar().setTitle("hello") in onFailure also.

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