简体   繁体   English

在Retrofit调用后更改折叠工具栏标题

[英]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. 我正在尝试根据Retrofit2响应修改工具栏标题,但是没有变化。

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. mProcess.getName()是来自JSON文件的字符串。 I have tested with a hardcoded string too (prevent mProcess.getName() wrong value) but no effect. 我也测试了一个硬编码字符串(防止mProcess.getName()错误的值)但没有效果。 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: 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 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 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: 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 在发现真正的问题之后,我在这里搜索并找到解决方案: https//github.com/henrytao-me/smooth-app-bar-layout/issues/32

Setting CollapsingToolbarLayout widget id (collapsing_toolbar) and code for activity: 设置CollapsingToolbarLayout小部件ID(collapsing_toolbar)和活动代码:

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

The name is now changed :D 名称现在已更改:D

I have tried getSupportActionBar().setTitle("hello"); 我试过getSupportActionBar().setTitle("hello"); inside retrofit callback it is working fine for me. 内部改装回调它对我来说很好。

Your problem is that on retrofit call everytime onFailure is called. 你的问题是,每次onFailure调用都会在改装调用中调用。 Hence getSupportActionBar().setTitle("hello") is not called. 因此不调用getSupportActionBar().setTitle("hello")

try getSupportActionBar().setTitle("hello") in onFailure also. 在onFailure中尝试getSupportActionBar().setTitle("hello")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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