【发布时间】:2018-07-03 08:20:01
【问题描述】:
我对共享元素有一种奇怪的行为,我不知道这是对共享元素的误解还是错误的实现。 我在谷歌上查了一下,似乎没有人遇到我遇到的问题。
让我解释一下。我有两个 Fragment,Fragment A 包含一个 RecyclerView,Fragment B 是 recyclerview 项目的详细视图。两者都有一个包含 TextView 的自定义工具栏。
我想将A中recyclerview项目的textview分享到B的工具栏textview。目前,输入共享元素转换正在工作(A-->B),但在其他方式下不起作用( AB)。
在返回过渡期间,B 的工具栏文本视图停留在工具栏中,并随着 B 返回过渡消失。 但是共享元素转换有效,另一个文本视图从 A 的回收器视图的顶部出现并完成了它的工作。
这就是问题所在。在 onBackPressed 之后,不再共享工具栏文本视图,并制作了此工具栏文本视图的副本并制作动画(仅在 recyclerview 中,它不是来自工具栏),而不是共享 B 工具栏文本视图到 A recyclerview 项目
我不明白问题出在哪里。过渡名称很好,否则动画无法工作。任何想法? (我在 Kotlin 下编码)
片段活动
override fun onBackPressed() {
if(supportFragmentManager.backStackEntryCount > 1){
supportFragmentManager.popBackStackImmediate()
} else {
super.onBackPressed()
}
}
片段 A 适配器 ViewHolder
class AnimationRecyclerViewHolder(val view: View, val listener: AnimationRecyclerCallBack) : RecyclerView.ViewHolder(view) {
val text: TextView = view.animation_recycler_text
fun bind(data: AnimationRecyclerData) {
text.text = data.description
text.transitionName = "textTransitionName$layoutPosition"
view.setOnClickListener {
listener.callback(data, view)
}
}
}
片段 A
override fun callback(data: AnimationRecyclerData, view: View) {
val frag = AnimationPageFragment.newInstance(data.type, this)
val transac = activity!!.supportFragmentManager.beginTransaction()
transac.shareText(view.animation_recycler_text, context!!, this, frag)
transac.replace(activity!!.fragContainerCatalogue.id, frag).addToBackStack(null).commit()
}
shareText 方法
fun FragmentTransaction.shareText(textview: TextView, context: Context, currentFrag: AbstractAnimationFragment, nextFrag: AbstractAnimationFragment) : FragmentTransaction {
val sharedTransitionName = textview.transitionName
val bundle = nextFrag.arguments ?: Bundle()
bundle.putString("sharedTransitionKey", sharedTransitionName)
bundle.putString("nextTitle", textview.text.toString())
nextFrag.arguments = bundle
nextFrag.enterTransition = Fade()
nextFrag.sharedElementEnterTransition = nextFrag.createShareTransition(sharedTransitionName, currentFrag.context!!)
currentFrag.activity!!.window.sharedElementsUseOverlay = false
return this.addSharedElement(textview, sharedTransitionName)
}
片段 B
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
var rootView = inflater.inflate(R.layout.animation_page_fragment_content, container, false)
toolbar = inflatedView.findViewById(R.id.toolbarShared)
titleToolbar = inflatedView.findViewById(R.id.toolbarTitleShared)
toolbar.setBackgroundColor(Color.RED)
titleToolbar.textSize = 25f
titleToolbar.setTextColor(Color.BLACK)
val bundle = this.arguments ?: Bundle()
val transitionName = bundle.getString("sharedTransitionKey")
val titleName = bundle.getString("nextTitle")
titleToolbar.text = titleName
titleToolbar.transitionName = transitionName
activity!!.window.sharedElementsUseOverlay = true
sharedElementEnterTransition = createShareTransition(titleToolbar.transitionName, context!!)
return rootView
}
【问题讨论】:
-
发布一些代码可能有助于我们找出问题所在。请说明您是如何实现转换的。
-
看起来您只是在设置一个 sharedElementEnterTransition - 您是否缺少 sharedElementReturnTransition?这指定了当用户点击后退按钮时视图如何向后移动。
-
根据文档 (developer.android.com/reference/android/support/v4/app/Fragment),如果没有设置 sharedElementReturnTransition 的值,则默认使用与 setSharedElementEnterTransition(Object) 相同的值
-
是的。那时我唯一能想到的是1)确保你的片段都是'android.support.v4.app.Fragment'和/或2)popBackStackImmediate()可能会取消你的过渡动画,你可以尝试只使用popBackStack() (不过,我找不到任何关于此的文档)
-
我一开始使用的是 popBackStack(),但你知道什么时候发生了奇怪的事情,你绝望了,你尝试了像 popBackStackImmediate 这样的愚蠢改变,但没有任何改变。当我编写动画时,我曾经读过github.com/alexjlockwood/custom-lollipop-transitions,因为唯一的区别是使用工具栏,也许这就是问题的根源。
标签: android android-fragments android-recyclerview android-toolbar android-transitions