Skip to content

Commit 0b1e64d

Browse files
committed
Demo bug fixed
1 parent 55db742 commit 0b1e64d

1 file changed

Lines changed: 128 additions & 66 deletions

File tree

  • sample/src/main/java/com/yuyakaido/android/cardstackview/sample

sample/src/main/java/com/yuyakaido/android/cardstackview/sample/MainActivity.kt

Lines changed: 128 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package com.yuyakaido.android.cardstackview.sample
33
import android.os.Bundle
44
import android.util.Log
55
import android.view.View
6-
import android.view.animation.AccelerateInterpolator
7-
import android.view.animation.DecelerateInterpolator
86
import android.view.animation.LinearInterpolator
97
import android.widget.TextView
108
import androidx.appcompat.app.ActionBarDrawerToggle
@@ -76,7 +74,14 @@ class MainActivity : AppCompatActivity(), CardStackListener {
7674
setSupportActionBar(toolbar)
7775

7876
// DrawerLayout
79-
val actionBarDrawerToggle = ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open_drawer, R.string.close_drawer)
77+
val actionBarDrawerToggle =
78+
ActionBarDrawerToggle(
79+
this,
80+
drawerLayout,
81+
toolbar,
82+
R.string.open_drawer,
83+
R.string.close_drawer
84+
)
8085
actionBarDrawerToggle.syncState()
8186
drawerLayout.addDrawerListener(actionBarDrawerToggle)
8287

@@ -104,38 +109,30 @@ class MainActivity : AppCompatActivity(), CardStackListener {
104109
private fun setupButton() {
105110
val skip = findViewById<View>(R.id.skip_button)
106111
skip.setOnClickListener {
107-
val setting = SwipeAnimationSetting.Builder()
108-
.setDirection(Direction.Left)
109-
.setDuration(Duration.Normal.duration)
110-
.setInterpolator(AccelerateInterpolator())
111-
.build()
112-
manager.setSwipeAnimationSetting(setting)
112+
updateCardStack(Direction.Left)
113113
cardStackView.swipe()
114114
}
115115

116116
val rewind = findViewById<View>(R.id.rewind_button)
117117
rewind.setOnClickListener {
118-
val setting = RewindAnimationSetting.Builder()
119-
.setDirection(Direction.Bottom)
120-
.setDuration(Duration.Normal.duration)
121-
.setInterpolator(DecelerateInterpolator())
122-
.build()
123-
manager.setRewindAnimationSetting(setting)
118+
updateCardStack(Direction.Bottom)
124119
cardStackView.rewind()
125120
}
126121

127122
val like = findViewById<View>(R.id.like_button)
128123
like.setOnClickListener {
129-
val setting = SwipeAnimationSetting.Builder()
130-
.setDirection(Direction.Right)
131-
.setDuration(Duration.Normal.duration)
132-
.setInterpolator(AccelerateInterpolator())
133-
.build()
134-
manager.setSwipeAnimationSetting(setting)
124+
updateCardStack(Direction.Right)
135125
cardStackView.swipe()
136126
}
137127
}
138128

129+
private fun updateCardStack(direction: Direction) {
130+
val cardStackLayoutManager = cardStackView.getCardStackLayoutManager()
131+
val swipeAnimationSetting = SwipeAnimationSetting.Builder().setDirection(direction).build()
132+
cardStackLayoutManager.setSwipeAnimationSetting(swipeAnimationSetting)
133+
cardStackView.setLayoutManager(cardStackLayoutManager)
134+
}
135+
139136
private fun initialize() {
140137
manager.setStackFrom(StackFrom.None)
141138
manager.setVisibleCount(3)
@@ -177,12 +174,13 @@ class MainActivity : AppCompatActivity(), CardStackListener {
177174

178175
private fun addFirst(size: Int) {
179176
val old = adapter.getSpots()
180-
val new = mutableListOf<Spot>().apply {
181-
addAll(old)
182-
for (i in 0 until size) {
183-
add(manager.topPosition, createSpot())
184-
}
185-
}
177+
val new =
178+
mutableListOf<Spot>().apply {
179+
addAll(old)
180+
for (i in 0 until size) {
181+
add(manager.topPosition, createSpot())
182+
}
183+
}
186184
val callback = SpotDiffCallback(old, new)
187185
val result = DiffUtil.calculateDiff(callback)
188186
adapter.setSpots(new)
@@ -191,10 +189,11 @@ class MainActivity : AppCompatActivity(), CardStackListener {
191189

192190
private fun addLast(size: Int) {
193191
val old = adapter.getSpots()
194-
val new = mutableListOf<Spot>().apply {
195-
addAll(old)
196-
addAll(List(size) { createSpot() })
197-
}
192+
val new =
193+
mutableListOf<Spot>().apply {
194+
addAll(old)
195+
addAll(List(size) { createSpot() })
196+
}
198197
val callback = SpotDiffCallback(old, new)
199198
val result = DiffUtil.calculateDiff(callback)
200199
adapter.setSpots(new)
@@ -207,12 +206,13 @@ class MainActivity : AppCompatActivity(), CardStackListener {
207206
}
208207

209208
val old = adapter.getSpots()
210-
val new = mutableListOf<Spot>().apply {
211-
addAll(old)
212-
for (i in 0 until size) {
213-
removeAt(manager.topPosition)
214-
}
215-
}
209+
val new =
210+
mutableListOf<Spot>().apply {
211+
addAll(old)
212+
for (i in 0 until size) {
213+
removeAt(manager.topPosition)
214+
}
215+
}
216216
val callback = SpotDiffCallback(old, new)
217217
val result = DiffUtil.calculateDiff(callback)
218218
adapter.setSpots(new)
@@ -225,12 +225,13 @@ class MainActivity : AppCompatActivity(), CardStackListener {
225225
}
226226

227227
val old = adapter.getSpots()
228-
val new = mutableListOf<Spot>().apply {
229-
addAll(old)
230-
for (i in 0 until size) {
231-
removeAt(this.size - 1)
232-
}
233-
}
228+
val new =
229+
mutableListOf<Spot>().apply {
230+
addAll(old)
231+
for (i in 0 until size) {
232+
removeAt(this.size - 1)
233+
}
234+
}
234235
val callback = SpotDiffCallback(old, new)
235236
val result = DiffUtil.calculateDiff(callback)
236237
adapter.setSpots(new)
@@ -239,24 +240,26 @@ class MainActivity : AppCompatActivity(), CardStackListener {
239240

240241
private fun replace() {
241242
val old = adapter.getSpots()
242-
val new = mutableListOf<Spot>().apply {
243-
addAll(old)
244-
removeAt(manager.topPosition)
245-
add(manager.topPosition, createSpot())
246-
}
243+
val new =
244+
mutableListOf<Spot>().apply {
245+
addAll(old)
246+
removeAt(manager.topPosition)
247+
add(manager.topPosition, createSpot())
248+
}
247249
adapter.setSpots(new)
248250
adapter.notifyItemChanged(manager.topPosition)
249251
}
250252

251253
private fun swap() {
252254
val old = adapter.getSpots()
253-
val new = mutableListOf<Spot>().apply {
254-
addAll(old)
255-
val first = removeAt(manager.topPosition)
256-
val last = removeAt(this.size - 1)
257-
add(manager.topPosition, last)
258-
add(first)
259-
}
255+
val new =
256+
mutableListOf<Spot>().apply {
257+
addAll(old)
258+
val first = removeAt(manager.topPosition)
259+
val last = removeAt(this.size - 1)
260+
add(manager.topPosition, last)
261+
add(first)
262+
}
260263
val callback = SpotDiffCallback(old, new)
261264
val result = DiffUtil.calculateDiff(callback)
262265
adapter.setSpots(new)
@@ -273,17 +276,76 @@ class MainActivity : AppCompatActivity(), CardStackListener {
273276

274277
private fun createSpots(): List<Spot> {
275278
val spots = ArrayList<Spot>()
276-
spots.add(Spot(name = "Yasaka Shrine", city = "Kyoto", url = "https://source.unsplash.com/Xq1ntWruZQI/600x800"))
277-
spots.add(Spot(name = "Fushimi Inari Shrine", city = "Kyoto", url = "https://source.unsplash.com/NYyCqdBOKwc/600x800"))
278-
spots.add(Spot(name = "Bamboo Forest", city = "Kyoto", url = "https://source.unsplash.com/buF62ewDLcQ/600x800"))
279-
spots.add(Spot(name = "Brooklyn Bridge", city = "New York", url = "https://source.unsplash.com/THozNzxEP3g/600x800"))
280-
spots.add(Spot(name = "Empire State Building", city = "New York", url = "https://source.unsplash.com/USrZRcRS2Lw/600x800"))
281-
spots.add(Spot(name = "The statue of Liberty", city = "New York", url = "https://source.unsplash.com/PeFk7fzxTdk/600x800"))
282-
spots.add(Spot(name = "Louvre Museum", city = "Paris", url = "https://source.unsplash.com/LrMWHKqilUw/600x800"))
283-
spots.add(Spot(name = "Eiffel Tower", city = "Paris", url = "https://source.unsplash.com/HN-5Z6AmxrM/600x800"))
284-
spots.add(Spot(name = "Big Ben", city = "London", url = "https://source.unsplash.com/CdVAUADdqEc/600x800"))
285-
spots.add(Spot(name = "Great Wall of China", city = "China", url = "https://source.unsplash.com/AWh9C-QjhE4/600x800"))
279+
spots.add(
280+
Spot(
281+
name = "Yasaka Shrine",
282+
city = "Kyoto",
283+
url = "https://source.unsplash.com/Xq1ntWruZQI/600x800"
284+
)
285+
)
286+
spots.add(
287+
Spot(
288+
name = "Fushimi Inari Shrine",
289+
city = "Kyoto",
290+
url = "https://source.unsplash.com/NYyCqdBOKwc/600x800"
291+
)
292+
)
293+
spots.add(
294+
Spot(
295+
name = "Bamboo Forest",
296+
city = "Kyoto",
297+
url = "https://source.unsplash.com/buF62ewDLcQ/600x800"
298+
)
299+
)
300+
spots.add(
301+
Spot(
302+
name = "Brooklyn Bridge",
303+
city = "New York",
304+
url = "https://source.unsplash.com/THozNzxEP3g/600x800"
305+
)
306+
)
307+
spots.add(
308+
Spot(
309+
name = "Empire State Building",
310+
city = "New York",
311+
url = "https://source.unsplash.com/USrZRcRS2Lw/600x800"
312+
)
313+
)
314+
spots.add(
315+
Spot(
316+
name = "The statue of Liberty",
317+
city = "New York",
318+
url = "https://source.unsplash.com/PeFk7fzxTdk/600x800"
319+
)
320+
)
321+
spots.add(
322+
Spot(
323+
name = "Louvre Museum",
324+
city = "Paris",
325+
url = "https://source.unsplash.com/LrMWHKqilUw/600x800"
326+
)
327+
)
328+
spots.add(
329+
Spot(
330+
name = "Eiffel Tower",
331+
city = "Paris",
332+
url = "https://source.unsplash.com/HN-5Z6AmxrM/600x800"
333+
)
334+
)
335+
spots.add(
336+
Spot(
337+
name = "Big Ben",
338+
city = "London",
339+
url = "https://source.unsplash.com/CdVAUADdqEc/600x800"
340+
)
341+
)
342+
spots.add(
343+
Spot(
344+
name = "Great Wall of China",
345+
city = "China",
346+
url = "https://source.unsplash.com/AWh9C-QjhE4/600x800"
347+
)
348+
)
286349
return spots
287350
}
288-
289351
}

0 commit comments

Comments
 (0)