Skip to content

Commit 120a7cd

Browse files
committed
修复批量删除接口
1 parent 2e87282 commit 120a7cd

File tree

9 files changed

+38
-40
lines changed

9 files changed

+38
-40
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
### 引入依赖
66

77
```kotlin
8-
implementation 'com.yhw.library:fastadapter:1.0.2'
8+
implementation 'com.yhw.library:fastadapter:1.0.3'
99
```
1010

1111
### 使用方法

library/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ publish {
77
userOrg = 'yanghongwei'
88
groupId = 'com.yhw.library'
99
artifactId = 'fastadapter'
10-
publishVersion = '1.0.2'
10+
publishVersion = '1.0.3'
1111
repoName="FastAdapter"
1212
desc = 'A fast adapter code library, suitable for RecyclerView、ListView、GridView、ViewPager、ViewPager2、ExpandableListView、Spinner, etc'
1313
website = 'https://github.com/LuckyCodeer/FastAdapter'

library/src/main/java/com/yhw/library/adapter/BaseListGridAdapter.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,8 @@ abstract class BaseListGridAdapter<T>(private var dataList: MutableList<T>) :
6262
this.notifyDataSetChanged()
6363
}
6464

65-
override fun removeAt(positionList: MutableList<Int>) {
66-
for (position in positionList) {
67-
this.dataList.removeAt(position)
68-
}
65+
override fun removeAll(dataList: MutableList<T>) {
66+
this.dataList.removeAll(dataList)
6967
this.notifyDataSetChanged()
7068
}
7169

library/src/main/java/com/yhw/library/adapter/BaseRecyclerAdapter.kt

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,8 @@ abstract class BaseRecyclerAdapter<T>(private var dataList: MutableList<T>) :
122122
}
123123
}
124124

125-
override fun removeAt(positionList: MutableList<Int>) {
126-
if (positionList.size >= itemCount) {
127-
return
128-
}
129-
for (i in positionList) {
130-
this.dataList.removeAt(i)
131-
}
125+
override fun removeAll(dataList: MutableList<T>) {
126+
this.dataList.removeAll(dataList)
132127
this.notifyDataSetChanged()
133128
}
134129

library/src/main/java/com/yhw/library/adapter/BaseViewPager2FragmentAdapter.kt

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,8 @@ class BaseViewPager2FragmentAdapter(
9292
}
9393
}
9494

95-
override fun removeAt(positionList: MutableList<Int>) {
96-
if (positionList.size >= itemCount) {
97-
return
98-
}
99-
for (i in positionList) {
100-
this.fragmentList.removeAt(i)
101-
}
95+
override fun removeAll(dataList: MutableList<Fragment>) {
96+
this.fragmentList.removeAll(dataList)
10297
this.notifyDataSetChanged()
10398
}
10499

library/src/main/java/com/yhw/library/adapter/BaseViewPagerFragmentAdapter.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,8 @@ class BaseViewPagerFragmentAdapter : FragmentStatePagerAdapter, IAdapter<Fragmen
6969
this.notifyDataSetChanged()
7070
}
7171

72-
override fun removeAt(positionList: MutableList<Int>) {
73-
for (position in positionList) {
74-
this.fragmentList.removeAt(position)
75-
}
72+
override fun removeAll(dataList: MutableList<Fragment>) {
73+
this.fragmentList.removeAll(dataList)
7674
this.notifyDataSetChanged()
7775
}
7876

library/src/main/java/com/yhw/library/adapter/BaseViewPagerViewAdapter.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,8 @@ class BaseViewPagerViewAdapter(private var viewList: MutableList<View>) : PagerA
5959
this.notifyDataSetChanged()
6060
}
6161

62-
override fun removeAt(positionList: MutableList<Int>) {
63-
for (position in positionList) {
64-
this.viewList.removeAt(position)
65-
}
62+
override fun removeAll(dataList: MutableList<View>) {
63+
this.viewList.removeAll(dataList)
6664
this.notifyDataSetChanged()
6765
}
6866

library/src/main/java/com/yhw/library/adapter/IAdapter.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ interface IAdapter<T> {
3535
/**
3636
* 移除多个数据
3737
*/
38-
fun removeAt(positionList: MutableList<Int>)
38+
fun removeAll(dataList: MutableList<T>)
3939

4040
/**
4141
* 移除全部数据

sample/src/main/java/com/yhw/demo/RecyclerViewActivity.kt

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.yhw.demo
22

33
import android.content.DialogInterface
44
import android.os.Bundle
5+
import android.util.Log
56
import android.view.View
67
import android.widget.Toast
78
import androidx.appcompat.app.AlertDialog
@@ -22,7 +23,7 @@ class RecyclerViewActivity : AppCompatActivity() {
2223

2324
val dataList = mutableListOf<String>()
2425
for (i in 0..100) {
25-
dataList.add("this is item")
26+
dataList.add("this is item $i")
2627
}
2728

2829
val deleteBtn = findViewById<AppCompatButton>(R.id.btn_delete)
@@ -31,14 +32,18 @@ class RecyclerViewActivity : AppCompatActivity() {
3132
val adapter = MyRecyclerAdapter(dataList)
3233
recyclerView.adapter = adapter
3334
//单击
34-
adapter.onItemClickListener =object: BaseRecyclerAdapter.OnItemClickListener {
35+
/* adapter.onItemClickListener = object : BaseRecyclerAdapter.OnItemClickListener {
3536
override fun onItemClick(position: Int, view: View) {
36-
if(!adapter.isShowCheckBox){
37-
Toast.makeText(this@RecyclerViewActivity, "item => $position", Toast.LENGTH_SHORT)
37+
if (!adapter.isShowCheckBox) {
38+
Toast.makeText(
39+
this@RecyclerViewActivity,
40+
"item => $position",
41+
Toast.LENGTH_SHORT
42+
)
3843
.show()
3944
}
4045
}
41-
}
46+
}*/
4247
//长按
4348
adapter.onItemLongClickListener = object : BaseRecyclerAdapter.OnItemLongClickListener {
4449
override fun onItemLongClick(position: Int, view: View) {
@@ -125,7 +130,8 @@ class RecyclerViewActivity : AppCompatActivity() {
125130
return@setOnClickListener
126131
}
127132
adapter.isShowCheckBox = false
128-
adapter.removeAt(adapter.checkedList)
133+
Log.i("TAG", "checked list ${adapter.checkedList.size}")
134+
adapter.removeAll(adapter.checkedList)
129135
adapter.checkedList.clear()
130136
deleteBtn.visibility = View.GONE
131137
}
@@ -135,11 +141,11 @@ class RecyclerViewActivity : AppCompatActivity() {
135141
/**
136142
* 直接继承 BaseRecyclerAdapter
137143
*/
138-
inner class MyRecyclerAdapter(dataList: MutableList<String>) : BaseRecyclerAdapter<String>(
144+
inner class MyRecyclerAdapter(var dataList: MutableList<String>) : BaseRecyclerAdapter<String>(
139145
dataList
140146
) {
141147
var isShowCheckBox = false
142-
var checkedList = mutableListOf<Int>()
148+
var checkedList = mutableListOf<String>()
143149

144150
override fun getItemLayoutId(viewType: Int): Int {
145151
return R.layout.sample_item_layout
@@ -148,20 +154,28 @@ class RecyclerViewActivity : AppCompatActivity() {
148154
override fun onBindViewItem(holder: RecyclerViewHolder, position: Int, data: String) {
149155
//使用 holder.getView(R.id.tv_text) 或者 setText()
150156
// val textView = holder.getView<TextView>(R.id.tv_text)
151-
holder.setText(R.id.tv_text, "$data $position")
157+
holder.setText(R.id.tv_text, data)
152158
holder.setImageResource(R.id.iv_image, R.mipmap.ic_launcher)
153159
val checkBox = holder.getView<AppCompatCheckBox>(R.id.checkbox)
154160
checkBox.visibility = if (isShowCheckBox) View.VISIBLE else View.GONE
155161

156162
holder.itemView.setOnClickListener {
157163
if (isShowCheckBox) {
158164
checkBox.isChecked = !checkBox.isChecked
165+
val s = dataList[position]
159166
if (checkBox.isChecked) {
160-
checkedList.add(position)
167+
checkedList.add(s)
161168
} else {
162-
checkedList.remove(position)
169+
checkedList.remove(s)
163170
}
164171
return@setOnClickListener
172+
} else {
173+
Toast.makeText(
174+
this@RecyclerViewActivity,
175+
"item => $position",
176+
Toast.LENGTH_SHORT
177+
)
178+
.show()
165179
}
166180
}
167181
}

0 commit comments

Comments
 (0)