记录Android 知乎图片选择器Matisse的注意事项

项目地址:GitHub - zhihu/Matisse: A well-designed local image and video selector for Android:fireworks: A well-designed local image and video selector for Android - GitHub - zhihu/Matisse: A well-designed local image and video selector for Androidhttps://github.com/zhihu/Matisse

直接依赖:

repositories {
    jcenter()
}

dependencies {
    implementation 'com.zhihu.android:matisse:$latest_version'
}

存在问题:默认打开本地图库,全部显示正常,但是切换到其他相册,空白展示;

目前作者还未修复此问题,可通过以下方式进行修改:

(1)将源码下载下来;

(2)使用import module的方式,导入到项目中;

(3)找到MatisseActivity,onAlbumSelected()方法中:

private void onAlbumSelected(Album album) {
        if (album.isAll() && album.isEmpty()) {
            mContainer.setVisibility(View.GONE);
            mEmptyView.setVisibility(View.VISIBLE);
        } else {
            mContainer.setVisibility(View.VISIBLE);
            mEmptyView.setVisibility(View.GONE);
            Fragment fragment = MediaSelectionFragment.newInstance(album);

            // 修复matisse源码存在的,切换其他相册空白问题
            Fragment oldFragment = getSupportFragmentManager().findFragmentByTag(MediaSelectionFragment.class.getSimpleName());
            if (oldFragment instanceof MediaSelectionFragment) {
                MediaSelectionFragment newFragment = (MediaSelectionFragment) oldFragment;
                newFragment.destroyManagerLoader();
            }
            getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.container, fragment, MediaSelectionFragment.class.getSimpleName())
                    .commitAllowingStateLoss();
        }
    }

(4)在MediaSelectionFragment中添加:

 public void destroyManagerLoader() {
        mAlbumMediaCollection.onDestroy();
    }

(5)在AlbumCollection添加:

public void onDestroy() {
        if (mLoaderManager != null) {
            mLoaderManager.destroyLoader(LOADER_ID);
        }
        mCallbacks = null;
    }

(6)在MatisseActivity onDestroy中调用即可:

 @Override
    protected void onDestroy() {
        super.onDestroy();
        mAlbumCollection.onDestroy();
        mSpec.onCheckedListener = null;
        mSpec.onSelectedListener = null;
    }

猜你喜欢

转载自blog.csdn.net/u010231454/article/details/125507625