微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SwipeRefershLayout 在 Android 中不起作用

如何解决SwipeRefershLayout 在 Android 中不起作用

SwipeRefershLayout 不适用于 Android。

这是我的 XML 代码

//other code
.....

    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
    android:id="@+id/swipeRefreshLayout"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/searchCardView">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginTop="8dp"
        tools:listitem="@layout/room_dashboard_sample"/>

</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
.....
//other codes

这是我的主要活动:

        modelList = new ArrayList<>();
    dashboardAdapter = new DashboardAdapter(this,modelList);
    binding.recyclerView.setLayoutManager(new linearlayoutmanager(this));
    binding.recyclerView.setAdapter(dashboardAdapter);

    modelList.clear();
    firestore.collection("Rooms").orderBy("timestamp",Query.Direction.DESCENDING).get().addOnSuccessListener(queryDocumentSnapshots -> {
        List<DocumentSnapshot> list = queryDocumentSnapshots.getDocuments();
        for (DocumentSnapshot d : list) {
            DashboardModel obj = d.toObject(DashboardModel.class);
            modelList.add(obj);
        }
        dashboardAdapter.notifyDataSetChanged();
    }).addOnFailureListener(e -> Timber.d("onFailure: %s",e.getMessage()));

    //please give attension in this below code!
    binding.swipeRefreshLayout.setonRefreshListener(() -> {
        dashboardAdapter.notifyDataSetChanged();
        binding.swipeRefreshLayout.setRefreshing(false);
    });

解决方法

最好创建一个特定的方法来调用数据库。您可以将它们封装在一个方法中。

private void getRoomsList(){
    firestore.collection("Rooms").orderBy("timestamp",Query.Direction.DESCENDING).get().addOnSuccessListener(queryDocumentSnapshots -> {
        List<DocumentSnapshot> list = queryDocumentSnapshots.getDocuments();
        for (DocumentSnapshot d : list) {
            DashboardModel obj = d.toObject(DashboardModel.class);
            modelList.add(obj);
        }
        dashboardAdapter.notifyDataSetChanged();
    }).addOnFailureListener(e -> Timber.d("onFailure: %s",e.getMessage()));
}

然后,您只需在 onCreateswipeRefreshLayout 内调用它。

binding.swipeRefreshLayout.setOnRefreshListener(() -> {
    getRoomList();
    binding.swipeRefreshLayout.setRefreshing(false);
});

为了更好的方法,您可以在 setRefreshing 之后调用 notifyDataSetChanged 并将其从 getRoomList 下方删除,如下所示:

//another code...
dashboardAdapter.notifyDataSetChanged();
binding.swipeRefreshLayout.setRefreshing(false); //place it here

swipeRefreshLayout 时。

binding.swipeRefreshLayout.setOnRefreshListener(() -> {
    getRoomList();
});

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。