У меня есть Recyclerview, который загружает бесконечный контент в одном направлении (прокрутка вниз)
Это работает нормально, но сейчас я пытаюсь загрузить бесконечный контент также в другом направлении (прокрутка вверх)
Мой подход был такой:
Фрагмент:
public class MonthlyFragment extends Fragment {
List<DayObject> dayObjects = new ArrayList<DayObject>();
List<DayObject> beforeDayObjects = new ArrayList<DayObject>();
RecyclerView recyclerView;
ViewModel viewModel;
CustomAdapter = new CustomAdapter();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_layout, container, false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
recyclerView = getActivity().findViewById(R.id.recyclerview);
GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 7);
viewModel = ViewModelProviders.of(getActivity()).get(ViewModel.class);
recyclerView.setLayoutManager(gridLayoutManager);
recyclerView.setHasFixedSize(false);
recyclerView.setAdapter(adapter);
scrollListener = new EndlessRecyclerViewScrollListener(gridLayoutManager) {
@Override
public void onLoadMore(int page, int totalItemsCount, RecyclerView view) {
generateData()
}
@Override
public void onLoadMoreBeginning() {
generateDataBefore();
}
};
recyclerView.addOnScrollListener(scrollListener);
}
public void generateData(){
dayObjects = ...; // generating data
adapter.submitList(dayObjects);
adapter.notifyDataSetChanged();
//working as expected
}
public void generateDataBefore(){
beforeDayObjects = ...; //generating data
dayObjects = Stream.concat(beforeDayObjects.stream(), dayObjects.stream())
.collect(Collectors.toList());
beforeDayObjects.removeAll(beforeDayObjects);
adapter.submitList(dayObjects);
adapter.notifyDataSetChanged();
//not working
}
}
EndlessRecyclerViewScrollListener:
public abstract class EndlessRecyclerViewScrollListener extends RecyclerView.OnScrollListener {
private int visibleThreshold = 7;
private int currentPage = 0;
private int previousTotalItemCount = 0;
private boolean loading = true;
private int startingPageIndex = 0;
RecyclerView.LayoutManager mLayoutManager;
public EndlessRecyclerViewScrollListener(LinearLayoutManager layoutManager) {
this.mLayoutManager = layoutManager;
}
public EndlessRecyclerViewScrollListener(GridLayoutManager layoutManager) {
this.mLayoutManager = layoutManager;
visibleThreshold = visibleThreshold * layoutManager.getSpanCount();
}
public EndlessRecyclerViewScrollListener(StaggeredGridLayoutManager layoutManager) {
this.mLayoutManager = layoutManager;
visibleThreshold = visibleThreshold * layoutManager.getSpanCount();
}
public int getLastVisibleItem(int[] lastVisibleItemPositions) {
int maxSize = 0;
for (int i = 0; i < lastVisibleItemPositions.length; i++) {
if (i == 0) {
maxSize = lastVisibleItemPositions[i];
}
else if (lastVisibleItemPositions[i] > maxSize) {
maxSize = lastVisibleItemPositions[i];
}
}
return maxSize;
}
@Override
public void onScrolled(RecyclerView view, int dx, int dy) {
if (loading && (totalItemCount > previousTotalItemCount)) {
loading = false;
previousTotalItemCount = totalItemCount;
}
if (!loading && (lastVisibleItemPosition + visibleThreshold) > totalItemCount) {
currentPage++;
onLoadMore(currentPage, totalItemCount, view);
loading = true;
}
if (!loading && (lastVisibleItemPosition - visibleThreshold) < 7) {
onLoadMoreBeginning();
loading = true;
}
}
public void resetState() {
this.currentPage = this.startingPageIndex;
this.previousTotalItemCount = 0;
this.loading = true;
}
public abstract void onLoadMore(int page, int totalItemsCount, RecyclerView view);
public abstract void onLoadMoreBeginning();
}
Когда я пробую этот код, я всегда получаю: FATAL EXCEPTION: pool- 1-thread-1
Кто-нибудь знает решение для этого?