我有一个recyclerview,我想在回收器填充来自json的数据时显示“loading ...”消息或旋转圈。 这是我的回收商的代码:
TcpListener
这是适配器:
arrayList = new ArrayList<>();
adapter = new ListAdapterGrupat2(this, arrayList, Comanda.class);
list.setAdapter(adapter);
final Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://mysite/")
.addConverterFactory(GsonConverterFactory.create())
.build();
service = retrofit.create(API.class);
if (myuser.equals("1")) {
service.getComenzileMele(user).enqueue(listener);
} else {
service.getComenziGrupat().enqueue(listener);
}
}
Callback<List<CustomObject>> listener = new Callback<List<CustomObject>>() {
@Override
public void onResponse(Call<List<CustomObject>> call, Response<List<CustomObject>> response) {
arrayList.clear();
arrayList.addAll(response.body());
adapter.notifyDataSetChanged();
}
@Override
public void onFailure(Call<List<CustomObject>> call, Throwable t) {
Toast.makeText(ComenziGrupatActivity.this, "ERROR", Toast.LENGTH_SHORT).show();
}
};
答案 0 :(得分:0)
将ProgressBar添加到您的班级和布局(靠近您的recicleView)。如果你想要文字:添加LinearLayout并放置ProgressBar
和TextView
(触发linearLayout上的可见性):
ProgressBar bar;
当您发出请求并隐藏recicleView时显示progressBar:
bar.setVisibility(VISIBLE);
recicleView.setVisibility(GONE);
service.getComenzileMele(user).enqueue(listener);
请求完成后隐藏progressBar并恢复recicleView:
Callback<List<CustomObject>> listener = new Callback<List<CustomObject>>() {
@Override
public void onResponse(Call<List<CustomObject>> call, Response<List<CustomObject>> response) {
bar.setVisibility(GONE);
recicleView.setVisibility(VISIBLE);
}
@Override
public void onFailure(Call<List<CustomObject>> call, Throwable t) {
bar.setVisibility(GONE);
recicleView.setVisibility(VISIBLE);
}
};