对于makeSceneTransitionAnimation,有两个静态函数
public static ActivityOptionsCompat makeSceneTransitionAnimation(Activity activity,
View sharedElement, String sharedElementName)
和
public static ActivityOptionsCompat makeSceneTransitionAnimation(Activity activity,
Pair<View, String>... sharedElements)
第一个函数调用在Kotlin中正常工作,但在调用第二个时,这两个调用都会返回错误
val imageTransition = Pair<View, String>(imageView, imageView.getTransitionName());
val textTransition = Pair<View, String>(textView, textView.getTransitionName());
val transitionList = Array(2, { imageTransition });
transitionList[1] = textTransition;
val options = ActivityOptionsCompat.makeSceneTransitionAnimation(this, *transitionList);
和
val imageTransition = Pair<View, String>(imageView, imageView.getTransitionName());
val textTransition = Pair<View, String>(textView, textView.getTransitionName());
val options = ActivityOptionsCompat.makeSceneTransitionAnimation(this, imageTransition, textTransition);
是否有正确的方法让这项工作或者这是互操作的问题?
修改 添加了更改以确保它使用相同的类
val imageView : View = view.findViewById(android.R.id.icon);
val textView : View = view.findViewById(android.R.id.text1);
imageView.setTransitionName("imageTransition");
textView.setTransitionName("textTransition")
val imageTransition : android.support.v4.util.Pair<android.view.View, java.lang.String> = android.support.v4.util.Pair.create(imageView, imageView.getTransitionName() as java.lang.String);
val textTransition : android.support.v4.util.Pair<android.view.View, java.lang.String> = android.support.v4.util.Pair.create(textView, textView.getTransitionName() as java.lang.String);
val transitionList = Array(2, { imageTransition });
transitionList[1] = textTransition;
val options = ActivityOptionsCompat.makeSceneTransitionAnimation(this, *transitionList);
当前编译错误:
Error:(72, 84) The spread operator (*foo) may only be applied in a vararg position
Error:(72, 99) No value passed for parameter sharedElementName
和另一个
val imageView : View = view.findViewById(android.R.id.icon);
val textView : View = view.findViewById(android.R.id.text1);
imageView.setTransitionName("imageTransition");
textView.setTransitionName("textTransition")
val imageTransition : android.support.v4.util.Pair<android.view.View, java.lang.String> = android.support.v4.util.Pair.create(imageView, imageView.getTransitionName() as java.lang.String);
val textTransition : android.support.v4.util.Pair<android.view.View, java.lang.String> = android.support.v4.util.Pair.create(textView, textView.getTransitionName() as java.lang.String);
val options = ActivityOptionsCompat.makeSceneTransitionAnimation(this, imageTransition, textTransition);
当前编译错误:
Error:(72, 84) Type mismatch: inferred type is android.support.v4.util.Pair<android.view.View, java.lang.String> but android.view.View! was expected
Error:(72, 101) Type mismatch: inferred type is android.support.v4.util.Pair<android.view.View, java.lang.String> but kotlin.String! was expected
答案 0 :(得分:11)
答案是数组变量之前的*
符号:
import android.support.v4.util.Pair as AndroidPair
// ...
val pair1 = AndroidPair<View, String>(fab,
getString(R.string.transition_fab))
val pair2 = AndroidPair<View, String>(findViewById(R.id.app_bar),
getString(R.string.transition_appbar))
ActivityOptionsCompat.makeSceneTransitionAnimation(this@MyActivity,
*arrayOf(pair1, pair2)).toBundle();
答案 1 :(得分:3)
这对我有用:
import android.support.v4.util.Pair
// ...
val options = ActivityOptionsCompat.makeSceneTransitionAnimation(activity,
Pair<View, String>(image, image.transitionName),
Pair<View, String>(title, title.transitionName))
startActivity(intent, options.toBundle())
答案 2 :(得分:2)
可能是您意外使用kotlin.Pair
而不是android.util.Pair
。请将以下import指令添加到文件的开头:
import android.util.Pair
答案 3 :(得分:1)
这是我推荐的完整代码段:
companion object {
const val EXTRA_ITEM = "EXTRA_ITEM"
const val TRANSITION_NAME_IMAGE = "TRANSITION_NAME_IMAGE"
const val TRANSITION_NAME_TEXT = "TRANSITION_NAME_TEXT"
}
override fun onItemClick(pos: Int, item: Item, imageView: ImageView, textView: TextView) {
val intent = Intent(context, YourActivity::class.java)
intent.putExtra(EXTRA_ITEM, item)
intent.putExtra(
TRANSITION_NAME_IMAGE,
ViewCompat.getTransitionName(imageView)
)
intent.putExtra(
TRANSITION_NAME_TEXT,
ViewCompat.getTransitionName(textView)
)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
val optionsCompat = ActivityOptionsCompat.makeSceneTransitionAnimation(
context as Activity,
androidx.core.util.Pair<View,String>(imageView, imageView.transitionName),
androidx.core.util.Pair<View,String>(textView, textView.transitionName)
)
startActivity(intent, optionsCompat.toBundle())
} else {
startActivity(intent)
}
}
您还必须在活动中实现此交易方法,以返回上一步屏幕。
override fun onOptionsItemSelected(item: MenuItem): Boolean {
val id = item.itemId
if (id == android.R.id.home) {
supportFinishAfterTransition()
return true
}
return super.onOptionsItemSelected(item)
}
答案 4 :(得分:0)
关键是要像这样启动Pair
:Pair<View, String>(view, string)
否则会抱怨。