我有一个RecyclerView,其中必须显示一组组。现在,进度条会显示很长时间,并且不会显示组。我正在使用改造来获取组。进度条加载很长时间,并且进度条的可见性消失了。没有显示任何数据。
这是Java文件
private void getAllGroups(int offset, int limit) {
if (offset == 0) {
relativeLayout.setVisibility(View.VISIBLE);
progressBar.setVisibility(View.VISIBLE);
} else {
loadLayout.setVisibility(View.VISIBLE);
}
Call<List<TrendingGroupsResponse>> allGroupsCall = retrofit.getAllMajorGroups(String.valueOf(offset), String.valueOf(limit));
Callback<List<TrendingGroupsResponse>> allGroupsCallBack = new Callback<List<TrendingGroupsResponse>>() {
@Override
public void onResponse(Call<List<TrendingGroupsResponse>> call, Response<List<TrendingGroupsResponse>> response) {
if (response.body() != null) {
Gson gson = new Gson();
Log.d(TAG, gson.toJson(response.body()));
for (TrendingGroupsResponse response1 : response.body())
allGroupsList.add(new SelectionGroupItem(response1));
if (response.body().size() < limit) {
reachedEnd = true;
}
loadmore = true;
if (offset == 0) {
allGroupsAdapter = new GroupSelectionTrendingAdapter(getApplicationContext(),
allGroupsList, GroupSelectionActivity.this, ALL_GROUPS_MODE);
allGroups.setAdapter(allGroupsAdapter);
} else if (allGroupsAdapter != null)
allGroupsAdapter.notifyDataSetChanged();
}
groupSelectLayout.setVisibility(View.VISIBLE);
mainlayout.setVisibility(View.VISIBLE);
relativeLayout.setVisibility(View.GONE);
progressBar.setVisibility(View.GONE);
loadLayout.setVisibility(View.GONE);
}
}
@Override
public void onFailure(Call<List<TrendingGroupsResponse>> call, Throwable t) {
View view = findViewById(R.id.root_view);
Snackbar snackbar = Snackbar.make(view, "Network Error", Snackbar.LENGTH_INDEFINITE)
.setAction("RELOAD", new View.OnClickListener() {
@Override
public void onClick(View view) {
getTrendingGroups();
getAllGroups(offset, limit);
}
});
snackbar.show();
}
};
allGroupsCall.enqueue(allGroupsCallBack);
}
如何解决此问题,以便加载和显示数据?
答案 0 :(得分:0)
也许您可以尝试在所有方法调用之前声明适配器(例如在onCreate()
的{{1}}方法上)
GroupSelectionActivity
然后,在allGroupsAdapter = new GroupSelectionTrendingAdapter(
getApplicationContext(),
new ArrayList<SelectionGroupItem>(),
GroupSelectionActivity.this,
ALL_GROUPS_MODE
);
// Considering allGroups is your RecyclerView
allGroups.setAdapter(allGroupsAdapter);
中公开方法GroupSelectionTrendingAdapter
updateData(List<SelectionGroupItem> items)
然后在您的回调中:
@UiThread
public void updateData(List<SelectionGroupItem> newItems) {
/**
* this.selectionGroupItems is a property of GroupSelectionTrendingAdapter
* that is used to display the data.
*
* It is init in the constructor, then it never changes
* and is never reset using the = operator
*/
this.selectionGroupItems.clear()
this.selectionGroupItems.addAll(newItems)
// On the UI thread, as it is updating UI items.
this.notifyDataSetChanged()
}
通过这种方式适配器永远不会改变,它是在屏幕寿命的一开始就设置的。 唯一改变的是您在其中显示的数据。
runOnUiThread
可以确保在显示线程上进行调用
另外,我不确定为什么将if (response.body() != null) {
// ...
for (TrendingGroupsResponse response1 : response.body())
allGroupsList.add(new SelectionGroupItem(response1));
// not needed anymore if (offset == 0) {
// Call is wrapped to run on the UI thread
runOnUiThread(new Runnable() {
@Override
public void run() {
allGroupsAdapter.updateData(allGroupsList);
}
});
// ...
}
赋予适配器,因为getApplicationContext()
也是上下文。
请提醒不要将活动的引用保留在可以破坏的视图内,以免保留引用(内存泄漏)