Skip to content

Commit 7965adb

Browse files
author
fengpeng
committed
选择图片监听事件添加index起始参数
1 parent 7a8473b commit 7965adb

2 files changed

Lines changed: 38 additions & 33 deletions

File tree

picker/src/main/java/pizzk/media/picker/adapter/PhotoGroupAdapter.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class PhotoGroupAdapter(context: Context, fixedList: List<PhotoItem>?, lp: ViewG
1717
val isAppend: Boolean
1818
private var appendText: String = ""
1919
private val lp: ViewGroup.LayoutParams
20-
private var changeBlock: (PhotoGroupAdapter) -> Unit = { _ -> }
20+
private var changeBlock: (PhotoGroupAdapter, Int) -> Unit = { _, _ -> }
2121

2222
init {
2323
if (null == fixedList || fixedList.isEmpty()) {
@@ -42,7 +42,7 @@ class PhotoGroupAdapter(context: Context, fixedList: List<PhotoItem>?, lp: ViewG
4242
appendText = text
4343
}
4444

45-
fun setChangeBlock(block: (PhotoGroupAdapter) -> Unit) {
45+
fun setChangeBlock(block: (PhotoGroupAdapter, Int) -> Unit) {
4646
this.changeBlock = block
4747
}
4848

@@ -53,15 +53,15 @@ class PhotoGroupAdapter(context: Context, fixedList: List<PhotoItem>?, lp: ViewG
5353
if (!isAppend || 1 == getList().size) {
5454
el.path = ""
5555
notifyItemChanged(index)
56-
changeBlock(this)
56+
changeBlock(this, index)
5757
return
5858
}
5959
remove(index)
6060
if (!TextUtils.isEmpty(getList()[getList().size - 1].path)) {
6161
getList().add(PhotoItem())
6262
}
6363
notifyDataSetChanged()
64-
changeBlock(this)
64+
changeBlock(this, index)
6565
}
6666

6767
//更新图片
@@ -86,7 +86,7 @@ class PhotoGroupAdapter(context: Context, fixedList: List<PhotoItem>?, lp: ViewG
8686
return false
8787
}
8888
}
89-
changeBlock(this)
89+
changeBlock(this, index)
9090
return true
9191
}
9292

picker/src/main/java/pizzk/media/picker/widget/PhotoGroupView.kt

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class PhotoGroupView : RecyclerView {
4040
exists: List<String>? = null,
4141
readOnly: Boolean,
4242
appendText: String = "",
43-
changed: (PhotoGroupAdapter) -> Unit = {}) {
43+
changed: (PhotoGroupAdapter, Int) -> Unit = { _, _ -> }) {
4444
val manager = object : GridLayoutManager(context, special.column) {
4545
override fun isAutoMeasureEnabled(): Boolean = true
4646
}
@@ -49,8 +49,8 @@ class PhotoGroupView : RecyclerView {
4949
pAdapter.setReadOnly(readOnly)
5050
pAdapter.setChangeBlock(changed)
5151
pAdapter.setAppendText(appendText)
52-
if (!pAdapter.update(exists, special.limit)) {
53-
changed(pAdapter)
52+
if (!pAdapter.update(exists, special.limit, index = -1)) {
53+
changed(pAdapter, -1)
5454
}
5555
this.adapter = pAdapter
5656
//配置Adapter
@@ -86,9 +86,12 @@ class PhotoGroupView : RecyclerView {
8686
}
8787

8888
//跳转至选择图片
89-
private fun showPickPhoto(activity: Activity, key: String,
90-
selects: List<String>, limit: Int,
91-
adapter: PhotoGroupAdapter, index: Int,
89+
private fun showPickPhoto(activity: Activity,
90+
key: String,
91+
selects: List<String>,
92+
limit: Int,
93+
adapter: PhotoGroupAdapter,
94+
index: Int,
9295
crop: CropParams?) {
9396
val action: Int = when (key) {
9497
choiceList[0] -> {
@@ -100,32 +103,34 @@ class PhotoGroupView : RecyclerView {
100103
else -> -1
101104
}
102105
if (action < 0) return
106+
val block: (Int, List<Uri>) -> Unit = { code, list ->
107+
if (code == PickControl.ACTION_CAMERA) {
108+
if (adapter.isAppend) {
109+
val allOf: MutableList<String> = ArrayList(adapter.selectCount() + 1)
110+
allOf.addAll(adapter.selectPaths())
111+
allOf.addAll(list.map(Uri::toString))
112+
adapter.update(allOf, limit, index)
113+
} else {
114+
adapter.update(list.map(Uri::toString), limit, index)
115+
}
116+
} else {
117+
if (adapter.isAppend) {
118+
val remotes: List<String> = selects.filter { null == PickUtils.path2Uri(it) }
119+
val allOf: MutableList<String> = ArrayList(limit)
120+
allOf.addAll(remotes)
121+
allOf.addAll(list.map(Uri::toString))
122+
adapter.update(allOf, limit, index)
123+
} else {
124+
adapter.update(list.map(Uri::toString), limit, index)
125+
}
126+
}
127+
}
103128
PickControl.obtain(clean = true).action(action)
104129
.selects(selects)
105130
.limit(limit)
106131
.crop(crop)
107-
.callback { code, list ->
108-
if (code == PickControl.ACTION_CAMERA) {
109-
if (adapter.isAppend) {
110-
val allOf: MutableList<String> = ArrayList(adapter.selectCount() + 1)
111-
allOf.addAll(adapter.selectPaths())
112-
allOf.addAll(list.map(Uri::toString))
113-
adapter.update(allOf, limit, index)
114-
} else {
115-
adapter.update(list.map(Uri::toString), limit, index)
116-
}
117-
} else {
118-
if (adapter.isAppend) {
119-
val remotes: List<String> = selects.filter { null == PickUtils.path2Uri(it) }
120-
val allOf: MutableList<String> = ArrayList(limit)
121-
allOf.addAll(remotes)
122-
allOf.addAll(list.map(Uri::toString))
123-
adapter.update(allOf, limit, index)
124-
} else {
125-
adapter.update(list.map(Uri::toString), limit, index)
126-
}
127-
}
128-
}.done(activity)
132+
.callback(block)
133+
.done(activity)
129134
}
130135

131136
//指定参数

0 commit comments

Comments
 (0)