停止 SwipeRefreshLayout 刷新动画,尽管 setRefresh(false)/isRefreshing=false

Stop SwipeRefreshLayout refresh animation, despite setRefresh(false)/isRefreshing=false

我正在使用 SwipeRefreshLayout 刷新我的 activity。布局看起来像这样,非常简单:

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/swipeRefreshLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <androidx.core.widget.NestedScrollView
            android:id="@+id/feedBase"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        <LinearLayout
                android:id="@+id/linearLayoutFeed"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"/>
    </androidx.core.widget.NestedScrollView>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

linearLayoutFeed 在刷新时以编程方式添加一些视图,并在线加载数据。该过程完成后,我调用 isRefreshing=false(Java 等效为 setRefreshing(false)。这似乎是停止它的唯一方法,也是其他问题中建议的方法。但是,这不起作用。

刷新指示器永远不会消失。我检查了调试器,操作完成并调用了 isRefreshing=false 的回调。之后,我检查了 isRefreshing 的值,它正确显示 false.

这是我的(简化)代码 运行:

swipeRefreshLayout.setOnRefreshListener {
        loadData() { // callback
            (context as AppCompatActivity).runOnUiThread {
                swipeRefreshLayout.isRefreshing = false
            }
        }
    }

private fun loadData(callback: () -> Unit) {
    Thread {
        try {
            // loading my data
        } catch (e: Exception) {
            e.printStackTrace()
        } finally {
            callback.invoke()
        }
    }.start()
}

这基本上是我在这里和其他网站上的其他 questions/answers 中读到的内容。但是,setRefreshing 的文档指出:

Notify the widget that refresh state has changed. Do not call this when refresh is triggered by a swipe gesture.

我一直在寻找其他函数来调用以结束刷新或只是不设置刷新,但我没有找到任何其他方法,也没有设置它有效。

现在是复杂的部分:

SwipeRefreshLayout设置为ViewPagerTabLayoutSwipeRefreshLayout 仅在第一页上。现在,当我第一次刷新它时,刷新指示器不会消失。但是当我将页面切换到最后一页然后返回第一页并刷新时,刷新指示器在完成后消失。

所以基本上

我不太理解这种行为,希望得到任何帮助,找出这里出了什么问题。我不认为我用页面切换改变了任何应该改变 SwipeRefreshLayout 行为的东西,但不知何故它确实改变了。

编辑

回答评论中的问题:

编辑 2

我刚刚注意到,在出现此错误时,我可以继续滑动。当第一个刷新指示器仍然存在时,它后面的第二个刷新指示器会出现并消失。

看来我的一些基础知识有误,或者至少对我的 activity/layout/views 是如何构建的有错误的理解。

加载指示器没有消失的原因是它确实消失了。他们中的一个至少做到了。

我以为我有一个 SwipeRefreshLayout,但实际上有两个!但是我只停用了其中一个。

我为布局充气的自定义视图本身就是一个 SwipeRefreshLayout。所以我以为我有这个层次结构:

SwipeRefreshLayout
->NestedScrollView
-->LinearLayout

但我实际上有这样的层次结构:

SwipeRefreshLayout (View itself)
->SwipeRefreshLayout (from Layout)
-->NestedScrollView
--->LinearLayout

所以我所做的是停止刷新我从视图而不是视图本身膨胀的 SwipeRefreshLayout。 因此,显而易见的解决方法是从布局文件中删除 SwipeRefreshLayout 并相应地管理视图本身。

话虽这么说,我还是不明白为什么文档说

Notify the widget that refresh state has changed. Do not call this when refresh is triggered by a swipe gesture.

这让我误会了问题所在。

另外,我还是不太明白为什么我来回滑动时只出现一次,然后就没有再出现,如上所述。我 假设 这是因为视图本身在重新聚焦时没有重绘刷新指示器,即使它仍然是 "active"。所以它在那里但看不见,只是没有得到 refreshed/redrawn。虽然不确定。