我正在尝试基于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); }}
宠物
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”).
总结以上是内存溢出为你收集整理的java-在Retrofit调用后更改折叠的工具栏标题全部内容,希望文章能够帮你解决java-在Retrofit调用后更改折叠的工具栏标题所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)