我有问题,为什么没有人帮助我?
在我的应用程序中,我想要加载数据并使用延迟加载程序作为recyclerView
。
我在下面写下了加载数据的代码:
private void loadNews() {
NewsSendData sendData = new NewsSendData();
sendData.setKey("");
newsPageImageProgress.show();
InterfaceApi api = ApiClient.getClient().create(InterfaceApi.class);
Call<NewsResponse> call = api.getMainNews(sendData);
call.enqueue(new Callback<NewsResponse>() {
@Override
public void onResponse(Call<NewsResponse> call, Response<NewsResponse> response) {
if (response.body().getData().getNewsDatum() != null) {
newModel.clear();
newModel.addAll(response.body().getData().getNewsDatum());
newsAdapter.notifyDataSetChanged();
newsPageRecyclerView.setAdapter(newsAdapter);
}
}
@Override
public void onFailure(Call<NewsResponse> call, Throwable t) {
});
并在recyclerView
的延迟加载程序中编写以下代码:
newsPageRecyclerView.setOnScrollListener(new EndlessRecyclerOnScrollListener(layoutManager) {
@Override
public void onLoadMore(int current_page) {
NewsSendData sendData = new NewsSendData();
sendData.setKey("");
InterfaceApi api = ApiClient.getClient().create(InterfaceApi.class);
Call<NewsResponse> call = api.getMainNews(sendData);
call.enqueue(new Callback<NewsResponse>() {
@Override
public void onResponse(Call<NewsResponse> call, Response<NewsResponse> response) {
if (response.body().getData().getNewsDatum() != null) {
newsAdapter.addNewItem(response.body().getData().getNewsDatum());
newsPageLoadLay.setVisibility(View.GONE);
}
}
@Override
public void onFailure(Call<NewsResponse> call, Throwable t) {
}
});
}
});
我应该在我的应用程序中使用 searchView ,我希望当用户搜索文本时,删除以前的数据,只显示来自服务器的新数据(来自服务器的搜索结果)。
对于搜索结果,请使用以下代码:
newsSearchView.setOnQueryTextListener(new MaterialSearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(final String query) {
NewsSendData sendData = new NewsSendData();
sendData.setKey(query);
InterfaceApi api = ApiClient.getClient().create(InterfaceApi.class);
Call<NewsResponse> call = api.getMainNews(sendData);
call.enqueue(new Callback<NewsResponse>() {
@Override
public void onResponse(Call<NewsResponse> call, Response<NewsResponse> response) {
if (response.body().getData().getNewsDatum() != null) {
newModel.clear();
newModel.addAll(response.body().getData().getNewsDatum());
newsAdapter.notifyDataSetChanged();
newsPageRecyclerView.setAdapter(newsAdapter);
newsPageImageProgress.hide();
toolbarTitleTxt.setText(query);
}
}
@Override
public void onFailure(Call<NewsResponse> call, Throwable t) {
});
我在活动的 onCreate()上写了上面的代码
使用上述代码时,请向我显示来自服务器的大量数据。
例如:我在搜索文本时,在 PostMan 中显示2个数据,但在我的应用程序中显示数据的这2个数据顶部并显示此下面的其他数据2数据。
当删除延迟加载程序代码时,我的问题是修复并只显示这2个数据,但是当再次添加延迟加载程序时显示其他数据!!!
我该如何修复这个错误?请帮帮我
答案 0 :(得分:0)
将EndlessRecyclerOnScrollListener代码替换为以下代码。
public abstract class EndlessRecyclerOnScrollListener extends RecyclerView.OnScrollListener {
public static String TAG = EndlessRecyclerOnScrollListener.class.getSimpleName();
int firstVisibleItem, visibleItemCount, totalItemCount;
private int previousTotal = 0; // The total number of items in the dataset after the last load
private boolean loading = true; // True if we are still waiting for the last set of data to load.
private int visibleThreshold = 4; // The minimum amount of items to have below your current scroll position before loading more.
private int current_page = 1;
private StaggeredGridLayoutManager mGridLayoutManager;
public EndlessRecyclerOnScrollListener(StaggeredGridLayoutManager gridLayoutManager) {
this.mGridLayoutManager = gridLayoutManager;
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
visibleItemCount = recyclerView.getChildCount();
totalItemCount = mGridLayoutManager.getItemCount();
int[] items = new int[3];
mGridLayoutManager.findFirstVisibleItemPositions(items);
firstVisibleItem = items[0];
if (loading) {
if (totalItemCount > previousTotal) {
loading = false;
previousTotal = totalItemCount;
}
}
if (!loading && (totalItemCount - visibleItemCount)
<= (firstVisibleItem + visibleThreshold)) {
// End has been reached
// Do something
current_page++;
onLoadMore(current_page);
loading = true;
}
}
public abstract void onLoadMore(int current_page);
}