Skip to content

Commit 2d4f22e

Browse files
committed
implemented setting up from menu programmatically, added view binder.
1 parent 84274c5 commit 2d4f22e

File tree

11 files changed

+114
-17
lines changed

11 files changed

+114
-17
lines changed

app/src/main/java/github/com/st235/swipetoactionlayout/ContactInfo.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ package github.com.st235.swipetoactionlayout
33
data class ContactInfo(
44
val name: String,
55
val position: String,
6-
val isOnline: Boolean
6+
val isOnline: Boolean,
7+
val isAuthor: Boolean = false
78
) {
89

910
val email: String

app/src/main/java/github/com/st235/swipetoactionlayout/ContactsAdapter.kt

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import android.view.View
55
import android.view.ViewGroup
66
import androidx.core.graphics.ColorUtils
77
import androidx.recyclerview.widget.RecyclerView
8+
import github.com.st235.lib_swipetoactionlayout.ActionBindHelper
89
import github.com.st235.lib_swipetoactionlayout.SwipeAction
910
import github.com.st235.lib_swipetoactionlayout.SwipeMenuListener
1011
import github.com.st235.swipetoactionlayout.identicon.IdenticonDrawable
@@ -18,6 +19,8 @@ class ContactsAdapter(
1819
private val onActionClicked: OnActionClicked
1920
): RecyclerView.Adapter<ContactsAdapter.ContactsViewHolder>() {
2021

22+
private val actionsBindHelper = ActionBindHelper()
23+
2124
fun remove(contact: ContactInfo) {
2225
val index = contacts.indexOf(contact)
2326
contacts.removeAt(index)
@@ -35,7 +38,9 @@ class ContactsAdapter(
3538
}
3639

3740
override fun onBindViewHolder(holder: ContactsViewHolder, position: Int) {
38-
holder.bind(contacts.get(position))
41+
val contact = contacts[position]
42+
actionsBindHelper.bind(contact.name, holder.swipeToActionLayout)
43+
holder.bind(contact)
3944
}
4045

4146
inner class ContactsViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView), SwipeMenuListener {
@@ -64,14 +69,23 @@ class ContactsAdapter(
6469
}
6570

6671
description.text = String.format("%s, %s", contact.position, contact.email)
72+
73+
if (contact.isAuthor) {
74+
isOnline.setImageResource(R.drawable.ic_favorite_black_18dp)
75+
swipeToActionLayout.setActionsRes(R.menu.special_menu)
76+
} else {
77+
isOnline.setImageResource(R.drawable.circle_green)
78+
swipeToActionLayout.setActionsRes(R.menu.swipe_to_action_menu)
79+
}
6780
}
6881

6982
override fun onClosed(view: View) {
7083
// empty on purpose
7184
}
7285

7386
override fun onOpened(view: View) {
74-
// empty on purpose
87+
val contact = contacts[adapterPosition]
88+
actionsBindHelper.closeOtherThan(contact.name)
7589
}
7690

7791
override fun onFullyOpened(view: View, quickAction: SwipeAction) {

app/src/main/java/github/com/st235/swipetoactionlayout/MainActivity.kt

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package github.com.st235.swipetoactionlayout
22

3+
import android.content.Intent
4+
import android.net.Uri
35
import android.os.Bundle
4-
import android.view.View
56
import android.widget.Toast
67
import androidx.appcompat.app.AppCompatActivity
78
import androidx.recyclerview.widget.LinearLayoutManager
8-
import github.com.st235.lib_swipetoactionlayout.SwipeAction
99
import kotlinx.android.synthetic.main.activity_main.*
1010

11+
1112
class MainActivity : AppCompatActivity() {
1213

1314
private val adapter = ContactsAdapter(
@@ -31,7 +32,7 @@ class MainActivity : AppCompatActivity() {
3132
ContactInfo("Landyn Martin", "Product Designer", true),
3233
ContactInfo("Madalyn Savage", "Software Engineer", true),
3334
ContactInfo("Cindy Moss", "Sales Manager", true),
34-
ContactInfo("Alexander Dadukin", "Cats Lover", true),
35+
ContactInfo("Alexander Dadukin", "Cats Lover", true, true),
3536
ContactInfo("Mathew Tapia", "Software Engineer", true),
3637
ContactInfo("Ayanna Shields", "Copyrighter", false)
3738
)
@@ -40,6 +41,7 @@ class MainActivity : AppCompatActivity() {
4041
R.id.call -> call(item)
4142
R.id.email -> email(item)
4243
R.id.delete -> remove(item)
44+
R.id.star -> openRepo()
4345
}
4446
}
4547

@@ -62,4 +64,11 @@ class MainActivity : AppCompatActivity() {
6264
private fun remove(item: ContactInfo) {
6365
adapter.remove(item)
6466
}
67+
68+
private fun openRepo() {
69+
val url = "https://github.com/st235/SwipeToActionLayout"
70+
val i = Intent(Intent.ACTION_VIEW)
71+
i.data = Uri.parse(url)
72+
startActivity(i)
73+
}
6574
}

app/src/main/res/drawable/circle_green.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@
44

55
<solid android:color="#00b248" />
66

7+
<stroke android:color="#fff" android:width="2dp" />
8+
79
</shape>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="18dp"
3+
android:height="18dp"
4+
android:viewportWidth="24"
5+
android:viewportHeight="24">
6+
<path
7+
android:pathData="M16.5,5c-1.54,0 -3.04,0.99 -3.56,2.36h-1.87C10.54,5.99 9.04,5 7.5,5 5.5,5 4,6.5 4,8.5c0,2.89 3.14,5.74 7.9,10.05l0.1,0.1 0.1,-0.1C16.86,14.24 20,11.39 20,8.5c0,-2 -1.5,-3.5 -3.5,-3.5z"
8+
android:fillColor="#f50057" />
9+
<path
10+
android:pathData="M16.5,3c-1.74,0 -3.41,0.81 -4.5,2.09C10.91,3.81 9.24,3 7.5,3 4.42,3 2,5.42 2,8.5c0,3.78 3.4,6.86 8.55,11.54L12,21.35l1.45,-1.32C18.6,15.36 22,12.28 22,8.5 22,5.42 19.58,3 16.5,3zM12.1,18.55l-0.1,0.1 -0.1,-0.1C7.14,14.24 4,11.39 4,8.5 4,6.5 5.5,5 7.5,5c1.54,0 3.04,0.99 3.57,2.36h1.87C13.46,5.99 14.96,5 16.5,5c2,0 3.5,1.5 3.5,3.5 0,2.89 -3.14,5.74 -7.9,10.05z"
11+
android:fillColor="#fff"/>
12+
</vector>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="18dp"
3+
android:height="18dp"
4+
android:viewportWidth="24"
5+
android:viewportHeight="24">
6+
<path
7+
android:pathData="M12,17.27l5.17,3.12c0.38,0.23 0.85,-0.11 0.75,-0.54l-1.37,-5.88 4.56,-3.95c0.33,-0.29 0.16,-0.84 -0.29,-0.88l-6.01,-0.51 -2.35,-5.54c-0.17,-0.41 -0.75,-0.41 -0.92,0L9.19,8.63l-6.01,0.51c-0.44,0.04 -0.62,0.59 -0.28,0.88l4.56,3.95 -1.37,5.88c-0.1,0.43 0.37,0.77 0.75,0.54L12,17.27z"
8+
android:fillColor="#000000"/>
9+
</vector>

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@
1919

2020
<androidx.appcompat.widget.AppCompatImageView
2121
android:id="@+id/isOnline"
22-
android:layout_width="8dp"
23-
android:layout_height="8dp"
24-
android:layout_margin="3dp"
22+
android:layout_width="14dp"
23+
android:layout_height="14dp"
2524
app:srcCompat="@drawable/circle_green"
2625
app:layout_constraintEnd_toEndOf="@id/icon"
2726
app:layout_constraintBottom_toBottomOf="@id/icon" />
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:tools="http://schemas.android.com/tools"
3+
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:android="http://schemas.android.com/apk/res/android">
4+
5+
<item
6+
android:id="@+id/star"
7+
android:icon="@drawable/ic_grade_black_18dp"
8+
android:background="@drawable/action_background"
9+
android:iconTint="#ffab00"
10+
tools:ignore="MenuTitle,UnusedAttribute" />
11+
</menu>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package github.com.st235.lib_swipetoactionlayout
2+
3+
import java.util.*
4+
5+
class ActionBindHelper {
6+
7+
private val actions: MutableMap<String, SwipeToActionLayout> = Collections.synchronizedMap(mutableMapOf())
8+
9+
fun bind(id: String, swipeToActionLayout: SwipeToActionLayout) {
10+
val oldId = findWithView(swipeToActionLayout)
11+
oldId?.let { actions.remove(it) }
12+
actions[id] = swipeToActionLayout
13+
}
14+
15+
private fun findWithView(swipeToActionLayout: SwipeToActionLayout): String? {
16+
for ((id, actionLayout) in actions.entries) {
17+
if (actionLayout == swipeToActionLayout) {
18+
return id
19+
}
20+
}
21+
22+
return null
23+
}
24+
25+
fun closeOtherThan(currentId: String) {
26+
for ((id, actionLayout) in actions.entries) {
27+
if (id == currentId) {
28+
continue
29+
}
30+
31+
actionLayout.close()
32+
}
33+
}
34+
35+
}

lib-swipetoactionlayout/src/main/java/github/com/st235/lib_swipetoactionlayout/SwipeToActionLayout.kt

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import android.content.Context
44
import android.util.AttributeSet
55
import android.util.Log
66
import android.view.*
7+
import androidx.annotation.MenuRes
78
import androidx.core.view.ViewCompat
89
import androidx.customview.widget.ViewDragHelper
910
import github.com.st235.lib_swipetoactionlayout.behaviour.BehaviourDelegate
@@ -120,12 +121,6 @@ class SwipeToActionLayout @JvmOverloads constructor(
120121
QuickActionsStates.FULL_OPENED -> {
121122
menuListener?.onFullyOpened(this, actions.last())
122123
}
123-
QuickActionsStates.OPENED -> {
124-
menuListener?.onOpened(this)
125-
}
126-
QuickActionsStates.CLOSED -> {
127-
menuListener?.onClosed(this)
128-
}
129124
}
130125

131126
}
@@ -137,12 +132,23 @@ class SwipeToActionLayout @JvmOverloads constructor(
137132
performHapticFeedback(HapticFeedbackConstants.CLOCK_TICK)
138133
}
139134
}
140-
else -> {
135+
QuickActionsStates.OPENED -> {
136+
menuListener?.onOpened(this)
137+
}
138+
QuickActionsStates.CLOSED -> {
139+
menuListener?.onClosed(this)
141140
}
142141
}
143142
}
144143
}
145144

145+
fun setActionsRes(@MenuRes menuRes: Int) {
146+
val barParser = XmlMenuParser(context)
147+
val items = barParser.inflate(menuRes)
148+
actions = items
149+
reloadActions()
150+
}
151+
146152
fun close() {
147153
transitionToState(QuickActionsStates.CLOSED)
148154
}

0 commit comments

Comments
 (0)