我对共享元素有一个奇怪的行为,我不知道这是对共享元素的误解还是错误的实现。 我查了一下谷歌,似乎没有人遇到我遇到的问题。
让我解释一下。我有两个 Fragments,Fragment A 包含一个 RecyclerView,Fragment B 是 recyclerview 项目的详细 View 。两者都有一个包含 TextView 的自定义工具栏。
我想将A 中recyclerview 项目的textview 共享到B 的工具栏textview。目前,输入共享元素转换正在工作(A-->B)但它在其他方面不起作用( A<--B)。
在返回过渡期间,B 的工具栏 textview 停留在工具栏中,并随着 B 返回过渡而消失。 但是共享元素转换有效,并且另一个 TextView 从 A 的回收 View 顶部出现并完成它的工作。
这就是问题所在。 onBackPressed 之后,不再共享工具栏 TextView ,并制作此工具栏 TextView 的副本并制作动画(仅在 recyclerview 中,它不是来自工具栏)而不是共享 B 工具栏 TextView 到一个 recyclerview item
我不明白问题出在哪里。过渡名称很好,否则动画无法工作。任何的想法? (我在 Kotlin 下编码)
fragment Activity
override fun onBackPressed() {
if(supportFragmentManager.backStackEntryCount > 1){
supportFragmentManager.popBackStackImmediate()
} else {
super.onBackPressed()
}
}
fragment 适配器 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)
}
}
}
fragment 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)
}
fragment 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
}
最佳答案
我找到了解决方案。
这个问题确实与我的 RecyclerView 有关。 我从来没有说过我的 recyclerview 在出现时也是动画的。 当 recyclerview 出现时我有一个输入动画,当用户滚动时有一个动画。我禁用了两者并且它工作正常。 现在,我只需要找到一种方法在返回时禁用 RecyclerView 动画,一切都会好起来的。
感谢那些阅读我并试图帮助我的人。
我用来解决问题的代码:
frag.setEnterSharedElementCallback(object : SharedElementCallback() {
override fun onSharedElementEnd(sharedElementNames: MutableList<String>?, sharedElements: MutableList<View>?, sharedElementSnapshots: MutableList<View>?) {
super.onSharedElementEnd(sharedElementNames, sharedElements, sharedElementSnapshots)
viewAdapter.mApplyAnim = true
}
})
关于android - 通过 fragments 在 recyclerview 和 toolbar 之间共享元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51149782/