[英]Change collapsing toolbar title after Retrofit call
我正在嘗試根據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()
錯誤的值)但沒有效果。 就在電話工作之前就像一個魅力。
有沒有辦法在獲得回調后更新工具欄標題?
謝謝您的幫助。 現在我發現了真正的問題:折疊工具欄。 我做了一個空白的應用程序來復制錯誤和代碼工作,但當我運行與崩潰工具欄相同的代碼...賓果! 完整的代碼如下:
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
我試過getSupportActionBar().setTitle("hello");
內部改裝回調它對我來說很好。
你的問題是,每次onFailure調用都會在改裝調用中調用。 因此不調用getSupportActionBar().setTitle("hello")
。
在onFailure中嘗試getSupportActionBar().setTitle("hello")
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.