簡體   English   中英

recyclerView 如何自定義拉取刷新? (不是滑動刷新)

[英]How to make Custom Pull to refresh for recyclerView? (Not Swipe to refresh)

我需要這個自定義下拉菜單來刷新布局。 我如何為 Android 開發這個? 請確保我想要的正是這種行為,而不是 Android 本機滑動刷新。

在此處輸入圖像描述

為此,您可以使用庫。 我找到了適合我的https://github.com/SimformSolutionsPvtLtd/SSPullToRefresh 還有許多其他庫提供相同的功能。 謝謝!!

首先補充:

implementation androidx.swiperefreshlayout:swiperefreshlayout:1.1.0

到你的gradle 文件

XML文件

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
    android:id="@+id/refreshLayout"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    app:layout_constraintTop_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintBottom_toBottomOf="parent">

//Add here your views
// I've got a RecyclerView so for what I need height is 0dp

</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

主要活動

 override fun onCreate(savedInstanceState: Bundle?) {

    super.onCreate(savedInstanceState)
    setContentView(activity_main)

    /* you probably have a few 
    code lines here*/
    initRefreshListener()
}
.
.
.
  private fun initRefreshListener() {

    val swipeRefreshLayout = findViewById<SwipeRefreshLayout>(R.id.refreshLayout)

    swipeRefreshLayout.setOnRefreshListener {

        Toast.makeText(this, "Refreshed", Toast.LENGTH_SHORT)
            .show() // totally optional
        // call API or what have you here
        swipeRefreshLayout.isRefreshing = false
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM