-
Notifications
You must be signed in to change notification settings - Fork 50
/
NotifyItemActivity.kt
142 lines (125 loc) · 5.35 KB
/
NotifyItemActivity.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package com.ocnyang.recyclerviewevent.notify_item
import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import android.widget.CheckBox
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.ocnyang.recyclerviewevent.R
class NotifyItemActivity : AppCompatActivity() {
private lateinit var mAdapter: NotifyItemAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_notify_item)
supportActionBar!!.setDisplayHomeAsUpEnabled(true)
findViewById<RecyclerView>(R.id.recyclerView).apply {
this.layoutManager = LinearLayoutManager(this@NotifyItemActivity)
mAdapter = NotifyItemAdapter(
this@NotifyItemActivity,
NotifyItemDataManager.getData()
)
this.adapter = mAdapter
}
// 刷新所有条目的选中状态,(注意观察条目刷新时,整个条目的闪动)
findViewById<CheckBox>(R.id.cb_check_all_left).setOnCheckedChangeListener { _, isChecked ->
mAdapter.apply {
data.forEachIndexed { index, bean ->
bean.checked = isChecked
notifyItemChanged(index)
}
}
}
// 刷新所有条目的选中状态,(此时其实只是刷新条目的复选框的状态,并不会重绘整个条目,所以不会有条目闪动)
findViewById<CheckBox>(R.id.cb_check_all_right).setOnCheckedChangeListener { _, isChecked ->
mAdapter.apply {
data.forEachIndexed { index, bean ->
bean.checked = isChecked
notifyItemChanged(index, NotifyItemAdapter.PAYLOAD_CHECKBOX)
}
}
}
// 交换条目1和条目2的图片并刷新这两个条目
findViewById<View>(R.id.btn_change_item1_left).setOnClickListener {
mAdapter.apply {
// 交换条目1、条目2数据中的图片资源
data[0].imgId = data[1].imgId.also {
data[1].imgId = data[0].imgId
}
notifyItemRangeChanged(0, 2)
}
}
// 交换条目1和条目2的图片并刷新这两个条目 (此时只是重新加载两个条目的图片,不会重绘其他控件,不会闪动)
findViewById<View>(R.id.btn_change_item1_right).setOnClickListener {
mAdapter.apply {
data[0].imgId = data[1].imgId.also {
data[1].imgId = data[0].imgId
}
notifyItemRangeChanged(0, 2, NotifyItemAdapter.PAYLOAD_IMAGE)
}
}
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
if (item.itemId == android.R.id.home) {
finish()
}
return super.onOptionsItemSelected(item)
}
}
class NotifyItemAdapter(var context: Context, var data: MutableList<NotifyItemBean>) :
RecyclerView.Adapter<NotifyItemViewHolder>() {
companion object {
@JvmStatic
val PAYLOAD_CHECKBOX = 0x00000001
@JvmStatic
val PAYLOAD_IMAGE = 0x00000010
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NotifyItemViewHolder =
NotifyItemViewHolder(
LayoutInflater.from(context).inflate(R.layout.activity_notify_item_item, parent, false)
)
override fun onBindViewHolder(holder: NotifyItemViewHolder, position: Int) {
holder.ivImg.setImageResource(data[position].imgId)
holder.tvTitle.text = data[position].name
holder.cb.setOnCheckedChangeListener(null)
holder.cb.isChecked = data[position].checked
holder.cb.setOnCheckedChangeListener { _, isChecked ->
data[position].checked = isChecked
}
}
/**
* 当使用 [RecyclerView.Adapter.notifyItemChanged] 两个参数的方法 刷新条目时会走此方法刷新布局
*/
override fun onBindViewHolder(
holder: NotifyItemViewHolder, position: Int, payloads: MutableList<Any>
) {
if (payloads.isEmpty()) {
onBindViewHolder(holder, position)
} else {
when (payloads[0]) {
PAYLOAD_CHECKBOX -> {
val checked = data[position].checked
if (checked != holder.cb.isChecked) {
holder.cb.setOnCheckedChangeListener(null)
holder.cb.isChecked = checked
holder.cb.setOnCheckedChangeListener { _, isChecked ->
data[position].checked = isChecked
}
}
}
PAYLOAD_IMAGE -> holder.ivImg.setImageResource(data[position].imgId)
}
}
}
override fun getItemCount(): Int = data.size
}
class NotifyItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var ivImg: ImageView = itemView.findViewById(R.id.iv_img)
var tvTitle: TextView = itemView.findViewById(R.id.tv_title)
var cb: CheckBox = itemView.findViewById(R.id.cb)
}