Skip to content

Commit 0991f01

Browse files
authored
Merge pull request #5 from Yalantis/hotfix/Issue_4
Add lost functionality and fix issue #4
2 parents 2fbd393 + 7714412 commit 0991f01

File tree

8 files changed

+402
-144
lines changed

8 files changed

+402
-144
lines changed

app/src/main/java/com/yalantis/kolodaandroid/MainActivity.kt

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ package com.yalantis.kolodaandroid
22

33
import android.support.v7.app.AppCompatActivity
44
import android.os.Bundle
5-
import android.widget.Toast
65
import com.yalantis.library.KolodaListener
76
import kotlinx.android.synthetic.main.activity_main.*
87
import android.annotation.SuppressLint
8+
import android.view.Menu
9+
import android.view.MenuItem
910
import android.view.ViewTreeObserver
11+
import com.yalantis.kolodaandroid.R.id.actionReload
1012

1113

1214
class MainActivity : AppCompatActivity() {
@@ -27,8 +29,6 @@ class MainActivity : AppCompatActivity() {
2729
@SuppressLint("NewApi")
2830
override fun onGlobalLayout() {
2931
//now we can retrieve the width and height
30-
val width = activityMain.width
31-
val height = activityMain.height
3232
//...
3333
//do whatever you want with them
3434
//...
@@ -55,19 +55,19 @@ class MainActivity : AppCompatActivity() {
5555
internal var cardsSwiped = 0
5656

5757
override fun onNewTopCard(position: Int) {
58-
Toast.makeText(this@MainActivity, "On new top card", Toast.LENGTH_LONG).show()
58+
//todo realize your logic
5959
}
6060

6161
override fun onCardSwipedLeft(position: Int) {
62-
Toast.makeText(this@MainActivity, "On card swiped left", Toast.LENGTH_LONG).show()
62+
//todo realize your logic
6363
}
6464

6565
override fun onCardSwipedRight(position: Int) {
66-
Toast.makeText(this@MainActivity, "On card swiped right", Toast.LENGTH_LONG).show()
66+
//todo realize your logic
6767
}
6868

6969
override fun onEmptyDeck() {
70-
Toast.makeText(this@MainActivity, "On empty deck", Toast.LENGTH_LONG).show()
70+
//todo realize your logic
7171
}
7272
}
7373
}
@@ -77,18 +77,18 @@ class MainActivity : AppCompatActivity() {
7777
*/
7878
private fun fillData() {
7979
val data = arrayOf(R.drawable.cupcacke,
80-
R.drawable.donut,
81-
R.drawable.eclair,
82-
R.drawable.froyo,
83-
R.drawable.gingerbread,
84-
R.drawable.honeycomb,
85-
R.drawable.ice_cream_sandwich,
86-
R.drawable.jelly_bean,
87-
R.drawable.kitkat,
88-
R.drawable.lollipop,
89-
R.drawable.marshmallow,
90-
R.drawable.nougat,
91-
R.drawable.oreo)
80+
R.drawable.donut,
81+
R.drawable.eclair,
82+
R.drawable.froyo,
83+
R.drawable.gingerbread,
84+
R.drawable.honeycomb,
85+
R.drawable.ice_cream_sandwich,
86+
R.drawable.jelly_bean,
87+
R.drawable.kitkat,
88+
R.drawable.lollipop,
89+
R.drawable.marshmallow,
90+
R.drawable.nougat,
91+
R.drawable.oreo)
9292
adapter = KolodaSampleAdapter(this, data.toList())
9393
koloda.adapter = adapter
9494
koloda.isNeedCircleLoading = true
@@ -98,4 +98,16 @@ class MainActivity : AppCompatActivity() {
9898
dislike.setOnClickListener { koloda.onClickLeft() }
9999
like.setOnClickListener { koloda.onClickRight() }
100100
}
101+
102+
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
103+
menuInflater.inflate(R.menu.menu_reload, menu)
104+
return true
105+
}
106+
107+
override fun onOptionsItemSelected(item: MenuItem): Boolean {
108+
when (item.itemId) {
109+
actionReload -> { koloda.reloadPreviousCard() }
110+
}
111+
return super.onOptionsItemSelected(item)
112+
}
101113
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:viewportHeight="24.0" android:viewportWidth="24.0" android:width="24dp">
3+
<path android:fillColor="#FFFFFF" android:pathData="M12,5V1L7,6l5,5V7c3.31,0 6,2.69 6,6s-2.69,6 -6,6 -6,-2.69 -6,-6H4c0,4.42 3.58,8 8,8s8,-3.58 8,-8 -3.58,-8 -8,-8z"/>
4+
</vector>

app/src/main/res/layout/activity_main.xml

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,43 @@
22
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:app="http://schemas.android.com/apk/res-auto"
44
android:id="@+id/activityMain"
5-
android:background="@color/colorPrimary"
6-
android:clipToPadding="false"
75
android:layout_width="match_parent"
8-
android:layout_height="match_parent">
6+
android:layout_height="match_parent"
7+
android:background="@color/colorPrimary"
8+
android:clipToPadding="false">
99

1010
<com.yalantis.library.Koloda
1111
android:id="@+id/koloda"
12-
android:layout_height="match_parent"
1312
android:layout_width="match_parent"
14-
android:elevation="1dp"
13+
android:layout_height="match_parent"
1514
app:koloda_card_layout="@layout/item_koloda" />
1615

1716
<LinearLayout
1817
android:id="@+id/buttonsLayout"
1918
android:layout_width="wrap_content"
2019
android:layout_height="wrap_content"
21-
android:layout_centerHorizontal="true"
2220
android:layout_alignParentBottom="true"
21+
android:layout_centerHorizontal="true"
2322
android:layout_marginBottom="16dp"
2423
android:orientation="horizontal">
2524

2625
<ImageButton
2726
android:id="@+id/dislike"
2827
android:layout_width="wrap_content"
2928
android:layout_height="wrap_content"
30-
android:src="@drawable/ic_dislike"
3129
android:layout_weight="1"
32-
android:background="@android:color/transparent"/>
30+
android:background="@android:color/transparent"
31+
android:src="@drawable/ic_dislike" />
3332

3433

3534
<ImageButton
3635
android:id="@+id/like"
3736
android:layout_width="wrap_content"
3837
android:layout_height="wrap_content"
39-
android:src="@drawable/ic_like"
40-
android:layout_weight="0.5"
4138
android:layout_marginLeft="40dp"
42-
android:background="@android:color/transparent"/>
39+
android:layout_weight="0.5"
40+
android:background="@android:color/transparent"
41+
android:src="@drawable/ic_like" />
4342

4443
</LinearLayout>
4544

app/src/main/res/menu/menu_reload.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<menu xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto">
4+
5+
<item
6+
android:id="@+id/actionReload"
7+
android:title=""
8+
android:icon="@drawable/ic_reload"
9+
app:showAsAction="always"/>
10+
11+
</menu>
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package com.yalantis.library
2+
3+
import android.content.Context
4+
import android.os.Build
5+
import android.support.annotation.DrawableRes
6+
import android.support.v4.content.ContextCompat
7+
import android.util.AttributeSet
8+
import android.view.View
9+
import android.widget.FrameLayout
10+
import android.widget.ImageView
11+
12+
/**
13+
* Created by anna on 1/2/18.
14+
*/
15+
class CardLayout
16+
@JvmOverloads constructor(context: Context, attrs: AttributeSet? = null,
17+
defStyleAttr: Int = 0, var cardParams: CardParams) : FrameLayout(context, attrs, defStyleAttr) {
18+
19+
private var rightImageView: ImageView? = null
20+
private var leftImageView: ImageView? = null
21+
private var cardView: View? = null
22+
var layoutAlpha = 1f
23+
set(value) {
24+
field = value
25+
cardView?.alpha = value
26+
}
27+
28+
29+
fun addOverlays(cardView: View?, @DrawableRes rightOverlayImage: Int?,
30+
@DrawableRes leftOverlayImage: Int?) {
31+
this.cardView = cardView
32+
addView(cardView)
33+
34+
leftOverlayImage?.let {
35+
leftImageView = ImageView(context)
36+
leftImageView?.setImageDrawable(ContextCompat.getDrawable(context, it))
37+
leftImageView?.alpha = 0f
38+
addView(leftImageView)
39+
}
40+
41+
rightOverlayImage?.let {
42+
rightImageView = ImageView(context)
43+
rightImageView?.setImageDrawable(ContextCompat.getDrawable(context, it))
44+
rightImageView?.alpha = 0f
45+
addView(rightImageView)
46+
}
47+
48+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
49+
rightImageView?.translationZ = SMALL_ELEVATION
50+
leftImageView?.translationZ = SMALL_ELEVATION
51+
cardView?.translationZ = NO_ELEVATION
52+
}
53+
}
54+
55+
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
56+
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
57+
val layoutParamsCardView = cardView?.layoutParams as FrameLayout.LayoutParams
58+
59+
cardView?.alpha = layoutAlpha
60+
cardParams.onParamsMeasure(layoutParamsCardView)
61+
62+
rightImageView?.layoutParams?.let {
63+
(rightImageView?.layoutParams as FrameLayout.LayoutParams).apply {
64+
width = layoutParamsCardView.width
65+
height = layoutParamsCardView.height
66+
gravity = layoutParamsCardView.gravity
67+
68+
setMargins(layoutParamsCardView.leftMargin, layoutParamsCardView.topMargin,
69+
layoutParamsCardView.rightMargin, layoutParamsCardView.bottomMargin)
70+
}
71+
rightImageView?.setPadding(cardView?.paddingLeft ?: 0, cardView?.paddingTop ?: 0,
72+
cardView?.paddingRight ?: 0, cardView?.paddingBottom ?: 0)
73+
}
74+
75+
leftImageView?.layoutParams?.let {
76+
(leftImageView?.layoutParams as FrameLayout.LayoutParams).apply {
77+
width = layoutParamsCardView.width
78+
height = layoutParamsCardView.height
79+
gravity = layoutParamsCardView.gravity
80+
setMargins(layoutParamsCardView.leftMargin, layoutParamsCardView.topMargin,
81+
layoutParamsCardView.rightMargin, layoutParamsCardView.bottomMargin)
82+
}
83+
84+
leftImageView?.setPadding(cardView?.paddingLeft ?: 0, cardView?.paddingTop ?: 0,
85+
cardView?.paddingRight ?: 0, cardView?.paddingBottom ?: 0)
86+
}
87+
}
88+
89+
internal fun changeRightOverlayAlpha(progress: Float) {
90+
rightImageView?.alpha = progress
91+
}
92+
93+
internal fun changeLeftOverlayAlpha(progress: Float) {
94+
leftImageView?.alpha = Math.abs(progress)
95+
}
96+
97+
interface CardParams {
98+
fun onParamsMeasure(layoutParamsCardView: LayoutParams)
99+
}
100+
101+
companion object {
102+
private const val NO_ELEVATION = 0F
103+
private const val SMALL_ELEVATION = 10F
104+
}
105+
106+
}

0 commit comments

Comments
 (0)