直接依赖:
- 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;
- }