改造调用后更改折叠的工具栏标题

时间:2018-08-14 18:38:21

标签: java android retrofit2

我正在尝试根据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()是JSON文件中的字符串。我也测试了硬编码的字符串(防止mProcess.getName()错误的值),但是没有效果。 在通话之前,就像魅力一样。

获取回调后是否有任何方法可以更新工具栏标题?

更新

感谢您的帮助。现在,我发现了真正的问题:折叠工具栏。 我做了一个空白的应用程序来复制错误,并且代码可以正常工作,但是当我使用折叠工具栏运行相同的代码时……Bingo!下面的完整代码:

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>

解决方案

发现真正的问题后,我在这里搜索并找到了解决方案: https://github.com/henrytao-me/smooth-app-bar-layout/issues/32

设置CollapsingToolbarLayout小部件ID(collapsing_toolbar)和活动代码:

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

名称现在更改为:D

1 个答案:

答案 0 :(得分:3)

我在翻新回调中尝试过getSupportActionBar().setTitle("hello");,对我来说效果很好。

您的问题是每次onFailure调用时都要进行翻新。因此不会调用getSupportActionBar().setTitle("hello")

也在onFailure中尝试getSupportActionBar().setTitle("hello")