recyclerview 位于附加到主要活动的(androidx)片段中。这是调用新活动的回收器适配器的一部分:
MainActivity activity;
@Override
public void onBindViewHolder(final MyRecyclerAdapter.ViewHolder viewHolder, final int position) {
final View background = viewHolder.background;
ViewCompat.setTransitionName(background,"background_" + position);
background.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(activity, DetailActivity.class);
Bundle options = ActivityOptionsCompat.makeSceneTransitionAnimation(activity, background, "background_" + position).toBundle();
activity.startActivity(intent, options);
}
}
}
这就是我得到的错误(对于我传递给 ActivityOptionsCompat 的每个共享元素一次):
E/BufferItemConsumer:[unnamed-29987-0] 无法释放缓冲区:未知错误 -1 (1)
每次您调用“finishAfterTransition();”时都会出现此错误 当前窗口失去焦点的错误
编辑: 正如 Mahabub Karim 建议的那样,我用回调替换了活动参考(不幸的是它不会更改错误消息):
//Adapter Class
OnItemClickListener onItemClickListener;
public static interface OnItemClickListener {
public abstract void onItemClicked(MyRecyclerAdapter.ViewHolder viewHolder, int position);
}
@Override
public void onBindViewHolder(final MyRecyclerAdapter.ViewHolder viewHolder, final int position) {
final View background = viewHolder.background;
ViewCompat.setTransitionName(background,"background_" + position);
background.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onItemClickListener.onItemClicked(viewHolder,position);
}
}
}
public void setListener(OnItemClickListener onItemClickListener){
this.onItemClickListener = onItemClickListener;
}
//MainActivity Class
...
@Override
public void onItemClicked(MyRecyclerAdapter.ViewHolder viewHolder, int position) {
final View background = viewHolder.background;
Intent intent = new Intent(this, DetailActivity.class);
Bundle options = ActivityOptionsCompat.makeSceneTransitionAnimation(this, background, "background_" + position).toBundle();
startActivity(intent, options);
}
...