gpt4 book ai didi

java - SwipeRefreshLayout 刷新 RecyclerView 无需下拉

转载 作者:行者123 更新时间:2023-12-02 08:42:23 25 4
gpt4 key购买 nike

我正在尝试在我的应用程序中实现 SwipeRefreshLayout 。我想要发生的是,当用户最初打开应用程序时,将从 Firebase 检索数据,而无需下拉布局以便数据刷新。之后,如果用户想要查看新数据,他们必须下拉布局,以便使用其他用户上传的新帖子刷新 RecyclerView

我遇到的问题是,当我从模拟器上传帖子时,它会自动弹出在我手机的屏幕上,而无需我下拉。那不是我想要的。首先,应用程序数据的初始打开应该自动检索而无需下拉。之后,每次你想查看新数据时,如果我转到不同的 fragment 然后返回,除非我下拉,否则数据不应该刷新。

我目前拥有的代码如下。有人可以告诉我为什么它没有按照我希望的方式工作吗?

我相对确定这是因为我的 readPosts(); 方法中的 notifyDataSetChanged(); 造成的,但我不确定如何修复它。 .我是否应该将其从 readPosts(); 中删除并将其添加到 mSwipeRefreshLayout.post...Runnable 中?

HomeFragment

public class HomeTabLayoutFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener {

private ProgressBar mProgressBar;
private PostAdapter mPostAdapter;
private List<Post> mPostLists;

private FirebaseAuth mFirebaseAuth;
private FirebaseUser mFirebaseUser;

private List<String> mFollowingList;

private SwipeRefreshLayout mSwipeRefreshLayout;

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

mFirebaseAuth = FirebaseAuth.getInstance();
mFirebaseUser = FirebaseAuth.getInstance().getCurrentUser();

mProgressBar = v.findViewById(R.id.progress_circular);

RecyclerView recyclerView = v.findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
linearLayoutManager.setReverseLayout(true);
linearLayoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(linearLayoutManager);
mPostLists = new ArrayList<>();
mPostAdapter = new PostAdapter(getContext(), mPostLists);
recyclerView.setAdapter(mPostAdapter);

mSwipeRefreshLayout = v.findViewById(R.id.refresh);
mSwipeRefreshLayout.setOnRefreshListener(this);
mSwipeRefreshLayout.post(() -> {
mSwipeRefreshLayout.setRefreshing(true);
readPosts();
});

checkIfUserExists();
checkFollowing();

return v;
}

@Override
public void onRefresh() {
readPosts();
}

private void checkIfUserExists() {
if (mFirebaseAuth == null) {
Intent intent = new Intent(getContext(), RegisterActivity.class);
startActivity(intent);
}
}

private void checkFollowing() {
mFollowingList = new ArrayList<>();
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Follow").child(mFirebaseUser.getUid()).child("Following");
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
mFollowingList.clear();
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
mFollowingList.add(snapshot.getKey());
}

readPosts();
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

}
});
}

private void readPosts() {
mSwipeRefreshLayout.setRefreshing(true);
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Posts");
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
mPostLists.clear();
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
Post post = snapshot.getValue(Post.class);
if (post != null && shouldAddPost(post)) {
mPostLists.add(post);
}
}

mPostAdapter.notifyDataSetChanged();
mProgressBar.setVisibility(View.GONE);
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

}
});

mSwipeRefreshLayout.setRefreshing(false);
}

private boolean shouldAddPost(@NotNull Post post) {
boolean isFollowingPublisher = false;
for (String id : mFollowingList) {
if (post.getPublisher().equals(id)) {
isFollowingPublisher = true;
break;
}
}

boolean isPublisher = post.getPublisher().equals(mFirebaseUser.getUid());

return isFollowingPublisher || isPublisher;
}
}

最佳答案

那是因为您正在使用addValueEventListener。在这种情况下,每次在此层次结构中进行任何更改时都会调用 ValueEventListener

如果您只需要一次数据,您应该使用addListenerForSingleValueEvent。换句话说,如果您不希望节点实时更新,那么您可以使用 addListenerForSingleValueEvent

->关于 mSwipeRefreshLayout.setRefreshing(false) 定位在 readPost 内的答案。

现在您添加的 mSwipeRefreshLayout.setRefreshing(true)mSwipeRefreshLayout.setRefreshing(false) 实际上是没有用的。它会立即调用..您应该做的是更改监听器内的状态。

private void readPosts() {
mSwipeRefreshLayout.setRefreshing(true);
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Posts");
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
mPostLists.clear();
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
Post post = snapshot.getValue(Post.class);
if (post != null && shouldAddPost(post)) {
mPostLists.add(post);
}
}
mPostAdapter.notifyDataSetChanged();
mProgressBar.setVisibility(View.GONE);
mSwipeRefreshLayout.setRefreshing(false);
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
mSwipeRefreshLayout.setRefreshing(false);
}
});

关于java - SwipeRefreshLayout 刷新 RecyclerView 无需下拉,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61299392/

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