【问题标题】:Swipe refresh layout not working with recyclerview滑动刷新布局不适用于 recyclerview
【发布时间】:2021-06-25 10:00:18
【问题描述】:

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

<?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 有什么问题?

【问题讨论】:

  • 向我们展示完整代码或最大代码,我们可以看到从变量到 id 的完整初始化
  • 对不起,我不明白,我使用 kotlin 合成绑定,所以我不需要初始化变量,如果这就是你的意思

标签: android android-recyclerview swiperefreshlayout


【解决方案1】:

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

var swipeRefreshLayout: SwipeRefreshLayout? = null

在 onCreate 方法中

swipeRefreshLayout = binding.swipeRefreshLayout

用于通话

swipeRefreshLayout?.isRefreshing = false

【讨论】:

  • 我正在使用 kotlin 扩展,所以我可以在班级的任何地方访问 id
  • 合成调用可能会产生一个问题,即您必须通过绑定初始化或通过绑定调用它是安全的。
  • 你能说出为什么合成调用不起作用吗?
  • 如果存在名称冲突,我们可以通过使用关键字在本地重命名冲突实体来消除歧义。这是你可以通过它stackoverflow.com/questions/41810179/…
  • 但是我的项目没有名字冲突
【解决方案2】:
swipeRefresh.setOnRefreshListener {
            refreshAction()                    // refresh your list contents somehow
            swipeRefresh.isRefreshing = false   // reset the SwipeRefreshLayout (stop the loading spinner)
        }

【讨论】:

    【解决方案3】:

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

    改变

    
    <SwipeRefreshLayout>
     <RelativeLayout>
       <RecyclerView>
    
    

    <SwipeRefreshLayout>
       <RecyclerView>
    

    这实际上对我有用。

    【讨论】:

      猜你喜欢
      • 2017-11-28
      • 2019-10-07
      • 2015-10-25
      • 1970-01-01
      • 1970-01-01
      • 2020-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多