gpt4 book ai didi

java - 当用户滚动到 RecyclerView 底部时,使用 ScrollListener 从 Firebase 实时数据库提取数据时出现问题

转载 作者:行者123 更新时间:2023-12-02 05:03:04 25 4
gpt4 key购买 nike

我有一个应用程序,它从 Firebase 实时数据库中提取对象数据,将其添加到 ArrayList,然后将其添加到 RecyclerView。我在 RecyclerView 上使用了 onScrollListener,这样当用户滚动到 RecyclerView 底部时,就会调用一个函数从 Firebase 中提取更多数据,将其添加到 ArrayList 并通知 RecyclerViewAdapter 数据集更改。

当我运行我的应用程序时,它会在我的 RecyclerView 中显示初始数据拉取,然后立即开始调用该函数以连续从我的数据库中获取更多数据,直到没有更多数据可供拉取并且我的 RecyclerView 随之更新数据;无论我是否滚动(以任何方向/任何幅度),都会发生这种情况。

理想情况下,我希望应用程序仅在用户滚动到 RecyclerView 底部时才提取数据,并且一次提取一个数据。我很难调试这个问题,因为逻辑似乎是正确的;任何帮助将不胜感激。

相关全局变量

RecyclerView rvVertical;
RecyclerViewAdapter rvaVertical;
long mTotalChildren = 0;
int mLastKey = 0;

ChildEventListener childEventListenerMain, childEventListenerPager;
boolean Loading = false;

private ArrayList<BlogObject> blogArray = new ArrayList<>();
private ArrayList<BlogObject> tempList = new ArrayList<>();

查看已加载

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.socialmediatab, container, false);

rvVertical = (RecyclerView) view.findViewById(R.id.bloglistRecyclerView);
Loading = true;
Log.d(TAG, "onCreateView - Loading = " + Loading);
firstEventListener();
regAdapterVertical();

addOnScrollListener();
return view;
}

第一个被调用的函数。初始化从 Firebase 中第一次提取的数据

private void firstEventListener() {

FirebaseDatabase.getInstance().getReference().child("blogs").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
mTotalChildren = dataSnapshot.getChildrenCount();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) { }
});

final Query BlogQuery = FirebaseDatabase.getInstance().getReference().child("blogs").orderByChild("createdAt").limitToLast(3);

childEventListenerMain = new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot ds, String s) {
Loading = true;
Log.d(TAG, "First get blogs instance - Loading = " + Loading);
BlogObject addblog = ds.getValue(BlogObject.class);

if (addblog != null) {
BlogObject addBlog = new BlogObject(ds.getValue(BlogObject.class).getImage(), ds.getValue(BlogObject.class).getTitle(), ds.getValue(BlogObject.class).getTimeStamp(), ds.getValue(BlogObject.class).getCreatedAt());
blogArray.add(addBlog);
rvaVertical.notifyDataSetChanged();
mLastKey = blogArray.get(0).getCreatedAt();
rvVertical.scrollToPosition(blogArray.size() - 1);
}
}
@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) { }
};

BlogQuery.addChildEventListener(childEventListenerMain);
ValueEventListener BlogChildSingleListener = new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Loading = false;
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) { }
};

BlogQuery.addListenerForSingleValueEvent(BlogChildSingleListener);
}

将滚动监听器添加到我的回收器 View 中。理想情况下,当且仅当用户滚动到 RecyclerView 底部时,才应该从 Firebase 获取更多数据

private void addOnScrollListener() {
rvVertical.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}

@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
Log.d(TAG, "Just Entered onScrolled and Loading = " + Loading);
super.onScrolled(recyclerView, dx, dy);

if(!rvVertical.canScrollVertically(1)) {
LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
if (blogArray.size() >= 3 && !Loading && linearLayoutManager.findLastCompletelyVisibleItemPosition() == blogArray.size() - 1 && blogArray.size() < mTotalChildren) {
Loading = true;
getMoreBlogs();
Log.d(TAG, "Right After getMoreBlogs gets called. Loading = " + Loading);
}
}
}
});
}

通过第一次提取数据来初始化适配器

private void regAdapterVertical() {
LinearSnapHelper linearSnapHelper = new SnapHelperOneByOne();
linearSnapHelper.attachToRecyclerView(rvVertical);
rvaVertical = new RecyclerViewAdapter(blogArray);
rvVertical.setAdapter(rvaVertical);
rvVertical.setLayoutManager((RecyclerView.LayoutManager) new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
}

这是我从 Firebase 提取更多数据的函数。它在我的滚动监听器中被调用

public void getMoreBlogs() {
tempList.clear();

Query getMoreQuery = FirebaseDatabase.getInstance().getReference().child("blogs").orderByChild("createdAt").endAt(mLastKey).limitToLast(3);
childEventListenerPager = new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot ds, String s) {
Loading = true;
BlogObject blogObject = ds.getValue(BlogObject.class);
if (blogObject != null) {
BlogObject addBlog = new BlogObject(ds.getValue(BlogObject.class).getImage(), ds.getValue(BlogObject.class).getTitle(), ds.getValue(BlogObject.class).getTimeStamp(), ds.getValue(BlogObject.class).getCreatedAt());
tempList.add(addBlog);
}
}

@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) { }
};

getMoreQuery.addChildEventListener(childEventListenerPager);

ValueEventListener addtoAdapter = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
tempList.remove(tempList.size() - 1);
blogArray.addAll(0, tempList);
mLastKey = blogArray.get(0).getCreatedAt();
rvaVertical.notifyDataSetChanged();
Loading = false;
}

@Override
public void onCancelled(DatabaseError databaseError) {

}
};

getMoreQuery.addListenerForSingleValueEvent(addtoAdapter);
}
}

这是我的加载 Logcat( boolean 值),我在整个程序中跟踪它,因为我相信这是问题的主要部分。我已将其附在下面:

2019-05-30 10:26:14.590 24930-24970/com.yourgesture.newsocialmediatab I/FirebaseAuth: [FirebaseAuth:] Loading module via FirebaseOptions.
2019-05-30 10:26:14.956 24930-24930/com.yourgesture.newsocialmediatab D/Social Media Tab: onCreateView - Loading = true
2019-05-30 10:26:16.399 24930-24930/com.yourgesture.newsocialmediatab D/Social Media Tab: First get blogs instance - Loading = true
2019-05-30 10:26:16.406 24930-24930/com.yourgesture.newsocialmediatab D/Social Media Tab: First get blogs instance - Loading = true
2019-05-30 10:26:16.534 24930-24930/com.yourgesture.newsocialmediatab D/Social Media Tab: Just Entered onScrolled and Loading = false
2019-05-30 10:26:16.539 24930-24930/com.yourgesture.newsocialmediatab D/Social Media Tab: Right After getMoreBlogs gets called. Loading = true
2019-05-30 10:26:16.663 24930-24930/com.yourgesture.newsocialmediatab D/Social Media Tab: Just Entered onScrolled and Loading = false
2019-05-30 10:26:16.666 24930-24930/com.yourgesture.newsocialmediatab D/Social Media Tab: Right After getMoreBlogs gets called. Loading = true
2019-05-30 10:26:16.826 24930-24930/com.yourgesture.newsocialmediatab D/Social Media Tab: Just Entered onScrolled and Loading = false
2019-05-30 10:26:16.828 24930-24930/com.yourgesture.newsocialmediatab D/Social Media Tab: Right After getMoreBlogs gets called. Loading = true
2019-05-30 10:26:16.976 24930-24930/com.yourgesture.newsocialmediatab D/Social Media Tab: Just Entered onScrolled and Loading = false
2019-05-30 10:26:16.977 24930-24930/com.yourgesture.newsocialmediatab D/Social Media Tab: Right After getMoreBlogs gets called. Loading = true
2019-05-30 10:26:17.229 24930-24930/com.yourgesture.newsocialmediatab D/Social Media Tab: Just Entered onScrolled and Loading = false

最佳答案

在你的firstEventListener()中,

getReference().child("博客")实际上会获取整个子数据,这将被视为读取操作,即使您没有使用它。

因此您的方法不会保存任何数据。

让我分享一种方法:

private void getUsers(String nodeId) {
Query query;

if (nodeId == null)
query = FirebaseDatabase.getInstance().getReference()
.child(Consts.FIREBASE_DATABASE_LOCATION_USERS)
.orderByKey()
.limitToFirst(mPostsPerPage);
else
query = FirebaseDatabase.getInstance().getReference()
.child(Consts.FIREBASE_DATABASE_LOCATION_USERS)
.orderByKey()
.startAt(nodeId)
.limitToFirst(mPostsPerPage);

query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
UserModel user;
List<UserModel> userModels = new ArrayList<>();
for (DataSnapshot userSnapshot : dataSnapshot.getChildren()) {
userModels.add(userSnapshot.getValue(UserModel.class));
}

mAdapter.addAll(userModels);
mIsLoading = false;
}

@Override
public void onCancelled(DatabaseError databaseError) {
mIsLoading = false;
}
});
}

三个新变量在上述函数中很重要:mPostsPerPage:MainActivity.java类中定义的Integer常量,指定每页查询多少条记录。

mIsLoading:它是 MainActivity 中定义的 boolean 值,用于跟踪 Firebase 数据库查询是否正在进行。

Consts.FIREBASE_DATABASE_LOCATION_USERS:它是在 Consts.java 类中定义的常量,其中仅存储“users”节点的位置

引用号:https://blog.shajeelafzal.com/2017/12/13/firebase-realtime-database-pagination-guide-using-recyclerview/

关于java - 当用户滚动到 RecyclerView 底部时,使用 ScrollListener 从 Firebase 实时数据库提取数据时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56374510/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com