场景在Activity中使用了RecyclerView自然会用到adapter。
释放:
在Activity 的 onDestroy中写入:
if (mRecyclerView != null) {
mRecyclerView.setAdapter(null);
sateRecyclerView = null;
}
if (mAdapter != null) {
mAdapter.setSateOnclickListener(null);//是自定义click,如果没有就不用写
sateAdapter = null;
}
GridLayoutManager监听释放:因为GridLayoutManager监听默认是有值的,所以释放的是有最好设置回默认值比较好。
private GridLayoutManager gridLayoutManager;
//在需要释放的时候调用
public void releaseListen() {
if (gridLayoutManager != null) {
gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.DefaultSpanSizeLookup());
gridLayoutManager = null;
}
itemOnClickListener = null;
}
@Override
public void onAttachedToRecyclerView(final RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
gridLayoutManager = (GridLayoutManager) recyclerView.getLayoutManager();
if (gridLayoutManager != null) {
gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
return getItemViewType(position) == TYPE_FOOT ? gridLayoutManager.getSpanCount() : 1;
}
});
}
}
AnimatorSet动画释放
if (animatorSetSate != null) {
animatorSetSate.removeAllListeners();
animatorSetSate.cancel();
}
ArrayList等列表的释放:其实是列表中bean造成了泄漏,这样释放的是列表的持有,真正释放需要释放bean。
list.clear();
网友评论