Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

We fixed the problem of always swiping right in CardStackView swipe() function #361

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
[![Download](https://api.bintray.com/packages/yuyakaido/maven/CardStackView/images/download.svg)](https://bintray.com/yuyakaido/maven/CardStackView/_latestVersion)
[![AndroidArsenal](https://img.shields.io/badge/Android%20Arsenal-CardStackView-blue.svg?style=flat)](https://android-arsenal.com/details/1/6075)
[![CircleCI](https://circleci.com/gh/yuyakaido/CardStackView.svg?style=svg)](https://circleci.com/gh/yuyakaido/CardStackView)
</br>
<a href="https://www.buymeacoffee.com/kzlsn" target="_blank"><img src="https://www.buymeacoffee.com/assets/img/custom_images/yellow_img.png" alt="Buy Me A Coffee" style="height: 41px !important;width: 174px !important;box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;-webkit-box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;" ></a>

# Overview

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,7 @@ private void initialize() {
setOverScrollMode(RecyclerView.OVER_SCROLL_NEVER);
}

}
public CardStackLayoutManager getCardStackLayoutManager(){
return (CardStackLayoutManager) getLayoutManager();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package com.yuyakaido.android.cardstackview.sample
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.animation.AccelerateInterpolator
import android.view.animation.DecelerateInterpolator
import android.view.animation.LinearInterpolator
import android.widget.TextView
import androidx.appcompat.app.ActionBarDrawerToggle
Expand Down Expand Up @@ -76,7 +74,14 @@ class MainActivity : AppCompatActivity(), CardStackListener {
setSupportActionBar(toolbar)

// DrawerLayout
val actionBarDrawerToggle = ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open_drawer, R.string.close_drawer)
val actionBarDrawerToggle =
ActionBarDrawerToggle(
this,
drawerLayout,
toolbar,
R.string.open_drawer,
R.string.close_drawer
)
actionBarDrawerToggle.syncState()
drawerLayout.addDrawerListener(actionBarDrawerToggle)

Expand Down Expand Up @@ -104,38 +109,30 @@ class MainActivity : AppCompatActivity(), CardStackListener {
private fun setupButton() {
val skip = findViewById<View>(R.id.skip_button)
skip.setOnClickListener {
val setting = SwipeAnimationSetting.Builder()
.setDirection(Direction.Left)
.setDuration(Duration.Normal.duration)
.setInterpolator(AccelerateInterpolator())
.build()
manager.setSwipeAnimationSetting(setting)
updateCardStack(Direction.Left)
cardStackView.swipe()
}

val rewind = findViewById<View>(R.id.rewind_button)
rewind.setOnClickListener {
val setting = RewindAnimationSetting.Builder()
.setDirection(Direction.Bottom)
.setDuration(Duration.Normal.duration)
.setInterpolator(DecelerateInterpolator())
.build()
manager.setRewindAnimationSetting(setting)
updateCardStack(Direction.Bottom)
cardStackView.rewind()
}

val like = findViewById<View>(R.id.like_button)
like.setOnClickListener {
val setting = SwipeAnimationSetting.Builder()
.setDirection(Direction.Right)
.setDuration(Duration.Normal.duration)
.setInterpolator(AccelerateInterpolator())
.build()
manager.setSwipeAnimationSetting(setting)
updateCardStack(Direction.Right)
cardStackView.swipe()
}
}

private fun updateCardStack(direction: Direction) {
val cardStackLayoutManager = cardStackView.getCardStackLayoutManager()
val swipeAnimationSetting = SwipeAnimationSetting.Builder().setDirection(direction).build()
cardStackLayoutManager.setSwipeAnimationSetting(swipeAnimationSetting)
cardStackView.setLayoutManager(cardStackLayoutManager)
}

private fun initialize() {
manager.setStackFrom(StackFrom.None)
manager.setVisibleCount(3)
Expand Down Expand Up @@ -177,12 +174,13 @@ class MainActivity : AppCompatActivity(), CardStackListener {

private fun addFirst(size: Int) {
val old = adapter.getSpots()
val new = mutableListOf<Spot>().apply {
addAll(old)
for (i in 0 until size) {
add(manager.topPosition, createSpot())
}
}
val new =
mutableListOf<Spot>().apply {
addAll(old)
for (i in 0 until size) {
add(manager.topPosition, createSpot())
}
}
val callback = SpotDiffCallback(old, new)
val result = DiffUtil.calculateDiff(callback)
adapter.setSpots(new)
Expand All @@ -191,10 +189,11 @@ class MainActivity : AppCompatActivity(), CardStackListener {

private fun addLast(size: Int) {
val old = adapter.getSpots()
val new = mutableListOf<Spot>().apply {
addAll(old)
addAll(List(size) { createSpot() })
}
val new =
mutableListOf<Spot>().apply {
addAll(old)
addAll(List(size) { createSpot() })
}
val callback = SpotDiffCallback(old, new)
val result = DiffUtil.calculateDiff(callback)
adapter.setSpots(new)
Expand All @@ -207,12 +206,13 @@ class MainActivity : AppCompatActivity(), CardStackListener {
}

val old = adapter.getSpots()
val new = mutableListOf<Spot>().apply {
addAll(old)
for (i in 0 until size) {
removeAt(manager.topPosition)
}
}
val new =
mutableListOf<Spot>().apply {
addAll(old)
for (i in 0 until size) {
removeAt(manager.topPosition)
}
}
val callback = SpotDiffCallback(old, new)
val result = DiffUtil.calculateDiff(callback)
adapter.setSpots(new)
Expand All @@ -225,12 +225,13 @@ class MainActivity : AppCompatActivity(), CardStackListener {
}

val old = adapter.getSpots()
val new = mutableListOf<Spot>().apply {
addAll(old)
for (i in 0 until size) {
removeAt(this.size - 1)
}
}
val new =
mutableListOf<Spot>().apply {
addAll(old)
for (i in 0 until size) {
removeAt(this.size - 1)
}
}
val callback = SpotDiffCallback(old, new)
val result = DiffUtil.calculateDiff(callback)
adapter.setSpots(new)
Expand All @@ -239,24 +240,26 @@ class MainActivity : AppCompatActivity(), CardStackListener {

private fun replace() {
val old = adapter.getSpots()
val new = mutableListOf<Spot>().apply {
addAll(old)
removeAt(manager.topPosition)
add(manager.topPosition, createSpot())
}
val new =
mutableListOf<Spot>().apply {
addAll(old)
removeAt(manager.topPosition)
add(manager.topPosition, createSpot())
}
adapter.setSpots(new)
adapter.notifyItemChanged(manager.topPosition)
}

private fun swap() {
val old = adapter.getSpots()
val new = mutableListOf<Spot>().apply {
addAll(old)
val first = removeAt(manager.topPosition)
val last = removeAt(this.size - 1)
add(manager.topPosition, last)
add(first)
}
val new =
mutableListOf<Spot>().apply {
addAll(old)
val first = removeAt(manager.topPosition)
val last = removeAt(this.size - 1)
add(manager.topPosition, last)
add(first)
}
val callback = SpotDiffCallback(old, new)
val result = DiffUtil.calculateDiff(callback)
adapter.setSpots(new)
Expand All @@ -273,17 +276,76 @@ class MainActivity : AppCompatActivity(), CardStackListener {

private fun createSpots(): List<Spot> {
val spots = ArrayList<Spot>()
spots.add(Spot(name = "Yasaka Shrine", city = "Kyoto", url = "https://source.unsplash.com/Xq1ntWruZQI/600x800"))
spots.add(Spot(name = "Fushimi Inari Shrine", city = "Kyoto", url = "https://source.unsplash.com/NYyCqdBOKwc/600x800"))
spots.add(Spot(name = "Bamboo Forest", city = "Kyoto", url = "https://source.unsplash.com/buF62ewDLcQ/600x800"))
spots.add(Spot(name = "Brooklyn Bridge", city = "New York", url = "https://source.unsplash.com/THozNzxEP3g/600x800"))
spots.add(Spot(name = "Empire State Building", city = "New York", url = "https://source.unsplash.com/USrZRcRS2Lw/600x800"))
spots.add(Spot(name = "The statue of Liberty", city = "New York", url = "https://source.unsplash.com/PeFk7fzxTdk/600x800"))
spots.add(Spot(name = "Louvre Museum", city = "Paris", url = "https://source.unsplash.com/LrMWHKqilUw/600x800"))
spots.add(Spot(name = "Eiffel Tower", city = "Paris", url = "https://source.unsplash.com/HN-5Z6AmxrM/600x800"))
spots.add(Spot(name = "Big Ben", city = "London", url = "https://source.unsplash.com/CdVAUADdqEc/600x800"))
spots.add(Spot(name = "Great Wall of China", city = "China", url = "https://source.unsplash.com/AWh9C-QjhE4/600x800"))
spots.add(
Spot(
name = "Yasaka Shrine",
city = "Kyoto",
url = "https://source.unsplash.com/Xq1ntWruZQI/600x800"
)
)
spots.add(
Spot(
name = "Fushimi Inari Shrine",
city = "Kyoto",
url = "https://source.unsplash.com/NYyCqdBOKwc/600x800"
)
)
spots.add(
Spot(
name = "Bamboo Forest",
city = "Kyoto",
url = "https://source.unsplash.com/buF62ewDLcQ/600x800"
)
)
spots.add(
Spot(
name = "Brooklyn Bridge",
city = "New York",
url = "https://source.unsplash.com/THozNzxEP3g/600x800"
)
)
spots.add(
Spot(
name = "Empire State Building",
city = "New York",
url = "https://source.unsplash.com/USrZRcRS2Lw/600x800"
)
)
spots.add(
Spot(
name = "The statue of Liberty",
city = "New York",
url = "https://source.unsplash.com/PeFk7fzxTdk/600x800"
)
)
spots.add(
Spot(
name = "Louvre Museum",
city = "Paris",
url = "https://source.unsplash.com/LrMWHKqilUw/600x800"
)
)
spots.add(
Spot(
name = "Eiffel Tower",
city = "Paris",
url = "https://source.unsplash.com/HN-5Z6AmxrM/600x800"
)
)
spots.add(
Spot(
name = "Big Ben",
city = "London",
url = "https://source.unsplash.com/CdVAUADdqEc/600x800"
)
)
spots.add(
Spot(
name = "Great Wall of China",
city = "China",
url = "https://source.unsplash.com/AWh9C-QjhE4/600x800"
)
)
return spots
}

}