-
Notifications
You must be signed in to change notification settings - Fork 51
/
GridActivity.kt
91 lines (81 loc) · 3.88 KB
/
GridActivity.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package smartrecycleradapter.feature
import android.os.Bundle
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import kotlinx.android.synthetic.main.activity_simple_item.recyclerView
import smartadapter.Position
import smartadapter.SmartRecyclerAdapter
import smartadapter.viewevent.dragdrop.AutoDragAndDropBinder
import smartadapter.viewevent.listener.OnClickEventListener
import smartrecycleradapter.extension.GridAutoLayoutManager
import smartrecycleradapter.models.MovieData
import smartrecycleradapter.models.MovieModel
import smartrecycleradapter.utils.AssetsUtils
import smartrecycleradapter.utils.showToast
import smartrecycleradapter.viewholder.HeaderViewHolder
import smartrecycleradapter.viewholder.ThumbViewHolder
/*
* Created by Manne Öhlund on 2019-08-11.
* Copyright (c) All rights reserved.
*/
class GridActivity : BaseSampleActivity() {
private val movieData: MovieData by lazy {
AssetsUtils.loadStyleFromAssets<MovieData>(this, "main-movie-data.json")
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
supportActionBar?.title = "Grid + Drag & Drop"
val gridAutoLayoutManager = GridAutoLayoutManager(this, 100)
gridAutoLayoutManager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
override fun getSpanSize(position: Position): Int {
return if ((recyclerView.adapter as SmartRecyclerAdapter).getItem(position) is String) gridAutoLayoutManager.spanCount else 1
}
}
val smartAdapter: SmartRecyclerAdapter = SmartRecyclerAdapter.empty()
.map(String::class, HeaderViewHolder::class)
.setViewTypeResolver { item, position ->
when (item) {
is MovieModel -> when (item.category) {
"coming-soon" -> ComingSoonThumbViewHolder::class
"action" -> ActionThumbViewHolder::class
"anim" -> AnimateThumbViewHolder::class
"sci-fi" -> SciFiThumbViewHolder::class
else -> null
}
else -> null
}
}
.setLayoutManager(gridAutoLayoutManager)
.add(OnClickEventListener(ThumbViewHolder::class) {
showToast("Movie ${it.position}")
})
.add(
AutoDragAndDropBinder(
longPressDragEnabled = true,
viewHolderTypes = listOf(
ComingSoonThumbViewHolder::class,
ActionThumbViewHolder::class,
AnimateThumbViewHolder::class,
SciFiThumbViewHolder::class
)
) {
supportActionBar?.subtitle =
"onItemMoved from ${it.viewHolder.adapterPosition} to ${it.targetViewHolder.adapterPosition}"
}
)
.into(recyclerView)
// Set adapter data
smartAdapter.addItem("Coming soon")
smartAdapter.addItems(movieData.categories.find { it.id == "coming-soon" }!!.items)
smartAdapter.addItem("Action")
smartAdapter.addItems(movieData.categories.find { it.id == "action" }!!.items)
smartAdapter.addItem("Animated")
smartAdapter.addItems(movieData.categories.find { it.id == "anim" }!!.items)
smartAdapter.addItem("Sci-Fi")
smartAdapter.addItems(movieData.categories.find { it.id == "sci-fi" }!!.items)
}
class ComingSoonThumbViewHolder(parentView: ViewGroup) : ThumbViewHolder(parentView)
class ActionThumbViewHolder(parentView: ViewGroup) : ThumbViewHolder(parentView)
class AnimateThumbViewHolder(parentView: ViewGroup) : ThumbViewHolder(parentView)
class SciFiThumbViewHolder(parentView: ViewGroup) : ThumbViewHolder(parentView)
}