数据更改时的活动转移 [英] Activity transition when data changes
问题描述
我有图像适配器,其中每个项目都是用户图像,单击它会打开一个带有选定用户图像的新活动,因此我将图像标记为共享元素并使用活动转换。
I got images adapter where each item is user image, when clicked it opens a new activity with selected user image, so I mark the image as shared element and using activity transitions.
我在第二个活动中执行的部分操作会影响所有用户,因此适配器调用 notifyDataSetChanged
并将位置重置为列表顶部。
Part of the actions that I perform on the second activity effects all of the users, so the adapter calls notifyDataSetChanged
and reset the position to the top of the list.
这种情况会弄乱返回动画,当我关闭第二个活动并返回到列表时,其中的数据已更改,因此图像被动画化为错误
When this happens it mess up the return animation, when I close the second activity and return to the list the data in it been changed so the image get animated to the wrong cell.
我有两个问题:
- 我该怎么做才能重新映射动画到正确的单元格?所有单元格都具有相同的共享ID ...
- 如果我的用户在列表中不再可见,如何用不同的动画替换返回的动画?
推荐答案
我该怎么做才能将动画重新映射到正确的单元格?所有单元格都有相同的共享ID。
What can I do to remap the animation to the right cell? All the cells got the same shared id.
在第一个活动中,您应该具有一些用于指定启动第二个活动的项的键。假设您有一个 Map
唯一的 userId
s和 User
s,即 Map< Integer,User>
。
In the first activity you should have some key that specifies the item that launches second activity. Let's assume you have a Map
of unique userId
s and User
s, i.e. Map<Integer, User>
.
- 第二次启动时活动通过了地图中该
User
的键,我们假设它是42
。 (在地图42-> John Doe
中,您正在为John Doe
启动第二个活动)。 -
setExitSharedElementCallback()
并覆盖onMapSharedElements()
。
- When you launch second activity pass this
User
's key in the map, let's say it is42
. (In the map42 -> John Doe
, and you are launching second activity forJohn Doe
). setExitSharedElementCallback()
in the first activity and overrideonMapSharedElements()
.
override fun onMapSharedElements(names: MutableList<String>?,
sharedElements: MutableMap<String, View>?) {
// we will implement this in step 6
}
覆盖 onActivityReenter()
在第一次活动中,并使用 supportPostponeEnterTransition()
,以便在执行一些操作(例如,我们要滚动列表以显示该项目。)
Override onActivityReenter()
in first activity and postpone transition with supportPostponeEnterTransition()
, in order not to show transition until we've made some actions (e.g. we want to scroll the list in order to show the item).
在 SharedElementCallback :: onMapSharedElements()
检查天气情况下的捆绑包在步骤4中保存的code>是否为null。如果它不为null,则意味着您在第二个活动中做了一些事情,并且希望重新映射共享元素。这意味着您必须执行以下操作:
In SharedElementCallback::onMapSharedElements()
check weather the Bundle
that you have saved in the step 4 is null or not. If it is not null, that would mean that you have made something in second activity and you want remapping of shared elements to happen. This means you have to do something like this:
override fun onMapSharedElements(names: MutableList<String>?,
sharedElements: MutableMap<String, View>?) {
// `reenterBundle` is the `Bundle` you have saved in step 3
if (null != reenterBundle
&& reenterBundle!!.containsKey("KEY_FROM_ACTIVITY_2")
&& null != view) {
val key = reenterBundle!!.getInt("KEY_FROM_ACTIVITY_2");
val newSharedElement = ... // find corresponding view with the `key`
val newTransitionName = ... // transition name of the view
// clear previous mapping and add new one
names?.clear()
names?.add(newTransitionName)
sharedElements?.clear()
sharedElements?.put(newTransitionName, newSharedElement)
reenterBundle = null
} else {
// The activity is exiting
}
}
在第二个活动中,覆盖 finishAfterTransition()
:
override fun finishAfterTransition() {
val data = Intent()
data.putExtra("KEY_FROM_ACTIVITY_2", 42) // `42` is the original position that we passed to this activity via Intent when launching it
setResult(RESULT_OK, data)
super.finishAfterTransition()
}
如果我的用户在列表中不再可见,如何用其他动画替换返回动画?
In case that my user is no longer visible on the list, how can I replace the return animation with different animation?
您可以使其可见(例如通过滚动 RecyclerView
太多,使您的视图可见),或者您可以通过清除名称$来删除步骤6中的共享元素转换c $ c>和
sharedElements
,不要在其中添加任何内容。
You can either make it visible (e.g. by scrolling RecyclerView
so much, that your view becomes visible), or you can just remove shared elements transition in step 6 by clearing out names
and sharedElements
and not adding anything into them.
我希望您已经了解了该概念尽管看起来有些混乱,但它仍然有效。但作为对您的帮助,我可以从我编写的应用程序中共享一些代码:
I hope you've learned the concept how it works although it seems a bit messy. But as a help for you I can share some code from an app written by me:
这篇关于数据更改时的活动转移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!