原文链接https://www.shanya.world/archives/614f32fd.html
添加依赖
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.0.0'
xml替换上SwipeRefreshLayout
放上一个TextView做刷新演示
<?xml version="1.0" encoding="utf-8"?>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout 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:id="@+id/swipeRedreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal|center_vertical"
android:text="TextView"
android:textSize="30sp" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
效果图
在这里插入图片描述
MainActivity 代码
package com.shanya.swiperefreshdemo
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
import java.util.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
swipeRedreshLayout.setOnRefreshListener {
textView.text = System.currentTimeMillis().toString()
swipeRedreshLayout.isRefreshing = false
}
}
}
网友评论