Git Product home page Git Product logo

compose-animatedlist's Introduction

Compose AnimatedList

Animated infinite and finite LazyRow and LazyColumn with scale and color animations on scroll change based on how far they are to selector items

animated_list.mp4

Gradle Setup

To get a Git project into your build:

  • Step 1. Add the JitPack repository to your build file Add it in your root build.gradle at the end of repositories:
allprojects {
  repositories {
      ...
      maven { url 'https://jitpack.io' }
  }
}
  • Step 2. Add the dependency
dependencies {
    implementation 'com.github.SmartToolFactory:Compose-AnimatedList:Tag'
}

AnimatedInfiniteLazyRow/Column

Declaration

@Composable
fun <T> AnimatedInfiniteLazyColumn(
    modifier: Modifier = Modifier,
    items: List<T>,
    initialFistVisibleIndex: Int = 0,
    visibleItemCount: Int = 5,
    inactiveItemPercent: Int = 85,
    spaceBetweenItems: Dp = 4.dp,
    selectorIndex: Int = -1,
    itemScaleRange: Int = 1,
    showPartialItem: Boolean = false,
    activeColor: Color = Color.Cyan,
    inactiveColor: Color = Color.Gray,
    key: ((index: Int) -> Any)? = null,
    contentType: (index: Int) -> Any? = { null },
    itemContent: @Composable LazyItemScope.(
        animationProgress: AnimationProgress,
        index: Int,
        item: T,
        size: Dp,
        lazyListState: LazyListState
    ) -> Unit
) {
    AnimatedInfiniteList(
        modifier = modifier,
        items = items,
        initialFirstVisibleIndex = initialFistVisibleIndex,
        visibleItemCount = visibleItemCount,
        inactiveItemPercent = inactiveItemPercent,
        spaceBetweenItems = spaceBetweenItems,
        selectorIndex = selectorIndex,
        itemScaleRange = itemScaleRange,
        showPartialItem = showPartialItem,
        activeColor = activeColor,
        inactiveColor = inactiveColor,
        orientation = Orientation.Vertical,
        key = key,
        contentType = contentType,
        itemContent = itemContent
    )
}
@Composable
fun <T> AnimatedInfiniteLazyRow(
    modifier: Modifier = Modifier,
    items: List<T>,
    initialFistVisibleIndex: Int = 0,
    activeItemWidth: Dp,
    inactiveItemWidth: Dp,
    visibleItemCount: Int = 5,
    spaceBetweenItems: Dp = 4.dp,
    selectorIndex: Int = -1,
    itemScaleRange: Int = 1,
    showPartialItem: Boolean = false,
    activeColor: Color = ActiveColor,
    inactiveColor: Color = InactiveColor,
    key: ((index: Int) -> Any)? = null,
    contentType: (index: Int) -> Any? = { null },
    itemContent: @Composable LazyItemScope.(
        animationProgress: AnimationProgress,
        index: Int,
        item: T,
        size: Dp,
        lazyListState: LazyListState
    ) -> Unit
) 

Params

  • items the data list
  • visibleItemCount count of items that are visible at any time
  • inactiveItemPercent percentage of scale that items inside itemScaleRange
  • can be scaled down to. This is a number between 0 and 100
  • spaceBetweenItems padding between 2 items
  • selectorIndex index of selector. When itemScaleRange is odd number it's center of selected item, when itemScaleRange is even number it's center of item with selector index and the one next to it
  • itemScaleRange range of area of scaling. When this value is odd any item that enters half of item size range subject to being scaled. When this value is even any item in 2 item size range is subject to being scaled
  • showPartialItem show items partially that are at the start and end
  • activeColor color of selected item
  • inactiveColor color of items are not selected
  • key a factory of stable and unique keys representing the item. Using the same key for multiple items in the list is not allowed. Type of the key should be savable via Bundle on Android. If null is passed the position in the list will represent the key. When you specify the key the scroll position will be maintained based on the key, which means if you add/remove items before the current visible item the item with the given key will be kept as the first visible one.
  • contentType a factory of the content types for the item. The item compositions of the same type could be reused more efficiently. Note that null is a valid type and items of such type will be considered compatible.
  • itemContent the content displayed by a single item

Usage

itemContent returns AnimationProgress which contains LazyListState and distance of each item to selector. By attaching a click modifier

AnimatedInfiniteLazyRow(
    modifier = Modifier
        .width(listWidth)
        .padding(10.dp),
    items = aspectRatios,
    visibleItemCount = 5,
    activeItemWidth = 40.dp,
    inactiveItemWidth = 30.dp,
    selectorIndex = 1,
    spaceBetweenItems = 0.dp,
    inactiveColor = InactiveColor,
    activeColor = ActiveColor,
    itemContent = { animationProgress, index, item, width, lazyListState ->
        val color = animationProgress.color
        val scale = animationProgress.scale
        Box(
            modifier = Modifier
                .scale(scale)
                .background(color, CircleShape)
                .size(width)
                .clickable(
                    interactionSource = remember {
                        MutableInteractionSource()
                    },
                    indication = null
                ) {
                    coroutineScope.launch {
                        lazyListState.animateScrollBy(animationProgress.distanceToSelector)
                    }
                },
            contentAlignment = Alignment.Center
        ) {

            Text(
                "$index",
                color = Color.White,
                fontSize = 20.sp,
                fontWeight = FontWeight.Bold
            )
        }
    }
)

compose-animatedlist's People

Contributors

smarttoolfactory avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.