滑动刷新布局不适用于 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
中定义,它会访问 class.
中的任何位置
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)
}
我只在将 relative/constraint 布局作为回收视图的父布局并将滑动刷新布局作为这些布局的父布局时遇到这些渲染问题。
变化自
<SwipeRefreshLayout>
<RelativeLayout>
<RecyclerView>
至
<SwipeRefreshLayout>
<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
中定义,它会访问 class.
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)
}
我只在将 relative/constraint 布局作为回收视图的父布局并将滑动刷新布局作为这些布局的父布局时遇到这些渲染问题。
变化自
<SwipeRefreshLayout>
<RelativeLayout>
<RecyclerView>
至
<SwipeRefreshLayout>
<RecyclerView>
这确实对我有用。