从早上搜索此错误但没有任何效果。问题是我已经实现了Endless recyclerview,它从网站的不同页面获取JSON数据,但这仅适用于第二页之后的第二页,它不会提取任何数据。
参见onLoadMore函数:
adapter.setLoadMore(new ILoadMore() {
@Override
public void loadMore() {
if (postItems.size() <= count) {
postItems.add(null);
adapter.notifyItemInserted(postItems.size() - 1);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
postItems.remove(postItems.size() - 1);
counter += 1;
Log.d("HAHAHAHA", "onScrolled: End " + counter);
if (counter <= pages) {
Log.d("HAHAHAHA", "onScrolled: Counter: " + counter);
getData(counter);
//Toast.makeText(getContext(), "Loading More", Toast.LENGTH_SHORT).show();
}
}
}, 2000);
adapter.setLoaded();
} else {
Toast.makeText(getContext(), "Done", Toast.LENGTH_SHORT).show();
}
}
});
} catch (JSONException e) {
e.printStackTrace();
}
适配器代码:
public AllAdapter(RecyclerView recyclerView, Activity activity, List<PostItem> postItemList, Controller controller) {
this.activity = activity;
this.postItemList = postItemList;
this.controller = controller;
final LinearLayoutManager manager = (LinearLayoutManager) recyclerView.getLayoutManager();
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
totalItemCount = manager.getItemCount();
lastVisibleItem = manager.findLastVisibleItemPosition();
if(!isLoading && totalItemCount <= (lastVisibleItem + visibleThreshold)) {
Log.d("HAHAHAHA", "onScrolled: End");
Log.d("HAHAHAHA", "onScrolled: End" + loadMore);
if (loadMore != null)
Log.d("HAHAHAHA", "Inside if");
loadMore.loadMore();
isLoading = true;
}
}
});
}
@Override
public int getItemViewType(int position) {
return postItemList.get(position) == null? VIEW_TYPE_LOAD: VIEW_TYPE_ITEM;
}
public void setLoadMore(ILoadMore loading) {
Log.d("HAHAHAHA", "onScrolled: End Inside setLoadMore");
this.loadMore = loading;
}
public void setLoaded() {
Log.d("HAHAHAHA", "onScrolled: End" + "Inside SetLoaded");
isLoading = false;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == VIEW_TYPE_ITEM) {
return new ItemViewHolder(LayoutInflater.from(activity)
.inflate(R.layout.allpost_item, parent, false));
}
else if (viewType == VIEW_TYPE_LOAD) {
return new LoadingViewHolder(LayoutInflater.from(parent.getContext())
.inflate(R.layout.progressbar, parent, false));
}
return null;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
if (holder instanceof ItemViewHolder) {
ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
Typeface face = Typeface.createFromAsset(activity.getAssets(), "googlesans.ttf");
PostItem postItem = postItemList.get(position);
itemViewHolder.all_title.setText(Html.fromHtml(postItem.getTitle().toString()));
itemViewHolder.date.setText(Html.fromHtml(postItem.getDate().toString()));
itemViewHolder.category.setText(Html.fromHtml(postItem.getCategory().toString()));
itemViewHolder.all_title.setTypeface(face);
Glide.with(activity).load(postItem.getThumbnail()).into(itemViewHolder.thumbnail);
} else if (holder instanceof LoadingViewHolder) {
LoadingViewHolder loadingViewHolder = (LoadingViewHolder) holder;
loadingViewHolder.progressBar.setIndeterminate(true);
}
}
@Override
public int getItemCount() {
return postItemList.size();
}
当我尝试调试代码时,adapter.setLoadMore()方法只被调用一次,即之后的第二页甚至不调用它。任何人都可以帮助我吗?