11package pizzk.media.picker.widget
22
3+ import android.annotation.SuppressLint
34import android.app.Activity
45import android.content.Context
6+ import android.content.res.TypedArray
7+ import android.graphics.Rect
58import android.net.Uri
6- import androidx.recyclerview.widget.GridLayoutManager
7- import androidx.recyclerview.widget.RecyclerView
89import android.util.AttributeSet
10+ import android.view.View
911import android.view.ViewGroup
12+ import androidx.recyclerview.widget.GridLayoutManager
13+ import androidx.recyclerview.widget.RecyclerView
1014import pizzk.media.picker.R
1115import pizzk.media.picker.adapter.CommonListAdapter
1216import pizzk.media.picker.adapter.PhotoGroupAdapter
@@ -20,15 +24,20 @@ import pizzk.media.picker.view.PickChoseActivity
2024 * 选取一组照片的视图
2125 */
2226class PhotoGroupView : RecyclerView {
27+ private var spacing: Int = 0
2328
24- constructor (context: Context ) : super (context)
25- constructor (context: Context , attrs: AttributeSet ? ) : super (context, attrs)
26- constructor (context: Context , attrs: AttributeSet ? , defStyle: Int ) : super (context, attrs, defStyle)
29+ constructor (context: Context ) : this (context, null )
30+ constructor (context: Context , attrs: AttributeSet ? ) : this (context, attrs, androidx.recyclerview.R .attr.recyclerViewStyle)
2731
28- init {
32+ @SuppressLint(" Recycle" )
33+ constructor (context: Context , attrs: AttributeSet ? , defStyle: Int ) : super (context, attrs, defStyle) {
2934 isVerticalScrollBarEnabled = false
3035 isHorizontalScrollBarEnabled = false
3136 overScrollMode = OVER_SCROLL_NEVER
37+ val attrId = R .styleable.PhotoGroupView
38+ val ta: TypedArray = context.obtainStyledAttributes(attrs, attrId) ? : return
39+ spacing = ta.getDimensionPixelOffset(R .styleable.PhotoGroupView_space , spacing)
40+ ta.recycle()
3241 }
3342
3443 private val choiceList: List <String > = listOf (
@@ -41,38 +50,49 @@ class PhotoGroupView : RecyclerView {
4150 readOnly : Boolean ,
4251 appendText : String = "",
4352 changed : (PhotoGroupAdapter , Int ) -> Unit = { _, _ -> }) {
44- val manager = object : GridLayoutManager (context, special.column) {
45- override fun isAutoMeasureEnabled (): Boolean = true
46- }
47- this .layoutManager = manager
48- val pAdapter = PhotoGroupAdapter (context, special.fixed, special.lp)
49- pAdapter.setReadOnly(readOnly)
50- pAdapter.setChangeBlock(changed)
51- pAdapter.setAppendText(appendText)
52- if (! pAdapter.update(exists, special.limit, index = - 1 )) {
53- changed(pAdapter, - 1 )
54- }
55- this .adapter = pAdapter
56- // 配置Adapter
57- pAdapter.setTapBlock { _, index ->
58- val el: PhotoItem = pAdapter.getList()[index]
59- if (el.path.isEmpty()) {
60- if (pAdapter.isReadOnly()) return @setTapBlock
61- // 选择图片
62- val selects: List <String > = if (pAdapter.isAppend) pAdapter.selectPaths() else emptyList()
63- PickChoseActivity .show(special.activity, choiceList) { key ->
64- showPickPhoto(special.activity, key, selects, special.limit, pAdapter, index, special.crop)
53+ post {
54+ val vWidth = measuredWidth - paddingStart - paddingEnd
55+ val minWidth = context.resources.getDimensionPixelOffset(R .dimen.pick_photo_min_size)
56+ val lp: ViewGroup .LayoutParams = ViewGroup .LayoutParams (0 , ViewGroup .LayoutParams .WRAP_CONTENT )
57+ do {
58+ lp.width = (vWidth - (special.column - 1 ) * spacing) / special.column
59+ if (lp.width >= minWidth || special.column <= 1 ) break
60+ special.column - = 1
61+ } while (true )
62+ val manager = object : GridLayoutManager (context, special.column) {
63+ override fun isAutoMeasureEnabled (): Boolean = true
64+ }
65+ this .layoutManager = manager
66+ addItemDecoration(GridSpacingItemDecoration (special.column, spacing))
67+ val pAdapter = PhotoGroupAdapter (context, special.fixed, lp)
68+ pAdapter.setReadOnly(readOnly)
69+ pAdapter.setChangeBlock(changed)
70+ pAdapter.setAppendText(appendText)
71+ if (! pAdapter.update(exists, special.limit, index = - 1 )) {
72+ changed(pAdapter, - 1 )
73+ }
74+ this .adapter = pAdapter
75+ // 配置Adapter
76+ pAdapter.setTapBlock { _, index ->
77+ val el: PhotoItem = pAdapter.getList()[index]
78+ if (el.path.isEmpty()) {
79+ if (pAdapter.isReadOnly()) return @setTapBlock
80+ // 选择图片
81+ val selects: List <String > = if (pAdapter.isAppend) pAdapter.selectPaths() else emptyList()
82+ PickChoseActivity .show(special.activity, choiceList) { key ->
83+ showPickPhoto(special.activity, key, selects, special.limit, pAdapter, index, special.crop)
84+ }
85+ } else {
86+ // 预览
87+ val selects: List <String > = if (pAdapter.isAppend) pAdapter.selectPaths() else arrayListOf (el.path)
88+ showPreview(special.activity, selects, if (pAdapter.isAppend) index else 0 )
6589 }
66- } else {
67- // 预览
68- val selects: List <String > = if (pAdapter.isAppend) pAdapter.selectPaths() else arrayListOf (el.path)
69- showPreview(special.activity, selects, if (pAdapter.isAppend) index else 0 )
7090 }
71- }
72- pAdapter.setTapChildBlock { _, _, index, what ->
73- if ( CommonListAdapter . WHAT0 != what) return @setTapChildBlock
74- // 移除图片
75- pAdapter.delete(index)
91+ pAdapter.setTapChildBlock { _, _, index, what ->
92+ if ( CommonListAdapter . WHAT0 != what) return @setTapChildBlock
93+ // 移除图片
94+ pAdapter.delete(index)
95+ }
7696 }
7797 }
7898
@@ -136,10 +156,21 @@ class PhotoGroupView : RecyclerView {
136156 // 指定参数
137157 class Special (
138158 var activity : Activity ,
139- var lp : ViewGroup .LayoutParams ,
140159 var limit : Int = 1 ,
141160 var column : Int = 4 ,
142161 var fixed : MutableList <PhotoItem >? = null ,
143162 var crop : CropParams ? = null
144163 )
164+
165+ internal class GridSpacingItemDecoration (private val spanCount : Int , private val spacing : Int ) : ItemDecoration() {
166+
167+ override fun getItemOffsets (outRect : Rect , view : View , parent : RecyclerView , state : State ) {
168+ val position = parent.getChildAdapterPosition(view)
169+ val column = position % spanCount
170+ outRect.left = column * spacing / spanCount
171+ outRect.right = spacing - (column + 1 ) * spacing / spanCount
172+ if (position < spanCount) return
173+ outRect.top = spacing
174+ }
175+ }
145176}
0 commit comments