我需要知道如何在使用Firebase滚动时动态加载数据。我想从Firebase获得最高限额50的记录。达到此限制后,我将下载下50个数据。
MarkerActivity.java
public class MarkerActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
private LinearLayoutManager layoutManager;
private ViewSwitcher mViewSwitcher;
private MarkerAdapter mMarkerAdapter;
private RecyclerView recyclerView
private int page_number = 1;
private int item_count = 10;
private boolean isLoading = true;
private int pastVisibleItems, visibleItemCount,totallItemCount,previous_totall = 0;
private int view_threshold = 10;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_markers);
recyclerView = findViewById(R.id.recycler_view);
layoutManager = new LinearLayoutManager(this);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(layoutManager);
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
visibleItemCount = layoutManager.getChildCount();
totallItemCount = layoutManager.getItemCount();
pastVisibleItems = layoutManager.findLastVisibleItemPosition();
if (dy>0)
{
if (isLoading)
{
if (totallItemCount>previous_totall)
{
isLoading = false;
previous_totall = totallItemCount;
}
}
if (!isLoading&&(totallItemCount-visibleItemCount)<=(pastVisibleItems=view_threshold))
{
page_number++;
isLoading = true;
}}
});
}
}
你能帮帮我吗?
答案 0 :(得分:0)
使用以下代码,您可以在firebase中实现分页: 设置firebase限制查询:
Query first = db.collection("cities")
.orderBy("population")
.limit(25);
first.get()
.addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot documentSnapshots) {
// ...
// Get the last visible document
DocumentSnapshot lastVisible = documentSnapshots.getDocuments()
.get(documentSnapshots.size() -1);
// Construct a new query starting at this document,
// get the next 25 cities.
Query next = db.collection("cities")
.orderBy("population")
.startAfter(lastVisible)
.limit(25);
// Use the query for pagination
// ...
}
});
要获得更多知识,您需要查看链接: Check this link for pagination in firebase
答案 1 :(得分:0)
Query query = mDatabaseReference.orderByKey().limitToLast(50);
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
boolean isFirst = true;
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
if (isFirst) {
isFirst = false;
oldPostId = postSnapshot.getKey();
}
Post post = postSnapshot.getValue(Post.class);
if (post != null)
mPostRecyclerAdapter.addPost(new Post(post.getUserName(), post.getPost(), post.getPostOwnerUuid(), postSnapshot.getKey(), post.getTotalLikes(), post.getPostTime(), post.getCategoryName()));
}
//set your adapter here after adding item to recycler view
}
现在使用recyclerview的OnscrollListener。
mRecyclerView.addOnScrollListener(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);
currentVisibleItem = linearLayoutManager.getChildCount();
totalItem = linearLayoutManager.getItemCount();
scrolledItem = linearLayoutManager.findFirstVisibleItemPosition();
if (isScrolling && (currentVisibleItem + scrolledItem == totalItem) && !isEndChildResults && oldPostId != null) {
scrollCount++;
showInterstitial(scrollCount);
isScrolling = false;
mProgressBarScroll.setVisibility(View.VISIBLE);
Query query = mDatabaseReference.orderByKey().endAt(oldPostId).limitToLast(50);
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
int count = 0;
mPostRecyclerAdapter.clearReversePostList();
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
count++;
if (dataSnapshot.getChildrenCount() < 50) {
isEndChildResults = true;
} else {
if (count == 1) {
oldPostId = postSnapshot.getKey();
}
}
if (count != 50) {
Post post = postSnapshot.getValue(Post.class);
if (post != null)
mPostRecyclerAdapter.addPost(new Post(post.getUserName(), post.getPost(), post.getPostOwnerUuid(), postSnapshot.getKey(), post.getTotalLikes(), post.getPostTime(), post.getCategoryName()));
mPostRecyclerAdapter.notifyDataSetChanged();
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
if (isEndChildResults && (firstVisibleItem < scrolledItem) && !isSnackBarShown) {
snackbar = Snackbar.make(coordinatorLayout, R.string.no_more_post_available, Snackbar.LENGTH_INDEFINITE)
.setAction(getString(R.string.ok), new View.OnClickListener() {
@Override
public void onClick(View view) {
snackbar.dismiss();
}
});
snackbar.show();
isSnackBarShown = true;
}
}
});
答案 2 :(得分:0)
这是从我身边完成的。你可以试试这个。
像这样添加监听器赞:
Query newItems = FirebaseDatabase.getInstance().getReference();
newItems.getRef().child(“Child")
.child(“Child")
.getRef().orderByChild(Constants.ARG_CONV_TIMESTEMP)
.limitToLast(10).addChildEventListener(childEventListener);
观察员:
private ChildEventListener childEventListener = new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
ChatBean chat = dataSnapshot.getValue(ChatBean.class);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
};
删除侦听器,如:
newItems.getRef().child(“Child")
.child(“Child")
.getRef().removeEventListener(childEventListener);