【问题标题】:How to make Custom Pull to refresh for recyclerView? (Not Swipe to refresh)如何使自定义拉动刷新recyclerView? (不要滑动刷新)
【发布时间】:2021-12-11 22:40:03
【问题描述】:

我需要这个自定义下拉菜单来刷新布局。我怎样才能为Android开发这个? 请确保我想要的正是这种行为,而不是 Android 原生滑动刷新。

【问题讨论】:

  • 还有many open source pull to refresh implementations,另外还有谷歌自己开源的SwipeRefreshLayout。检查他们的实现,看看他们如何解决问题,如果现有解决方案都不符合您的需求,请使用它来得出您自己的解决方案。

标签: android kotlin material-design


【解决方案1】:

首先添加:

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>

MainActivity

 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
    }
}

【讨论】:

  • 我以前用过这个,它会显示一个默认的行为,默认的圆形进度条。我想要一个拉伸效果,就像我在 GIF 中展示的那样
猜你喜欢
  • 2017-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多