@@ -76,7 +76,7 @@ class PreviewActivity : AppCompatActivity() {
7676 private lateinit var selectAdapter: PreviewSelectAdapter
7777
7878 // 标志
79- private var currentIndex : Int = 0
79+ private var currentPageIndex : Int = 0
8080 private var selectLimit: Int = 0
8181 private var finishFlag: Boolean = false
8282
@@ -110,10 +110,8 @@ class PreviewActivity : AppCompatActivity() {
110110 selectAdapter.setClickListener { path ->
111111 val selectIndex = photoAdapter.indexOf(path)
112112 if (selectIndex < 0 ) return @setClickListener
113- setCurrentIndex (selectIndex, true )
113+ setCurrentPageIndex (selectIndex, true )
114114 }
115- currentIndex = intent.getIntExtra(KEY_INDEX , 0 )
116- currentIndex = min(currentIndex, photoAdapter.count)
117115 }
118116
119117 override fun finish () {
@@ -158,16 +156,16 @@ class PreviewActivity : AppCompatActivity() {
158156 checkBox = findViewById(R .id.check)
159157 llSelect = findViewById(R .id.llSelect)
160158 llSelect.setOnClickListener {
161- if (currentIndex < 0 ) return @setOnClickListener
162- val path: String = photoAdapter.getPath(currentIndex )
159+ if (currentPageIndex < 0 ) return @setOnClickListener
160+ val path: String = photoAdapter.getPath(currentPageIndex )
163161 val select: Boolean = ! selectAdapter.getList().contains(path)
164162 if (select && selectAdapter.getList().size >= selectLimit) {
165163 val hint: String = getString(R .string.pick_media_most_select_limit)
166164 val content: String = String .format(hint, selectLimit)
167165 Toast .makeText(baseContext, content, Toast .LENGTH_SHORT ).show()
168166 return @setOnClickListener
169167 }
170- switchSelectBox(select, currentIndex )
168+ switchSelectBox(select, path, currentPageIndex )
171169 selectAdapter.onPreviewChanged(path, selectedView)
172170 }
173171 // 选中列表
@@ -181,40 +179,43 @@ class PreviewActivity : AppCompatActivity() {
181179 photosView.addOnPageChangeListener(object : PagerListener () {
182180 override fun onPageSelected (position : Int ) {
183181 super .onPageSelected(position)
184- if (position == currentIndex) return
185- currentIndex = position
186- setCurrentIndex(position, false )
182+ if (position == currentPageIndex) return
183+ setCurrentPageIndex(position, false )
187184 }
188185 })
189186 photosView.adapter = photoAdapter
190187 // 状态更新
191- setCurrentIndex(currentIndex, true )
188+ val index = intent.getIntExtra(KEY_INDEX , 0 )
189+ setCurrentPageIndex(min(index, photoAdapter.count - 1 ), true )
192190 notifyBottomChanged(true )
193191 }
194192
195-
196193 private fun notifyBottomChanged (shown : Boolean ) {
194+ val views = arrayOf(rlBottom, selectedView)
195+ val vis = views.map { it.visibility }.toMutableList()
197196 if (selectLimit > 0 ) {
198- rlBottom.visibility = if (shown) View .VISIBLE else View .GONE
197+ vis[ 0 ] = if (shown) View .VISIBLE else View .GONE
199198 val emptySelect: Boolean = selectAdapter.getList().isEmpty()
200- selectedView.visibility = if (shown && ! emptySelect) View .VISIBLE else View .GONE
199+ vis[ 1 ] = if (shown && ! emptySelect) View .VISIBLE else View .GONE
201200 } else {
202- rlBottom.visibility = View .GONE
203- selectedView.visibility = View .GONE
201+ vis[ 0 ] = View .GONE
202+ vis[ 1 ] = View .GONE
204203 }
204+ views.forEachIndexed { i, v -> if (v.visibility != vis[i]) v.visibility = vis[i] }
205205 }
206206
207207 // 设置当前选中索引
208- private fun setCurrentIndex (index : Int , adjust : Boolean ) {
208+ private fun setCurrentPageIndex (index : Int , adjust : Boolean ) {
209+ currentPageIndex = index
209210 toolbar.title = " ${index + 1 } /${photoAdapter.count} "
210- val select: Boolean =
211- selectAdapter.onPreviewChanged(photoAdapter.getPath(index), selectedView)
212- switchSelectBox(select, index)
211+ val path = photoAdapter.getPath(index)
212+ val select: Boolean = selectAdapter.getList().contains(path)
213+ switchSelectBox(select, path, index)
214+ selectAdapter.onPreviewChanged(path, selectedView)
213215 photoAdapter.resetScale()
214216 photoAdapter.stopPlay()
215217 if (! adjust) return
216- val smooth = false
217- photosView.setCurrentItem(index, smooth)
218+ photosView.setCurrentItem(index, false )
218219 }
219220
220221 // 切换显示、隐藏标题等界面
@@ -246,15 +247,14 @@ class PreviewActivity : AppCompatActivity() {
246247 }
247248
248249 // 切换选择状态
249- private fun switchSelectBox (value : Boolean , index : Int ) {
250- checkBox.setImageResource(if (value) R .drawable.album_check_active else R .drawable.album_check_normal)
251- val path: String = photoAdapter.getPath(index)
250+ private fun switchSelectBox (select : Boolean , path : String , index : Int ) {
251+ checkBox.setImageResource(if (select) R .drawable.album_check_active else R .drawable.album_check_normal)
252252 photoAdapter.get(index)?.let { media ->
253253 val enable = PickControl .obtain().filter().invoke(media)
254254 llSelect.visibility = if (enable) View .VISIBLE else View .GONE
255255 }
256256 val contain: Boolean = selectAdapter.getList().contains(path)
257- if (value ) {
257+ if (select ) {
258258 if (! contain && selectAdapter.getList().add(path)) {
259259 notifyBottomChanged(true )
260260 selectAdapter.notifyDataSetChanged()
0 commit comments