focus the proper portrait photo at swiping the stripe

This commit is contained in:
tibbi 2019-10-02 22:13:04 +02:00
parent efee783e1f
commit 17003c6ab1
2 changed files with 32 additions and 0 deletions

View file

@ -19,6 +19,7 @@ class PortraitPhotosAdapter(val context: Context, val photos: ArrayList<String>,
RecyclerView.Adapter<PortraitPhotosAdapter.ViewHolder>() {
var currentSelectionIndex = -1
var views = HashMap<Int, View>()
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<String>,
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<String>,
.into(portrait_photo_item_thumbnail)
if (photo.isNotEmpty()) {
views[position] = this
setOnClickListener {
itemClick(position, x.toInt())
setCurrentPhoto(position)

View file

@ -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)