使用recyclerview和Gridview时遇到困难。问题是当我的应用程序成功加载下一页(下一个数据)时,recyclerview总是回到顶部。
我希望recyclerview从上一个索引开始。
示例:首页加载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();
}
});
}
答案 0 :(得分:1)
在这种情况下,适配器是Data_Adapter,而您正在使用
创建adapter = new Data_Adapter(MainActivity.this, arraylist);
示例:-添加第二页数据后,RecyclerView到达顶部
创建数组列表:-
private ArrayList<String> catList = new ArrayList<>();
在onCreate [上初始化适配器并将其分配给回收视图]
if (arraylist != null) {
adapter = new Data_Adapter(MainActivity.this, arraylist);
recyclerView.setAdapter(adapter);
}
在getdata方法中[在onResponse中以通知适配器]:-
if (arraylist != null && arraylist.size() > 0) {
// to notify adapter
adapter.notifyItemInserted(adapter.getItemCount());
}