Skip to content

Commit

Permalink
selecteditem
Browse files Browse the repository at this point in the history
  • Loading branch information
toni-kustiana committed Sep 5, 2022
1 parent a185863 commit 8d5073c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,23 @@ class MainActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val list = mutableListOf("Abah", "Hezbi", "Ade", "Robert", "Jovan", "Ucup")
val list = mutableListOf(Name("Abah"),
Name("Hezbi"),
Name("Ade"),
Name("Robert"),
Name("Jovan"),
Name("Ucup"))

val chips = findViewById<SlidingChipsView<String>>(R.id.chips)
val chips = findViewById<SlidingChipsView<Name>>(R.id.chips)
chips.firstSelected = true
chips.delegate = object : SlidingChipsDelegate<String> {
override fun onSelected(item: String, position: Int) {
Toast.makeText(this@MainActivity, item, Toast.LENGTH_SHORT).show()
chips.delegate = object : SlidingChipsDelegate<Name> {
override fun onSelected(item: Name, position: Int) {
Toast.makeText(this@MainActivity, item.toString(), Toast.LENGTH_SHORT).show()
}

}
chips.items = list
chips.selectedItem = Name("Ade")

}
}
21 changes: 21 additions & 0 deletions app/src/main/java/id/co/edtslib/slidingchipsview/example/Name.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package id.co.edtslib.slidingchipsview.example

data class Name(
val short: String
) {
override fun toString(): String {
return short
}

override fun hashCode(): Int {
return super.hashCode()
}

override fun equals(other: Any?): Boolean {
if (other is Name) {
return short == other.short
}

return false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,22 @@ open class SlidingChipsView<T> : RecyclerView {
var delegate: SlidingChipsDelegate<T>? = null

private var prevSelectedIndex = -1

var selectedItem: T? = null
set(value) {
field = value
var i = 0
for (item in items) {
if (item?.equals(value) == true) {
break
}

i++
}

selectionIndex = i
}

var selectionIndex = 0
set(value) {
field = value
Expand Down Expand Up @@ -77,10 +93,8 @@ open class SlidingChipsView<T> : RecyclerView {
field = value

val list = mutableListOf<RecyclerData<T>>()
var i = 0
for (item in items) {
for ((i, item) in items.withIndex()) {
list.add(RecyclerData(item, firstSelected && i == 0))
i++
}

_adapter?.list = list
Expand Down Expand Up @@ -146,7 +160,7 @@ open class SlidingChipsView<T> : RecyclerView {

addItemDecoration(ItemDecoration(context.resources.getDimensionPixelSize(R.dimen.chip_dimen_8dp)))

_adapter = ChipAdapter<RecyclerData<T>>(textColor, textPadding, chipBackgroundColor, strokeColor,
_adapter = ChipAdapter(textColor, textPadding, chipBackgroundColor, strokeColor,
textStyle)
_adapter?.delegate = object : BaseRecyclerViewAdapterDelegate<RecyclerData<T>> {
override fun onClick(t: RecyclerData<T>, position: Int, holder: BaseViewHolder<RecyclerData<T>>?) {
Expand Down

0 comments on commit 8d5073c

Please sign in to comment.