From 17003c6ab1a8eeeabcaaccf34f60801901169216 Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 2 Oct 2019 22:13:04 +0200 Subject: [PATCH] focus the proper portrait photo at swiping the stripe --- .../pro/adapters/PortraitPhotosAdapter.kt | 7 ++++++ .../gallery/pro/fragments/PhotoFragment.kt | 25 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/adapters/PortraitPhotosAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/adapters/PortraitPhotosAdapter.kt index 708a7c01b..7bf330e1c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/adapters/PortraitPhotosAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/adapters/PortraitPhotosAdapter.kt @@ -19,6 +19,7 @@ class PortraitPhotosAdapter(val context: Context, val photos: ArrayList, RecyclerView.Adapter() { var currentSelectionIndex = -1 + var views = HashMap() private var strokeBackground = context.resources.getDrawable(R.drawable.stroke_background) private val itemWidth = context.resources.getDimension(R.dimen.portrait_photos_stripe_height).toInt() @@ -37,9 +38,14 @@ class PortraitPhotosAdapter(val context: Context, val photos: ArrayList, if (currentSelectionIndex != position) { currentSelectionIndex = position notifyDataSetChanged() + views[position]?.performClick() } } + fun performClickOn(position: Int) { + views[position]?.performClick() + } + inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) { fun bindView(photo: String, position: Int): View { itemView.apply { @@ -67,6 +73,7 @@ class PortraitPhotosAdapter(val context: Context, val photos: ArrayList, .into(portrait_photo_item_thumbnail) if (photo.isNotEmpty()) { + views[position] = this setOnClickListener { itemClick(position, x.toInt()) setCurrentPhoto(position) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/fragments/PhotoFragment.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/fragments/PhotoFragment.kt index ac9fa40ac..2f4530523 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/fragments/PhotoFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/fragments/PhotoFragment.kt @@ -16,6 +16,7 @@ import android.os.Bundle import android.os.Handler import android.util.DisplayMetrics import android.view.LayoutInflater +import android.view.MotionEvent import android.view.View import android.view.ViewGroup import android.widget.RelativeLayout @@ -470,6 +471,8 @@ class PhotoFragment : ViewPagerFragment() { setupStripeBottomMargin() val coverIndex = getCoverImageIndex(paths) + setupStripeUpListener(adapter, screenWidth, itemWidth) + mView.photo_portrait_stripe.onGlobalLayout { mView.photo_portrait_stripe.scrollBy((coverIndex - fakeItemsCnt) * itemWidth, 0) adapter.setCurrentPhoto(coverIndex) @@ -520,6 +523,28 @@ class PhotoFragment : ViewPagerFragment() { return coverIndex } + private fun setupStripeUpListener(adapter: PortraitPhotosAdapter, screenWidth: Int, itemWidth: Int) { + mView.photo_portrait_stripe.setOnTouchListener { v, event -> + if (event.action == MotionEvent.ACTION_UP || event.action == MotionEvent.ACTION_CANCEL) { + var closestIndex = -1 + var closestDistance = Integer.MAX_VALUE + val center = screenWidth / 2 + for ((key, value) in adapter.views) { + val distance = Math.abs(value.x.toInt() + itemWidth / 2 - center) + if (distance < closestDistance) { + closestDistance = distance + closestIndex = key + } + } + + Handler().postDelayed({ + adapter.performClickOn(closestIndex) + }, 100) + } + false + } + } + private fun openPanorama() { Intent(context, PanoramaPhotoActivity::class.java).apply { putExtra(PATH, mMedium.path)