【问题标题】:Refreshing items as the user scrolls down in SwipeRefreshLayout当用户在 SwipeRefreshLayout 中向下滚动时刷新项目
【发布时间】:2020-07-06 13:46:36
【问题描述】:

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

【问题讨论】:

  • 嘿。你有很多物品吗?你想显示项目十到十例如???用户滚动?
  • @hamidkeyhani 是的,这正是我想要的……是的,我有很多图片,随着更多帖子的发布,它们会不断增加

标签: android firebase android-recyclerview pagination swiperefreshlayout


【解决方案1】:

我建议您使用 Android 中的 Paging Library。它是为它设计的,你有一个很好的教程here。这可能有点工作,但值得。

【讨论】:

  • 好的,我会看看,谢谢...您认为它可以与 Firebase 一起使用吗? @饼干
猜你喜欢
  • 1970-01-01
  • 2015-06-10
  • 2015-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-22
  • 2020-03-22
  • 2012-09-08
相关资源
最近更新 更多