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

刷卡刷新布局不适用于 recyclerview

如何解决刷卡刷新布局不适用于 recyclerview

我有一个带有导航视图的布局,抽屉布局作为父级。然后我有其他视图的线性布局,包括滑动刷新布局和回收器视图:

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activities.MainActivity"
    android:orientation="vertical"
    android:fitsSystemWindows="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar_main_activity"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/black"/>

        <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
            android:id="@+id/swipeRefreshLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/rv_main_activity"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>

        </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

    </LinearLayout>

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        app:headerLayout="@layout/nav_header_layout"
        app:menu="@menu/nav_main_layout"
        android:layout_gravity="start"/>

</androidx.drawerlayout.widget.DrawerLayout>

问题是加载图标出现但永远不会消失,即使我编写代码让它消失:

swipeRefreshLayout.setonRefreshListener(object : SwipeRefreshLayout.OnRefreshListener{
            override fun onRefresh() {
                Toast.makeText(this@MainActivity,"Swiped",Toast.LENGTH_SHORT).show()
                swipeRefreshLayout.isRefreshing = false
            }

        })

甚至连吐司消息都没有出现。

我的代码或 xml 有什么问题?

error

解决方法

当您将 swipeRefreshLayout 定义为本地到 onCreate 视图时,它仅访问该块。如果在全局 swipeRefreshLayout 中定义,它将访问类中的任何位置。

var swipeRefreshLayout: SwipeRefreshLayout? = null

在 onCreate 方法中

swipeRefreshLayout = binding.swipeRefreshLayout

打电话

swipeRefreshLayout?.isRefreshing = false
,
swipeRefresh.setOnRefreshListener {
            refreshAction()                    // refresh your list contents somehow
            swipeRefresh.isRefreshing = false   // reset the SwipeRefreshLayout (stop the loading spinner)
        }

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