Skip to content

Pull Down To Refresh

kevadiya krunal edited this page Apr 4, 2019 · 3 revisions

WaveSwipeRefreshLayout based on the {@link android.support.v4.widget.SwipeRefreshLayout} The WaveSwipeRefreshLayout should be used whenever the user can refresh the contents of a view via a vertical swipe gesture.

The activity that instantiates this view should add an OnRefreshListener to be notified whenever the swipe to refresh gesture is completed. The WaveSwipeRefreshLayout will notify the listener each and every time the gesture is completed again; the listener is responsible for correctly determining when to actually initiate a refresh of its content. If the listener determines there should not be a refresh, it must call setRefreshing(false) to cancel any visual indication of a refresh. If an activity wishes to show just the progress animation, it should call setRefreshing(true). To disable the gesture and progress animation, call setEnabled(false) on the view.

Who to use.

<com.kotlinlibrary.swiperefreshlayout.WaveSwipeRefreshLayout
            android:id="@+id/wsrl_main"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintBottom_toBottomOf="parent">

    <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
            tools:listitem="@layout/item_loadmore"
            tools:itemCount="5"/>
</com.kotlinlibrary.swiperefreshlayout.WaveSwipeRefreshLayout>
class SwipeToRefreshActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_swipe_to_refresh)

        val adapter = recyclerView.setUp<String> {
            withLayoutResId(R.layout.item_advertisement)
            onBind { _, item ->
                setBackgroundColor(getRandomColor())
                textViewAdvertisement.text = item
            }
            onClick{ id, index, item ->
                logs("(${R.id.textViewAdvertisement}, $id) -> $item", LogType.ERROR)
            }
            withItems(mutableListOf("one", "two", "three", "four", "five", "six", "seven"))
        }

        wsrl_main.setColorSchemeColors(Color.WHITE, Color.WHITE)
        wsrl_main.setWaveColor(Color.rgb(116, 170, 80))
        wsrl_main.setOnRefreshListener(object : WaveSwipeRefreshLayout.OnRefreshListener {
            override fun onRefresh() {
                Handler().postDelayed({
                    adapter + mutableListOf("one", "two", "three", "four", "five", "six", "seven")
                    wsrl_main.isRefreshing = false
                }, 5000)
            }
        })
    }

    private fun getRandomColor(): Int {
        val rnd = Random(System.nanoTime())
        return Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256))
    }
}

Screen-shot

Clone this wiki locally