当用户在SwipeRefreshLayout中向下滚动时刷新项目

问题描述 投票:0回答:1

我在RecyclerView上有一个SwipeRefreshLayout,我希望这些项目在用户向下滚动时刷新,而不是每次都从顶部下拉。我需要类似Instagram或Facebook的内容,这些内容会在用户向下滚动时加载。这样,用户就不必不断反复出现来刷新和加载更多帖子

这是我的XML代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.google.android.material.appbar.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/bar">

</com.google.android.material.appbar.AppBarLayout>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
    android:id="@+id/swiperefresh"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/bar">
    <androidx.core.widget.NestedScrollView
        android:id="@+id/scrollview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/bar">

            <androidx.recyclerview.widget.RecyclerView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/bar"
                android:id="@+id/recycler_view_posts"/>
    </androidx.core.widget.NestedScrollView>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

</RelativeLayout>

这是我的片段代码:

public class HomeFragment extends Fragment {
//POSTS
private RecyclerView recyclerViewPosts;
private PostAdapter postAdapter;
private List<Post> postLists;
private List<String> followingList;
private Context mContext;
private static final int TOTAL_NUMBER_OF_ITEMS_TO_LOAD = 5;
private SwipeRefreshLayout swipeRefreshLayout;
private int currentPage = 1;

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

//POSTS
recyclerViewPosts = view.findViewById(R.id.recycler_view_posts);
recyclerViewPosts.setHasFixedSize(true);
final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
linearLayoutManager.setReverseLayout(true);
linearLayoutManager.setStackFromEnd(true);
recyclerViewPosts.setLayoutManager(linearLayoutManager);
postLists = new ArrayList<>();
postAdapter = new PostAdapter(getContext(), postLists);
recyclerViewPosts.setAdapter(postAdapter);
swipeRefreshLayout = view.findViewById(R.id.swiperefresh);
checkFollowing();
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener(){
@Override
public void onRefresh() {
    currentPage++;
    postLists.clear();
    readPosts();
    }
    });
    return view;
    }
private void readPosts() {
    DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Posts");
    Query postQuery = reference.limitToLast(currentPage * TOTAL_NUMBER_OF_ITEMS_TO_LOAD);
    postQuery.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
    postLists.clear();
    for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
    Post post = snapshot.getValue(Post.class);
    postLists.add(post);
    }
    postAdapter.notifyDataSetChanged();
    swipeRefreshLayout.setRefreshing(false);
    }
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
    }
    });
    }
}
android firebase android-recyclerview pagination swiperefreshlayout
1个回答
0
投票

我建议您使用Android的Paging Library。它是为此设计的,并且您为此提供了不错的教程Paging Library。这可能需要一些工作,但值得。

© www.soinside.com 2019 - 2024. All rights reserved.