Android

Android SwipeRefreshLayout(下拉刷新數據)

1.先導入函示庫(build.gradle: Module)

dependencies {
   implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0"
}

2.程式碼範例

a.layout

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
     android:id="@+id/swipe"
     android:layout_width="match_parent"
     android:layout_height="match_parent">

     <androidx.recyclerview.widget.RecyclerView
         android:id="@+id/recycler"
         android:layout_width="match_parent"
        android:layout_height="match_parent" />

</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

b.創建對話框

     //設定刷新View的顏色
     swipe.setColorSchemeResources(
         R.color.colorPrimary,
         R.color.colorPrimaryDark,
         R.color.colorAccent
     )

     //刷新監聽
     swipe.setOnRefreshListener {
         //3秒後 刷新效果消失
         object :CountDownTimer(3000,1000) {
             override fun onFinish() {
                 swipe.isRefreshing = false
             }

             override fun onTick(p0: Long) {

             }
         }.start()
     }

4.效果展示

發表迴響