我在使用recyclerview和Gridview工作时遇到困难。问题是当我的应用程序成功加载下一页(下一个数据(时,回收器视图总是回到顶部。
我希望回收器视图从最后一个索引开始。
示例:第一页加载 10 个项目,成功借出下一页后,回收器视图从项目 8 开始滚动。
为了解决这个问题,我已经尝试了StackOverflow上的所有解决方案,但仍然一无所获。
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (newState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
isScrolling = true;
}
}
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
currentItems = manager.getChildCount();
totalItems = manager.getItemCount();
scrolloutItems = manager.findFirstVisibleItemPosition();
if (isScrolling && (currentItems + scrolloutItems == totalItems)) {
isScrolling = false;
getdata(pagetoken);
//getdata(pagetoken);
Toast.makeText(MainActivity.this, "Scrolling", Toast.LENGTH_SHORT).show();
}
}
});
}
public void getdata() {
progress.setVisibility(View.VISIBLE);
ApiClient.getApis().getblogdata(Constants.key).enqueue(new Callback<MyModel>() {
@Override
public void onResponse(Call<MyModel> call, Response<MyModel> response) {
if (response.isSuccessful()) {
assert response.body() != null;
arraylist.addAll(response.body().items);
}
if (arraylist != null) {
adapter = new Data_Adapter(MainActivity.this, arraylist);
recyclerView.setAdapter(adapter);
}
pagetoken = response.body().nextPageToken;
swiprefresh.setRefreshing(false);
progress.setVisibility(View.GONE);
}
@Override
public void onFailure(Call<MyModel> call, Throwable t) {
Toast.makeText(MainActivity.this, "t t Connection Error : n Please Check Your Internet Connection", Toast.LENGTH_SHORT).show();
}
});
}
public void getdata(String pageToken) {
ApiClient.getApis().getblogdata(Constants.key, pageToken).enqueue(new Callback<MyModel>() {
@Override
public void onResponse(Call<MyModel> call, Response<MyModel> response) {
if (response.isSuccessful()) {
assert response.body() != null;
arraylist.addAll(response.body().items);
}
if (arraylist != null) {
adapter = new Data_Adapter(MainActivity.this, arraylist);
recyclerView.setAdapter(adapter);
}
if (response.body().nextPageToken != null) {
pagetoken = response.body().nextPageToken;
}
Log.d("retro", "" + arraylist.size());
swiprefresh.setRefreshing(false);
}
@Override
public void onFailure(Call<MyModel> call, Throwable t) {
Toast.makeText(MainActivity.this, "Connection Error : n Please Check Your Internet Connection", Toast.LENGTH_SHORT).show();
}
});
}
-
问题 :- 回收器视图在添加第二页数据后进入顶部位置
-
原因 :- 您一直在制作适配器的新实例
在您的情况下,适配器是Data_Adapter并且您正在使用
adapter = new Data_Adapter(MainActivity.this, arraylist);
-
解决方案 :- 只制作一个适配器实例
-
示例:- 回收器视图在添加第二页数据后转到顶部
-
创建数组列表:-
private ArrayList<String> catList = new ArrayList<>();
-
on Create [ 初始化适配器并将其分配给回收视图]
if (arraylist != null) { adapter = new Data_Adapter(MainActivity.this, arraylist); recyclerView.setAdapter(adapter); }
-
在 getdata 方法 [ 在响应中通知适配器 ] :-
if (arraylist != null && arraylist.size() > 0) { // to notify adapter adapter.notifyItemInserted(adapter.getItemCount()); }