Я следовал учебному пособию: как создать RecyclerView
с Firestore Pagination , созданным членом StackOverFlow, Алекс Мамо (большое спасибо) на YouTube.
Моя проблема в том, что когда я прокручиваю до последнего элемента и изменяю фрагмент ... и когда я возвращаюсь, мой RecyclerView
больше не загружает все данные; RecyclerView
загружает первый запрос и достигает значения lastItemVisible
.
Большое спасибо заранее
Вот код, который я использую:
**Code for MainActivity:**
private void setFragment(Fragment fragment) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_frame_layout, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
Код MyRecyclerAdapter:
onBindViewHolder
tournamentViewHolder.view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "onClick: title: " + list.get(position).getTitle());
FragmentHoles fragmentHoles = new FragmentHoles();
Bundle bundle = new Bundle();
bundle.putString("title", list.get(position).getTitle());
fragmentHoles.setArguments(bundle);
AppCompatActivity activity = (AppCompatActivity) v.getContext();
activity.getSupportFragmentManager()
.beginTransaction()
.replace(R.id.main_frame_layout, fragmentHoles)
.addToBackStack(null).commit();
}
});
Фрагмент кода -> setRecyclerView
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
firstQuery.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
final List<Tournament> list = new ArrayList<>();
for (DocumentSnapshot document : task.getResult()) {
Tournament tournamentModel = document.toObject(Tournament.class);
list.add(tournamentModel);
}
tournamentAdapter = new TournamentAdapter(list);
recyclerView.setAdapter(tournamentAdapter);
lastVisible = task.getResult().getDocuments().get(task.getResult().size() - 1);
RecyclerView.OnScrollListener onScrollListener = new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(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);
LinearLayoutManager linearLayoutManager = ((LinearLayoutManager) recyclerView.getLayoutManager());
int firstVisibleItemPosition = linearLayoutManager.findFirstVisibleItemPosition();
int visibleItemCount = linearLayoutManager.getChildCount();
int totalItemCount = linearLayoutManager.getItemCount();
if (isScrolling && (firstVisibleItemPosition + visibleItemCount == totalItemCount) && !isLastItemReached) {
isScrolling = false;
Query nextQuery = productsRef.orderBy("id", Query.Direction.DESCENDING).startAfter(lastVisible).limit(limit);
nextQuery.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> t) {
if (t.isSuccessful()) {
for (DocumentSnapshot d : t.getResult()) {
Tournament productModel = d.toObject(Tournament.class);
list.add(productModel);
}
tournamentAdapter.notifyDataSetChanged();
lastVisible = t.getResult().getDocuments().get(t.getResult().size() - 1);
if (t.getResult().size() < limit) {
Log.d(TAG, "onComplete: size: " + t.getResult().size());
Log.d(TAG, "onComplete: reached: ");
isLastItemReached = true;
}
}
}
});
}
}
};
recyclerView.addOnScrollListener(onScrollListener);
}
}
});