improve gallery transition and fix crash

This commit is contained in:
Johan von Forstner
2020-04-24 20:37:03 +02:00
parent 12329f82b3
commit 78421ec79f
3 changed files with 35 additions and 10 deletions

View File

@@ -14,7 +14,6 @@ import com.ortiz.touchview.TouchImageView
import net.vonforst.evmap.MapsActivity
import net.vonforst.evmap.R
import net.vonforst.evmap.adapter.GalleryAdapter
import net.vonforst.evmap.adapter.galleryTransitionName
import net.vonforst.evmap.api.goingelectric.ChargerPhoto
import net.vonforst.evmap.databinding.FragmentGalleryBinding
import net.vonforst.evmap.viewmodel.GalleryViewModel
@@ -103,15 +102,7 @@ class GalleryFragment : Fragment(), MapsActivity.FragmentCallback {
) {
if (isReturning) {
val currentPage = currentPage ?: return
val index = binding.gallery.currentItem
if (startingPosition != currentPosition) {
names.clear()
names.add(galleryTransitionName(index))
sharedElements.clear()
sharedElements[galleryTransitionName(index)] = currentPage
}
sharedElements[names[0]] = currentPage
}
}
}

View File

@@ -10,6 +10,7 @@ import android.content.res.Configuration
import android.os.Bundle
import android.view.*
import android.view.inputmethod.InputMethodManager
import androidx.core.app.SharedElementCallback
import androidx.core.content.ContextCompat
import androidx.core.view.updateLayoutParams
import androidx.databinding.DataBindingUtil
@@ -24,6 +25,7 @@ import androidx.navigation.fragment.findNavController
import androidx.navigation.ui.setupWithNavController
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.transition.TransitionInflater
import com.google.android.gms.location.FusedLocationProviderClient
import com.google.android.gms.location.LocationServices
import com.google.android.gms.maps.CameraUpdateFactory
@@ -106,6 +108,10 @@ class MapFragment : Fragment(), OnMapReadyCallback, MapsActivity.FragmentCallbac
insets
}
setExitSharedElementCallback(exitElementCallback)
exitTransition = TransitionInflater.from(requireContext())
.inflateTransition(R.transition.map_exit_transition)
return binding.root
}
@@ -495,4 +501,20 @@ class MapFragment : Fragment(), OnMapReadyCallback, MapsActivity.FragmentCallbac
override fun getRootView(): View {
return root
}
private val exitElementCallback: SharedElementCallback = object : SharedElementCallback() {
override fun onMapSharedElements(
names: MutableList<String>,
sharedElements: MutableMap<String, View>
) {
// Locate the ViewHolder for the clicked position.
val position = galleryVm.galleryPosition.value ?: return
val vh = binding.gallery.findViewHolderForAdapterPosition(position)
if (vh?.itemView == null) return
// Map the first shared element name to the child ImageView.
sharedElements[names[0]] = vh.itemView
}
}
}