简体   繁体   中英

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

I need this custom pull-down to refresh layout. How Can I develop this for Android? Please make sure I want exactly this behavior, not Android native swipe to refresh.

在此处输入图像描述

For that, you can use a library. I found the one that suits me https://github.com/SimformSolutionsPvtLtd/SSPullToRefresh . There are also lots of other libraries providing the same functionality. Thanks!!

First of all add:

implementation androidx.swiperefreshlayout:swiperefreshlayout:1.1.0

to your gradle file

XML file

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM