【问题标题】:Share element between recyclerview and toolbar through fragments通过片段在recyclerview和工具栏之间共享元素
【发布时间】: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


【解决方案1】:

我找到了解决办法。

这个问题确实与我的 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
        }
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-13
    • 2015-05-15
    • 2018-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多