1
1
package me.monster.blogtest
2
2
3
- import androidx.appcompat.app.AppCompatActivity
4
3
import android.os.Bundle
4
+ import android.widget.Toast
5
+ import androidx.appcompat.app.AppCompatActivity
5
6
import androidx.recyclerview.widget.DividerItemDecoration
6
- import androidx.recyclerview.widget.LinearLayoutManager
7
+ import androidx.recyclerview.widget.GridLayoutManager
7
8
import kotlinx.android.synthetic.main.activity_diff_list.*
8
9
import me.monster.blogtest.adapter.SimpleListAdapter
10
+ import kotlin.random.Random
11
+
12
+ const val type_text = 1
13
+ const val type_image = 2
14
+ const val span_count = 2
15
+ val allImage = mutableListOf (
16
+ " https://cdn.pixabay.com/photo/2018/01/05/16/24/rose-3063284_150.jpg" ,
17
+ " https://cdn.pixabay.com/photo/2018/01/28/11/24/sunflower-3113318_150.jpg" ,
18
+ " https://cdn.pixabay.com/photo/2020/03/10/15/54/dandelion-4919334_150.jpg" ,
19
+ " https://cdn.pixabay.com/photo/2018/04/05/14/09/sunflower-3292932_150.jpg" ,
20
+ " https://cdn.pixabay.com/photo/2017/05/08/13/15/spring-bird-2295434_150.jpg" ,
21
+ " https://cdn.pixabay.com/photo/2016/07/12/18/54/lily-1512813_150.jpg" ,
22
+ " https://cdn.pixabay.com/photo/2015/04/10/00/41/yellow-715540_150.jpg" ,
23
+ " https://cdn.pixabay.com/photo/2012/09/08/21/51/anemone-56414_150.jpg" ,
24
+ " https://cdn.pixabay.com/photo/2017/01/11/17/27/drip-1972411_150.jpg" ,
25
+ " https://cdn.pixabay.com/photo/2017/03/15/09/00/crocus-2145539_150.jpg" ,
26
+ " https://cdn.pixabay.com/photo/2018/03/10/20/26/flowers-3215188_150.jpg" ,
27
+ " https://cdn.pixabay.com/photo/2016/07/23/00/12/sun-flower-1536088_150.jpg" ,
28
+ " https://cdn.pixabay.com/photo/2018/10/03/03/42/flower-gerbel-3720383_150.jpg" ,
29
+ " https://cdn.pixabay.com/photo/2017/12/30/13/25/portrait-3050076_150.jpg" ,
30
+ " https://cdn.pixabay.com/photo/2016/01/08/05/24/sunflower-1127174_150.jpg" ,
31
+ " https://cdn.pixabay.com/photo/2015/04/19/08/33/flower-729515_150.jpg" ,
32
+ " https://cdn.pixabay.com/photo/2016/08/28/23/24/sunflower-1627193_150.jpg" ,
33
+ " https://cdn.pixabay.com/photo/2018/09/26/21/47/flowers-3705716_150.jpg" ,
34
+ " https://cdn.pixabay.com/photo/2012/07/12/14/50/flower-52358_150.jpg" ,
35
+ " https://cdn.pixabay.com/photo/2013/05/26/12/14/rose-113735_150.jpg"
36
+ )
9
37
10
38
class DiffListActivity : AppCompatActivity () {
11
39
12
- private val numberList = mutableListOf<SimpleText >()
40
+ private var numberList = mutableListOf<BaseType >()
13
41
private val simpleListAdapter = SimpleListAdapter ()
14
42
15
43
override fun onCreate (savedInstanceState : Bundle ? ) {
@@ -27,42 +55,76 @@ class DiffListActivity : AppCompatActivity() {
27
55
btnEdit.setOnClickListener {
28
56
edit()
29
57
}
58
+ btnAddImg.setOnClickListener { addImage(1 ) }
30
59
31
60
rv_main_diff.also {
32
61
it.adapter = simpleListAdapter
33
- it.layoutManager = LinearLayoutManager (this )
62
+ it.layoutManager = GridLayoutManager (this , span_count).apply {
63
+ this .spanSizeLookup = object : GridLayoutManager .SpanSizeLookup () {
64
+ override fun getSpanSize (position : Int ): Int {
65
+ return if (numberList[position].type == type_image) {
66
+ span_count / span_count
67
+ } else {
68
+ span_count
69
+ }
70
+ }
71
+ }
72
+ }
34
73
it.addItemDecoration(DividerItemDecoration (this , DividerItemDecoration .VERTICAL ))
35
74
}
36
75
prepareList()
37
76
simpleListAdapter.submitList(numberList)
38
77
}
39
78
79
+ private fun addImage (count : Int ) {
80
+ val copyBase = copyBase()
81
+ if (count == 1 ) {
82
+ copyBase.add(SimpleImage (allImage[Random (System .currentTimeMillis()).nextInt(allImage.size - 1 )]))
83
+ } else {
84
+ for (i in 0 until count) {
85
+ copyBase.add(SimpleImage (allImage[Random (System .currentTimeMillis() * i).nextInt(allImage.size - 1 )]))
86
+ }
87
+ }
88
+ numberList = copyBase
89
+ simpleListAdapter.submitList(copyBase)
90
+ }
91
+
40
92
/* *
41
93
* 编辑倒数第二个元素
42
94
*/
43
95
private fun edit () {
44
- val tmpList = MutableList (10 ) {
45
- return @MutableList numberList[it]
96
+ val tmpList = copyBase()
97
+ val baseType = tmpList[tmpList.size - 2 ]
98
+ if (baseType.type == type_text) {
99
+ tmpList[tmpList.size - 2 ] = SimpleText (" New Edit To" )
100
+ numberList = tmpList
101
+ simpleListAdapter.submitList(tmpList)
102
+ } else {
103
+ Toast .makeText(this , " 当前倒数第二个不是文本" , Toast .LENGTH_SHORT ).show()
46
104
}
47
- tmpList[tmpList.size - 2 ] = SimpleText (" New Edit To" )
48
- simpleListAdapter.submitList(tmpList)
49
105
}
50
106
51
107
/* *
52
108
* 在列表前加上 20 个元素
53
109
*/
54
110
private fun add () {
55
- val tmpList = MutableList (10 ) {
56
- return @MutableList numberList[it]
57
- }
111
+ val tmpList = copyBase()
58
112
for (i in 0 until 20 ) {
59
113
val element = SimpleText ((" Add Item $i " ))
60
114
tmpList.add(0 , element)
61
115
numberList.add(0 , element)
62
116
}
117
+ numberList = tmpList
63
118
simpleListAdapter.submitList(tmpList)
64
119
}
65
120
121
+ private fun copyBase (): MutableList <BaseType > {
122
+ val tmpList = MutableList (numberList.size) {
123
+ return @MutableList numberList[it]
124
+ }
125
+ return tmpList
126
+ }
127
+
66
128
/* *
67
129
* 减去列表中最前面的 10 个元素
68
130
*/
@@ -71,7 +133,9 @@ class DiffListActivity : AppCompatActivity() {
71
133
val tmpList = MutableList (numberList.size) {
72
134
return @MutableList numberList[it]
73
135
}
74
- simpleListAdapter.submitList(tmpList.subList(0 , 10 ))
136
+ val subList = tmpList.subList(0 , 10 )
137
+ numberList = subList
138
+ simpleListAdapter.submitList(subList)
75
139
}
76
140
}
77
141
@@ -81,6 +145,10 @@ class DiffListActivity : AppCompatActivity() {
81
145
}
82
146
}
83
147
84
- data class SimpleText (var title : String )
148
+ data class SimpleText (var title : String ) : BaseType(type_text)
149
+
150
+ data class SimpleImage (var link : String ) : BaseType(type_image)
151
+
152
+ open class BaseType (val type : Int )
85
153
86
154
}
0 commit comments