我正在尝试用Gridlayout
数据库实现firestore
并使其滚动
5个项目,然后再添加5个项目。
我从How to paginate Firestore with Android?尝试了这段代码但不起作用,它适用于linearlayout,但我想要gridlayout:
private DocumentSnapshot lastVisible;
private boolean isScrolling;
private boolean isLastItemReached;
private int limit = 6;
//________RV________
MainCategoriesRV = view.findViewById(R.id.MainCategoriesRV);
MainCategoriesRV.setHasFixedSize(true);
gLayoutManager=new GridLayoutManager(MainCategoriesRV.getContext(),2);
MainCategoriesRV.setLayoutManager(gLayoutManager);
//______PROGRESS BAR__________
MainActivityProgress = view.findViewById(R.id.MainActivityProgress);
final CollectionReference ref = firebaseFirestore
.collection("MainCategories");
Query firstQuery = ref
.orderBy("Category_Name", Query.Direction.ASCENDING).limit(5);
firstQuery.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if ( task.isSuccessful() ) {
final List<mainCategroies> list = new ArrayList<>();
for (DocumentSnapshot document : task.getResult()) {
mainCategroies maincategroies = document.toObject(mainCategroies.class);
// assert maincategroies != null;
// maincategroies.setId(document.getId());
list.add(maincategroies);
}
final CategoriesAdapter categoriesAdapter = new CategoriesAdapter(list);
MainCategoriesRV.setAdapter(categoriesAdapter);
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(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
int firstVisibleItemPosition = gLayoutManager.findFirstVisibleItemPosition();
int visibleItemCount = gLayoutManager.getChildCount();
int totalItemCount = gLayoutManager.getItemCount();
if ( isScrolling && (firstVisibleItemPosition+visibleItemCount== totalItemCount) && !isLastItemReached ) {
isScrolling = false;
Query nextQuery = ref.orderBy("Category_Name", Query.Direction.ASCENDING)
.startAfter(lastVisible)
.limit(limit);
nextQuery.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
for (DocumentSnapshot d : task.getResult()) {
mainCategroies maincategroies = d.toObject(mainCategroies.class);
list.add(maincategroies);
}
categoriesAdapter.notifyDataSetChanged();
for (int i = 0; i < task.getResult().size(); i++) {
lastVisible = task.getResult().getDocuments().get(task.getResult().size() - 1);
}
Toast.makeText(getContext(), "please wait,... ", Toast.LENGTH_SHORT).show();
if ( task.getResult().size() < limit ) {
isLastItemReached = true;
}
}
});
}
}
};
MainCategoriesRV.addOnScrollListener(onScrollListener);
}
}
});
这是我的xml文件
<RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/MainCategoriesRV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
但是不起作用。我搜索了很多,但是没有明显的答案。
问题在于,当我尝试滚动时,仅加载第一个5个项目,而不加载接下来的5个项目。 那我该如何解决这个问题呢?