CardStackView

repository·master·Indexed 25 days ago

https://github.com/yuyakaido/cardstackview

An Android library that provides a card stack UI component similar to Tinder, allowing users to swipe through items in a stack. It includes features for programmatic swiping and rewinding, customizable animations, overlay views for labels, and a CardStackListener for interaction callbacks. The library is managed via CardStackLayoutManager and CardStackAdapter.

Tokens
1.9K
Snippets
5
Records
10
Agent score
32%

What's inside CardStackView

  1. Implement Paging and Reloading

    master

    Paging

    To implement paging (loading more items as the user swipes), use one of the following methods:

    1. Use DiffUtil.
    2. Manually call RecyclerView.Adapter.notifyItemRangeInserted(int position, int itemCount).

    Caution: Do NOT call RecyclerView.Adapter.notifyDataSetChanged for paging, as it resets the top position and can cause performance issues.

    Reloading

    To reload the entire dataset, you can call RecyclerView.Adapter.notifyDataSetChanged.

  2. Setup CardStackView

    master

    To use CardStackView, you need to initialize it with a CardStackLayoutManager and a CardStackAdapter. This is typically done in your Activity or Fragment after finding the view by its ID.

    val cardStackView = findViewById<CardStackView>(R.id.card_stack_view)
    cardStackView.layoutManager = CardStackLayoutManager()
    cardStackView.adapter = CardStackAdapter()
  3. Implement Overlay Views

    master

    You can add overlay views (e.g., for showing 'Like' or 'Dislike' labels) by including a FrameLayout in your RecyclerView item layout. The library looks for specific layout IDs to identify the overlay position.

    Supported Layout IDs:

    • left_overlay (Left)
    • right_overlay (Right)
    • top_overlay (Top)
    • bottom_overlay (Bottom)

    You can control the alpha change rate of these overlays by setting a custom interpolator via CardStackLayoutManager.setOverlayInterpolator(interpolator).

    <FrameLayout
        android:id="@+id/left_overlay"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <!-- Set your left overlay -->
    
    </FrameLayout>
  4. Migrate from CardStackView 1.x to 2.x

    master

    If you are upgrading from version 1.x to 2.x, note the following changes in features and callbacks:

    Feature Mapping

    • Move to Origin $\rightarrow$ [Cancel](#cancel)
    • Reverse $\rightarrow$ [Rewind](#rewind)
    • ElevationEnabled $\rightarrow$ [Stack From](#stack-from)
    • TranslationDiff $\rightarrow$ [Translation Interval](#translation-interval)
    • ScaleDiff $\rightarrow$ [Scale Interval](#scale-interval)
    • SwipeEnabled $\rightarrow$ [Swipe Restriction](#swipe-restriction)

    Callback Mapping

    • Replace CardStackView.CardEventListener with CardStackListener.
    • onCardDragging(float percentX, float percentY) $\rightarrow$ onCardDragging(Direction direction, float ratio)
    • onCardSwiped(SwipeDirection direction) $\rightarrow$ onCardSwiped(Direction direction)
    • onCardReversed() $\rightarrow$ onCardRewound()
    • onCardMovedToOrigin() $\rightarrow$ onCardCanceled()
    • Note on Clicks: onCardClicked(int index) is no longer provided by the library. You must implement click handling within your RecyclerView item implementation.
  5. Scroll to specific positions

    master

    Use these methods for advanced navigation within the stack:

    • CardStackView.smoothScrollToPosition(int position): Scrolls to the specified position with an animation.
    • CardStackView.scrollToPosition(int position): Scrolls to the specified position immediately without animation.
  6. Implement CardStackListener callbacks

    master

    To respond to user interactions and card lifecycle events, implement the CardStackListener interface. The following methods are available:

    • onCardDragging(Direction direction, float ratio): Called continuously while the card is being dragged. direction indicates the swipe direction, and ratio represents the progress of the drag.
    • onCardSwiped(Direction direction): Called when the card has been successfully swiped.
    • onCardRewound(): Called when the card is returned to the stack (rewound).
    • onCardCanceled(): Called when the card drag is canceled (e.g., dragged less than the threshold).
    • onCardAppeared(View view, int position): Called when a specific card view appears on screen.
    • onCardDisappeared(View view, int position): Called when a specific card view disappears from the screen.
  7. Perform an automatic swipe

    master

    You can trigger a single swipe programmatically using CardStackView.swipe(). You can also customize the swipe animation using SwipeAnimationSetting.Builder() and applying it via CardStackLayoutManager.setSwipeAnimationSetting(setting).

    CardStackView.swipe()
    
    // With custom animation
    val setting = SwipeAnimationSetting.Builder()
            .setDirection(Direction.Right)
            .setDuration(Duration.Normal.duration)
            .setInterpolator(AccelerateInterpolator())
            .build()
    CardStackLayoutManager.setSwipeAnimationSetting(setting)
    CardStackView.swipe()
  8. Rewind the card stack

    master

    You can rewind the stack programmatically using CardStackView.rewind(). Custom rewind animations can be configured via RewindAnimationSetting.Builder() and applied using CardStackLayoutManager.setRewindAnimationSetting(setting).

    CardStackView.rewind()
    
    // With custom animation
    val setting = RewindAnimationSetting.Builder()
            .setDirection(Direction.Bottom)
            .setDuration(Duration.Normal.duration)
            .setInterpolator(DecelerateInterpolator())
            .build()
    CardStackLayoutManager.setRewindAnimationSetting(setting)
    CardStackView.rewind()
  9. Configure CardStackLayoutManager settings

    master

    The CardStackLayoutManager provides several methods to customize the visual behavior and constraints of the stack:

    MethodDescription
    setStackFrom(StackFrom)Sets the origin of the stack (e.g., StackFrom.Top, StackFrom.Bottom, StackFrom.Left, StackFrom.Right, or StackFrom.None).
    setVisibleCount(int)Sets the number of visible cards in the stack (Default: 3).
    setTranslationInterval(float)Sets the translation interval between cards in dp (Default: 8dp).
    setScaleInterval(float)Sets the scale interval between cards (Default: 0.95f).
    setMaxDegree(float)Sets the maximum rotation degree (Default: 20.0f).
    setDirections(List<Direction>)Sets allowed swipe directions (e.g., Direction.HORIZONTAL, Direction.VERTICAL, or Direction.FREEDOM).
    setSwipeThreshold(float)Sets the swipe threshold percentage (Default: 0.3f).
    setCanScrollHorizontal(boolean)Enables/disables horizontal scrolling.
    setCanScrollVertical(boolean)Enables/disables vertical scrolling.
    setSwipeableMethod(SwipeableMethod)Sets how cards can be swiped (e.g., SwipeableMethod.AutomaticAndManual, SwipeableMethod.Automatic, SwipeableMethod.Manual, or SwipeableMethod.None).