繁体   English   中英

滑动刷新布局不适用于 recyclerview

[英]Swipe refresh layout not working with 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 有什么问题?

错误

当您将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)
        }

当我将相对\/约束布局作为回收器视图的父级并将滑动刷新布局作为这些布局的父级时,我只遇到过这些渲染问题。

从改变


<SwipeRefreshLayout>
 <RelativeLayout>
   <RecyclerView>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM