-
Notifications
You must be signed in to change notification settings - Fork 51
/
DiffUtilActivity.kt
76 lines (64 loc) · 2.44 KB
/
DiffUtilActivity.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
package smartrecycleradapter.feature
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import kotlinx.android.synthetic.main.activity_simple_item.recyclerView
import smartadapter.SmartRecyclerAdapter
import smartadapter.diffutil.DiffUtilExtension
import smartadapter.diffutil.SimpleDiffUtilExtension
import smartadapter.diffutil.extension.diffSwapList
import smartrecycleradapter.R
import smartrecycleradapter.feature.simpleitem.SimpleItemViewHolder
import kotlin.random.Random.Default.nextInt
/*
* Created by Manne Öhlund on 2019-08-23.
* Copyright (c) All rights reserved.
*/
class DiffUtilActivity : BaseSampleActivity() {
val maxItemCount = nextInt(20, 100)
val items = (0..maxItemCount).toMutableList()
private lateinit var smartRecyclerAdapter: SmartRecyclerAdapter
private val predicate = object : DiffUtilExtension.DiffPredicate<Int> {
override fun areItemsTheSame(oldItem: Int, newItem: Int): Boolean {
return oldItem == newItem
}
override fun areContentsTheSame(oldItem: Int, newItem: Int): Boolean {
return oldItem == newItem
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
supportActionBar?.title = "Diff Util Sample"
smartRecyclerAdapter = SmartRecyclerAdapter
.items(items)
.map(Integer::class, SimpleItemViewHolder::class)
.add(SimpleDiffUtilExtension(predicate))
.into(recyclerView)
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.scroll_top, menu)
menuInflater.inflate(R.menu.sort, menu)
menuInflater.inflate(R.menu.shuffle, menu)
return super.onCreateOptionsMenu(menu)
}
override fun onOptionsItemSelected(item: MenuItem?): Boolean {
when (item?.itemId) {
R.id.scroll_top -> {
recyclerView.smoothScrollToPosition(0)
}
R.id.sort -> {
smartRecyclerAdapter.diffSwapList(items)
recyclerView.scrollToPosition(0)
}
R.id.shuffle -> {
smartRecyclerAdapter.diffSwapList(
items.shuffled()
.filter {
it % nextInt(1, maxItemCount/5) == 1
}
)
}
}
return super.onOptionsItemSelected(item)
}
}