UltraSwipeRefresh

repository·master·Indexed 43 days ago

https://github.com/jenly1314/ultraswiperefresh

A Jetpack Compose-based library providing advanced pull-to-refresh and pull-to-load-more capabilities. It offers a more customizable alternative to the official SwipeRefresh, featuring multiple sliding modes (Translate, FixedContent, FixedBehind, FixedFront), customizable header and footer indicators, and support for secondary content. Version 1.6.0 includes built-in indicator styles for Classic, Progress Bar, and Lottie animations.

Tokens
3.4K
Snippets
7
Records
11
Agent score
54%

What's inside UltraSwipeRefresh

  1. Overview of UltraSwipeRefresh

    master

    UltraSwipeRefresh is a high-performance Jetpack Compose component designed for pull-to-refresh and pull-to-load-more interactions. It serves as a more feature-rich and extensible alternative to the official Android SwipeRefresh.

    Key features include:

    • Multiple Sliding Modes: Supports Translate, FixedContent, FixedBehind, and FixedFront interaction modes.
    • Fully Customizable UI: Allows complete customization of indicators via headerIndicator and footerIndicator.
    • Flexible Configuration: Any sliding mode can be combined with any indicator style.
    • Secondary Content Support: Supports Header and Footer secondary content (e.g., a 'second floor' for headers or a 'basement' for footers) for multi-layered interaction scenarios.
  2. Explore related libraries

    master

    The author provides several other specialized Android UI components and libraries, including:

    • Compose-based components: CodeTextField (verification code input), compose-component (general Jetpack Compose component library).
    • Layout extensions: SuperSwipeRefreshLayout (supports pull-up refresh), SuperSlidingPaneLayout (enhanced sliding effects).
    • Animation & View components: SuperTextView (animated TextView), LoadingView (arc loading animation), WaveView (ripple animation), GiftSurfaceView (gift pattern animation), FlutteringLayout (heart floating effect), DragPolygonView (draggable polygons), CircleProgressView (circular progress), ArcSeekBar (arc slider), and DrawBoard (drawing/editing board).
  3. Install UltraSwipeRefresh dependencies

    master

    Add the following dependencies to your module's build.gradle file. The core refresh component is required, while various indicator styles are optional.

    Note: The current versions use the -SNAPSHOT suffix as they are hosted on JitPack before a formal Maven Central release.

    // 极致体验的Compose刷新组件 (*必须)
    implementation 'com.github.jenly1314.UltraSwipeRefresh:refresh:-SNAPSHOT'
    
    // 经典样式的指示器 (可选)
    implementation 'com.github.jenly1314.UltraSwipeRefresh:refresh-indicator-classic:-SNAPSHOT'
    
    // Lottie动画指示器 (可选)
    implementation 'com.github.jenly1314.UltraSwipeRefresh:refresh-indicator-lottie:-SNAPSHOT'
    
    // 进度条样式的指示器 (可选)
    implementation 'com.github.jenly1314.UltraSwipeRefresh:refresh-indicator-progress:-SNAPSHOT'
  4. Install UltraSwipeRefresh via Gradle

    master

    To use UltraSwipeRefresh in your Android project, follow these two steps:

    1. Add mavenCentral() to your project's build.gradle or settings.gradle file.
    2. Add the core dependency and any optional indicator libraries to your module's build.gradle file.
    // 1. In Project build.gradle or settings.gradle
    repositories {
        mavenCentral()
    }
    
    // 2. In Module build.gradle
    // Core component (Required)
    implementation 'com.github.jenly1314.UltraSwipeRefresh:refresh:1.6.0'
    
    // Optional Indicator Libraries
    implementation 'com.github.jenly1314.UltraSwipeRefresh:refresh-indicator-classic:1.6.0'
    implementation 'com.github.jenly1314.UltraSwipeRefresh:refresh-indicator-lottie:1.6.0'
    implementation 'com.github.jenly1314.UltraSwipeRefresh:refresh-indicator-progress:1.6.0'
  5. Configure global defaults with UltraSwipeRefreshTheme

    master

    If your application requires a consistent refresh style across all screens, use UltraSwipeRefreshTheme.config to set global default configurations. This is recommended to be done in your Application.onCreate() method.

    You can use the .copy() method on the existing config to update specific properties like headerScrollMode or footerScrollMode without affecting others.

    // Global setting of default sliding mode (Recommended in Application.onCreate)
    UltraSwipeRefreshTheme.config = UltraSwipeRefreshTheme.config.copy(
       headerScrollMode = NestedScrollMode.Translate,
       footerScrollMode = NestedScrollMode.Translate,
    )
  6. Use the UltraSwipeRefresh Composable

    master

    UltraSwipeRefresh is a Compose component that supports both pull-to-refresh and pull-to-load-more gestures. It is designed as a more feature-rich and extensible replacement for the official SwipeRefresh.

    To use it, you need to manage its state using rememberUltraSwipeRefreshState() and provide callbacks for onRefresh and onLoadMore.

    Key parameters include:

    • state: The state object used to control and observe refresh/load-more status.
    • onRefresh: Callback triggered when the pull-to-refresh gesture completes.
    • onLoadMore: Callback triggered when the pull-to-load-more gesture completes.
    • headerIndicator: The UI component displayed during pull-to-refresh.
    • footerIndicator: The UI component displayed during pull-to-load-more.
    • onCollapseScroll: An optional callback used to synchronize list position when the header/footer collapses, helping to eliminate visual bounce.
    • headerScrollMode / footerScrollMode: Defines the sliding mode (e.g., NestedScrollMode.Translate).

    Tip: For the best sliding experience, always pair headerIndicator/footerIndicator with headerScrollMode/footerScrollMode.

    @Composable
    fun UltraSwipeRefreshSample() {
        val state = rememberUltraSwipeRefreshState()
        var itemCount by remember { mutableIntStateOf(20) }
        val coroutineScope = rememberCoroutineScope()
        val lazyListState = rememberLazyListState()
    
        UltraSwipeRefresh(
            state = state,
            onRefresh = {
                coroutineScope.launch {
                    state.isRefreshing = true
                    // TODO: Refresh logic
                    delay(2000)
                    itemCount = 20
                    state.isRefreshing = false
                }
            },
            onLoadMore = {
                coroutineScope.launch {
                    state.isLoading = true
                    // TODO: Load more logic
                    delay(2000)
                    itemCount += 20
                    state.isLoading = false
                }
            },
            modifier = Modifier.background(color = Color(0x7FEEEEEE)),
            headerScrollMode = NestedScrollMode.Translate,
            footerScrollMode = NestedScrollMode.Translate,
            onCollapseScroll = {
                // it < 0: triggered by pull-to-refresh collapsing
                // it > 0: triggered by pull-to-load-more collapsing
                if (it > 0) {
                    lazyListState.animateScrollBy(it)
                }
            },
            headerIndicator = {
                ClassicRefreshHeader(it)
            },
            footerIndicator = {
                ClassicRefreshFooter(it)
            }
        ) {
            LazyColumn(
                modifier = Modifier.fillMaxSize().background(color = Color.White),
                state = lazyListState,
            ) {
                repeat(itemCount) {
                    item {
                        Text(
                            text = "UltraSwipeRefresh列表Item${it + 1}",
                            modifier = Modifier.padding(horizontal = 16.dp, vertical = 10.dp),
                            color = Color(0xFF333333),
                            fontSize = 16.sp
                        )
                        Divider(
                            modifier = Modifier.padding(horizontal = 16.dp),
                            color = Color(0xFFF2F3F6)
                        )
                    }
                }
            }
        }
    }
  7. Available Indicator Styles

    master

    UltraSwipeRefresh provides several built-in indicator styles for both Headers and Footers. You can use these directly or implement your own custom indicators (including Lottie animations).

    Built-in Styles

    CategoryHeader OptionsFooter Options
    Official DefaultSwipeRefreshHeaderSwipeRefreshFooter
    ClassicClassicRefreshHeaderClassicRefreshFooter
    Progress BarProgressRefreshHeaderProgressRefreshFooter
    Lottie AnimationLottieRefreshHeaderLottieRefreshFooter

    To use a Lottie animation, you can use the provided Lottie indicators or create a custom one using any Lottie JSON file.

  8. Manage refresh and loading status via UltraSwipeRefreshState

    master

    The UltraSwipeRefreshState class provides observable properties to manage the lifecycle of pull-to-refresh and pull-to-load actions. You can update these properties to trigger UI changes in the component.

    Key Properties

    • isRefreshing: Set to true to indicate the component is currently performing a pull-down refresh. Setting this to false will trigger the header to collapse.
    • isLoading: Set to true to indicate the component is currently performing a pull-up load (loading more). Setting this to false will trigger the footer to collapse.
    • isFinishing: An internal-set property that becomes true during the temporary state when a header or footer is collapsing after isRefreshing or isLoading transitions to false.
    val state = rememberUltraSwipeRefreshState()
    
    // Example: Triggering a refresh
    // In your refresh logic:
    state.isRefreshing = true
    
    // Once data is loaded:
    state.isRefreshing = false
  9. Initialize UltraSwipeRefreshState with rememberUltraSwipeRefreshState

    master

    To control and observe the refresh and loading status of an UltraSwipeRefresh component, use the rememberUltraSwipeRefreshState composable function. This ensures the state is preserved across recompositions.

    You can initialize the state with specific initial values for isRefreshing and isLoading, or use the parameterless version to start with both set to false.

    // Initialize with specific initial states
    val state = rememberUltraSwipeRefreshState(isRefreshing = false, isLoading = false)
    
    // Or initialize with default (false, false) states
    val state = rememberUltraSwipeRefreshState()