2

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);
}
...
4

1 回答 1

0

永远不要对适配器进行活动或片段引用,因为它会创建循环依赖项,这意味着活动会引用适配器,而适配器会引用活动,因此无法留下内存。总是每一个都需要内存参考。改用回调方法,它将由适配器侦听器的活动实现。

使用此链接接受的答案 - > 来自适配器的回调

于 2019-04-12T20:26:26.600 回答