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

本文介绍了如何解决Matisse库在切换Android相册时出现空白的bug。通过下载源码,导入项目,修改MatisseActivity和MediaSelectionFragment的相关方法,以及在AlbumCollection中添加销毁加载器的方法,最终在MatisseActivity的onDestroy中调用,成功修复了该问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目地址: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;
    }

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值