簡體   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