•  


Guide - Accompanist
Skip to content

Swipe Refresh for Jetpack Compose

Maven Central

Warning

This library is deprecated, with official pull refresh support in androidx.compose.material.pullrefresh . The migration guide and original documentation is below.

Migration

Accompanist SwipeRefresh has been replaced by PullRefresh in Compose Material 1.3.0 . The implementation is similar but instead of being a Composable function, it is a Modifier that can be applied to a Composable function.

A simple example is as follows:

val
 viewModel
:
 MyViewModel
 =
 viewModel
()

val
 refreshing
 by
 viewModel
.
isRefreshing


val
 pullRefreshState
 =
 rememberPullRefreshState
(
refreshing
,
 {
 viewModel
.
refresh
()
 })


Box
(
Modifier
.
pullRefresh
(
pullRefreshState
))
 {

    LazyColumn
(
Modifier
.
fillMaxSize
())
 {

        ...

    }


    PullRefreshIndicator
(
refreshing
,
 pullRefreshState
,
 Modifier
.
align
(
Alignment
.
TopCenter
))

}

Migration steps

  1. Replace SwipeRefresh with a Box or other layout of your choice, save your onRefresh lambda for the next step.
  2. Replace rememberSwipeRefreshState() with rememberPullRefreshState(refreshing, onRefresh)
  3. Add either the default PullRefreshIndicator or your own custom implementation to your layout.

Custom Indicator

Instead of using the provided PullRefreshIndicator composable, you can create your own custom indicator. A full sample can be seen in the Compose samples .

Original Docs

A library which provides a layout which provides the swipe-to-refresh UX pattern, similar to Android's SwipeRefreshLayout .

SwipeRefresh demo

Usage

To implement this UX pattern there are two key APIs which are needed: SwipeRefresh , which is provides the layout, and rememberSwipeRefreshState() which provides some remembered state.

The basic usage of a SwipeRefresh using a ViewModel looks like so:

val
 viewModel
:
 MyViewModel
 =
 viewModel
()

val
 isRefreshing
 by
 viewModel
.
isRefreshing
.
collectAsState
()


SwipeRefresh
(

    state
 =
 rememberSwipeRefreshState
(
isRefreshing
),

    onRefresh
 =
 {
 viewModel
.
refresh
()
 },

)
 {

    LazyColumn
 {

        items
(
30
)
 {
 index
 ->

            // TODO: list items

        }

    }

}

The full example, including the view model implementation can be found here .

The content needs to be 'vertically scrollable' for SwipeRefresh() to be able to react to swipe gestures. Layouts such as LazyColumn are automatically vertically scrollable, but others such as Column or LazyRow are not. In those instances, you can provide a Modifier.verticalScroll modifier to that content like so:

SwipeRefresh
(

    // ...

)
 {

    Column
(
Modifier
.
verticalScroll
(
rememberScrollState
()))
 {

        // content

    }

}

Indicating a refresh without swiping

As this library is built with a separate state object, it's easy to display a refreshing indicator without a swipe to triggering it.

The unrealistic example below displays a forever refreshing indicator:

val
 swipeRefreshState
 =
 rememberSwipeRefreshState
(
true
)


SwipeRefresh
(

    state
 =
 swipeRefreshState
,

    onRefresh
 =
 {
 /* todo */
 },

)
 {

    LazyColumn
 {

        items
(
30
)
 {
 index
 ->

            // TODO: list items

        }

    }

}

Indicator

The library provides a default indicator: SwipeRefreshIndicator() , which SwipeRefresh uses automatically. You can customize the default indicator, and even provide your own indicator content using the indicator slot.

Customizing default indicator

To customize the default indicator, we can provide our own indicator content block, to call SwipeRefreshIndicator() with customized parameters:

SwipeRefresh
(

    state
 =
 /* ... */
,

    onRefresh
 =
 /* ... */
,

    indicator
 =
 {
 state
,
 trigger
 ->

        SwipeRefreshIndicator
(

            // Pass the SwipeRefreshState + trigger through

            state
 =
 state
,

            refreshTriggerDistance
 =
 trigger
,

            // Enable the scale animation

            scale
 =
 true
,

            // Change the color and shape

            backgroundColor
 =
 MaterialTheme
.
colors
.
primary
,

            shape
 =
 MaterialTheme
.
shapes
.
small
,

        )

    }

)

Tweaked indicator demo

Custom indicator

As mentioned, you can also provide your own custom indicator content. A SwipeRefreshState is provided to indicator content slot, which contains the information necessary to react to a swipe refresh gesture.

An example of a custom indicator is provided here .

Download

Maven Central

repositories
 {

    mavenCentral
()

}


dependencies
 {

    implementation
 "com.google.accompanist:accompanist-swiperefresh:<version>"

}

Snapshots of the development version are available in Sonatype's snapshots repository . These are updated on every commit.

- "漢字路" 한글한자자동변환 서비스는 교육부 고전문헌국역지원사업의 지원으로 구축되었습니다.
- "漢字路" 한글한자자동변환 서비스는 전통문화연구회 "울산대학교한국어처리연구실 옥철영(IT융합전공)교수팀"에서 개발한 한글한자자동변환기를 바탕하여 지속적으로 공동 연구 개발하고 있는 서비스입니다.
- 현재 고유명사(인명, 지명등)을 비롯한 여러 변환오류가 있으며 이를 해결하고자 많은 연구 개발을 진행하고자 하고 있습니다. 이를 인지하시고 다른 곳에서 인용시 한자 변환 결과를 한번 더 검토하시고 사용해 주시기 바랍니다.
- 변환오류 및 건의,문의사항은 juntong@juntong.or.kr로 메일로 보내주시면 감사하겠습니다. .
Copyright ⓒ 2020 By '전통문화연구회(傳統文化硏究會)' All Rights reserved.
 한국   대만   중국   일본