美文网首页
Android 常用类的释放

Android 常用类的释放

作者: 主音King | 来源:发表于2019-10-25 14:00 被阅读0次

    场景在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();
    

    相关文章

      网友评论

          本文标题:Android 常用类的释放

          本文链接:https://www.haomeiwen.com/subject/vsiqyctx.html