Skip to content

Commit d2d0114

Browse files
committedMar 30, 2020
feat: 新增多类型元素的示例
1 parent 5f4ec6b commit d2d0114

File tree

6 files changed

+197
-38
lines changed

6 files changed

+197
-38
lines changed
 

‎app/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,6 @@ dependencies {
3838
implementation 'androidx.navigation:navigation-fragment:2.1.0-alpha05'
3939
implementation 'androidx.navigation:navigation-ui:2.0.0'
4040
implementation 'androidx.fragment:fragment:1.1.0-alpha01'
41+
implementation 'com.github.bumptech.glide:glide:4.11.0'
42+
annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
4143
}

‎app/src/main/AndroidManifest.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
package="me.monster.blogtest">
44

5+
<uses-permission android:name="android.permission.INTERNET"/>
6+
57
<application
68
android:allowBackup="true"
79
android:icon="@mipmap/ic_launcher"
Lines changed: 81 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,43 @@
11
package me.monster.blogtest
22

3-
import androidx.appcompat.app.AppCompatActivity
43
import android.os.Bundle
4+
import android.widget.Toast
5+
import androidx.appcompat.app.AppCompatActivity
56
import androidx.recyclerview.widget.DividerItemDecoration
6-
import androidx.recyclerview.widget.LinearLayoutManager
7+
import androidx.recyclerview.widget.GridLayoutManager
78
import kotlinx.android.synthetic.main.activity_diff_list.*
89
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+
)
937

1038
class DiffListActivity : AppCompatActivity() {
1139

12-
private val numberList = mutableListOf<SimpleText>()
40+
private var numberList = mutableListOf<BaseType>()
1341
private val simpleListAdapter = SimpleListAdapter()
1442

1543
override fun onCreate(savedInstanceState: Bundle?) {
@@ -27,42 +55,76 @@ class DiffListActivity : AppCompatActivity() {
2755
btnEdit.setOnClickListener {
2856
edit()
2957
}
58+
btnAddImg.setOnClickListener { addImage(1) }
3059

3160
rv_main_diff.also {
3261
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+
}
3473
it.addItemDecoration(DividerItemDecoration(this, DividerItemDecoration.VERTICAL))
3574
}
3675
prepareList()
3776
simpleListAdapter.submitList(numberList)
3877
}
3978

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+
4092
/**
4193
* 编辑倒数第二个元素
4294
*/
4395
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()
46104
}
47-
tmpList[tmpList.size -2] = SimpleText("New Edit To")
48-
simpleListAdapter.submitList(tmpList)
49105
}
50106

51107
/**
52108
* 在列表前加上 20 个元素
53109
*/
54110
private fun add() {
55-
val tmpList = MutableList(10) {
56-
return@MutableList numberList[it]
57-
}
111+
val tmpList = copyBase()
58112
for (i in 0 until 20) {
59113
val element = SimpleText(("Add Item $i"))
60114
tmpList.add(0, element)
61115
numberList.add(0, element)
62116
}
117+
numberList = tmpList
63118
simpleListAdapter.submitList(tmpList)
64119
}
65120

121+
private fun copyBase(): MutableList<BaseType> {
122+
val tmpList = MutableList(numberList.size) {
123+
return@MutableList numberList[it]
124+
}
125+
return tmpList
126+
}
127+
66128
/**
67129
* 减去列表中最前面的 10 个元素
68130
*/
@@ -71,7 +133,9 @@ class DiffListActivity : AppCompatActivity() {
71133
val tmpList = MutableList(numberList.size) {
72134
return@MutableList numberList[it]
73135
}
74-
simpleListAdapter.submitList(tmpList.subList(0, 10))
136+
val subList = tmpList.subList(0, 10)
137+
numberList = subList
138+
simpleListAdapter.submitList(subList)
75139
}
76140
}
77141

@@ -81,6 +145,10 @@ class DiffListActivity : AppCompatActivity() {
81145
}
82146
}
83147

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)
85153

86154
}

‎app/src/main/java/me/monster/blogtest/adapter/SimpleListAdapter.kt

Lines changed: 59 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,89 @@ import android.R
44
import android.view.LayoutInflater
55
import android.view.View
66
import android.view.ViewGroup
7+
import android.widget.ImageView
78
import android.widget.TextView
89
import androidx.recyclerview.widget.DiffUtil
910
import androidx.recyclerview.widget.ListAdapter
1011
import androidx.recyclerview.widget.RecyclerView
12+
import com.bumptech.glide.Glide
1113
import me.monster.blogtest.DiffListActivity
14+
import me.monster.blogtest.type_image
15+
import me.monster.blogtest.type_text
1216

1317
/**
1418
* @description
1519
* @author: Created jiangjiwei in 2020/3/23 23:04
1620
*/
1721

18-
class SimpleDiffItemCallback : DiffUtil.ItemCallback<DiffListActivity.SimpleText>() {
19-
override fun areItemsTheSame(oldItem: DiffListActivity.SimpleText, newItem: DiffListActivity.SimpleText): Boolean {
22+
class SimpleDiffItemCallback : DiffUtil.ItemCallback<DiffListActivity.BaseType>() {
23+
override fun areItemsTheSame(
24+
oldItem: DiffListActivity.BaseType,
25+
newItem: DiffListActivity.BaseType
26+
): Boolean {
2027
return oldItem == newItem
2128
}
2229

23-
override fun areContentsTheSame(oldItem: DiffListActivity.SimpleText, newItem: DiffListActivity.SimpleText): Boolean {
24-
return oldItem.title.hashCode() == newItem.title.hashCode()
30+
override fun areContentsTheSame(
31+
oldItem: DiffListActivity.BaseType,
32+
newItem: DiffListActivity.BaseType
33+
): Boolean {
34+
return if (oldItem.type != oldItem.type) {
35+
false
36+
} else {
37+
if (oldItem.type == type_text) {
38+
(oldItem as DiffListActivity.SimpleText).title.hashCode() == (newItem as DiffListActivity.SimpleText).title.hashCode()
39+
} else {
40+
(oldItem as DiffListActivity.SimpleImage).link.hashCode() == (newItem as DiffListActivity.SimpleImage).link.hashCode()
41+
}
42+
}
2543
}
2644
}
2745

28-
class SimpleListAdapter : ListAdapter<DiffListActivity.SimpleText, SimpleListViewHolder>(
46+
class SimpleListAdapter : ListAdapter<DiffListActivity.BaseType, RecyclerView.ViewHolder>(
2947
SimpleDiffItemCallback()
3048
) {
3149

32-
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SimpleListViewHolder {
33-
return SimpleListViewHolder(
34-
LayoutInflater.from(
35-
parent.context
36-
).inflate(R.layout.simple_list_item_1, parent, false)
37-
)
50+
override fun getItemViewType(position: Int): Int {
51+
return getItem(position).type
3852
}
3953

40-
override fun onBindViewHolder(holder: SimpleListViewHolder, position: Int) {
41-
holder.tvText.text = getItem(position).title
54+
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
55+
return if (viewType == type_text) {
56+
SimpleListViewHolder(
57+
LayoutInflater.from(
58+
parent.context
59+
).inflate(R.layout.simple_list_item_1, parent, false)
60+
)
61+
} else {
62+
SimpleImageViewHolder(
63+
LayoutInflater.from(
64+
parent.context
65+
).inflate(me.monster.blogtest.R.layout.item_img_diff, parent, false)
66+
)
67+
}
68+
}
69+
70+
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
71+
val item = getItem(position)
72+
if (item.type == type_text) {
73+
(holder as SimpleListViewHolder).tvText.text =
74+
(item as DiffListActivity.SimpleText).title
75+
} else if (item.type == type_image) {
76+
val simpleImage = item as DiffListActivity.SimpleImage
77+
val ivDiff = (holder as SimpleImageViewHolder).ivDiff
78+
Glide.with(ivDiff)
79+
.asBitmap()
80+
.load(simpleImage.link)
81+
.into(ivDiff)
82+
}
4283
}
4384
}
4485

45-
class SimpleListViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {
86+
class SimpleListViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
4687
var tvText: TextView = itemView.findViewById(android.R.id.text1)
88+
}
89+
90+
class SimpleImageViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
91+
val ivDiff = itemView.findViewById<ImageView>(me.monster.blogtest.R.id.ivDiff)
4792
}

‎app/src/main/res/layout/activity_diff_list.xml

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,48 @@
66
android:orientation="vertical"
77
tools:context=".MainActivity">
88

9-
<androidx.appcompat.widget.AppCompatButton
10-
android:id="@+id/btnEdit"
9+
<androidx.appcompat.widget.LinearLayoutCompat
1110
android:layout_width="match_parent"
1211
android:layout_height="48dp"
13-
android:text="改列表中倒数第二个" />
12+
android:orientation="horizontal">
1413

15-
<androidx.appcompat.widget.AppCompatButton
16-
android:id="@+id/btnAdd"
17-
android:layout_width="match_parent"
18-
android:layout_height="48dp"
19-
android:text="加 10 个" />
14+
<androidx.appcompat.widget.AppCompatButton
15+
android:id="@+id/btnEdit"
16+
android:layout_width="0dp"
17+
android:layout_height="48dp"
18+
android:layout_weight="1"
19+
android:text="改倒数第二个" />
20+
21+
<androidx.appcompat.widget.AppCompatButton
22+
android:id="@+id/btnAdd"
23+
android:layout_width="0dp"
24+
android:layout_height="48dp"
25+
android:layout_weight="1"
26+
android:text="加 10 个" />
27+
28+
<androidx.appcompat.widget.AppCompatButton
29+
android:id="@+id/btnMinus"
30+
android:layout_width="0dp"
31+
android:layout_height="48dp"
32+
android:layout_weight="1"
33+
android:text="减 10 个" />
2034

21-
<androidx.appcompat.widget.AppCompatButton
22-
android:id="@+id/btnMinus"
35+
</androidx.appcompat.widget.LinearLayoutCompat>
36+
37+
<androidx.appcompat.widget.LinearLayoutCompat
2338
android:layout_width="match_parent"
2439
android:layout_height="48dp"
25-
android:text="减 10 个" />
40+
android:orientation="horizontal">
41+
42+
<androidx.appcompat.widget.AppCompatButton
43+
android:id="@+id/btnAddImg"
44+
android:layout_width="0dp"
45+
android:layout_height="48dp"
46+
android:layout_weight="1"
47+
android:text="加一个图片" />
48+
49+
</androidx.appcompat.widget.LinearLayoutCompat>
50+
2651

2752
<androidx.recyclerview.widget.RecyclerView
2853
android:id="@+id/rv_main_diff"
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools"
5+
android:layout_width="85dp"
6+
android:layout_height="85dp">
7+
8+
<ImageView
9+
android:id="@+id/ivDiff"
10+
android:layout_width="75dp"
11+
android:layout_height="75dp"
12+
android:layout_marginStart="5dp"
13+
android:layout_marginTop="5dp"
14+
app:layout_constraintStart_toStartOf="parent"
15+
app:layout_constraintTop_toTopOf="parent"
16+
tools:srcCompat="@tools:sample/avatars" />
17+
</androidx.constraintlayout.widget.ConstraintLayout>

0 commit comments

Comments
 (0)
Please sign in to comment.