android-fragments 如何在Kotlin中刷新活动片段

dldeef67  于 2022-11-14  发布在  Android
关注(0)|答案(1)|浏览(215)

在我的Kotlin项目中,我想刷新一个Activity的片段。当我完成Activity时,它应该刷新片段。我想这可以在onResume中完成。那么当我恢复Activity时,如何刷新当前片段呢?我的片段页面包含列表视图,因此当我完成Activity时,它应该被更新。

kfgdxczn

kfgdxczn1#

"试试这个"
使用滑动刷新布局

<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"
    tools:context=".MainActivity">

<ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listView"
        />

</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

主要活动

class MainActivity : AppCompatActivity() {
   lateinit var swipeRefreshLayout: SwipeRefreshLayout

   override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      setContentView(R.layout.activity_main)
      swipeRefreshLayout = findViewById(R.id.swipeRefreshLayout)
      swipeRefreshLayout.setOnRefreshListener {
           //TODO operation
         }
      }
   }

相关问题