从编辑配置文件片段更改图片时,更新导航抽屉中的配置文件图片

时间:2017-03-22 16:53:37

标签: android android-fragments sharedpreferences android-navigation-drawer android-glide

我有一个Android应用程序,其中有4个片段,带有导航抽屉和个人资料照片,但事情是当我更改/更新配置文件片段中的个人资料图片时,个人资料图片正在更新此片段并且我也正在保存图片共享首选项中的URL(也上传到服务器),但当我回到Home Fragment时,导航抽屉中的个人资料图片不会使用最新图片进行更新。我正在使用Glide lib。为了加载配置文件pic,此图像正在加载存储在共享首选项中的URL。但当我关闭我的应用程序并打开导航抽屉更新中的个人资料图片与最新的照片。我不能要求用户关闭应用程序并在编辑配置文件片段中更改其个人资料照片时打开。有没有办法解决这个问题?

 //getting image URL from Shared Preferences.
       imgUrl = Prefrences.getProfile_picture(HomeActivity.this);
       loadImageUrl(imgUrl);

//loading image in navigation drawer with glide in Main Activity
private void loadImageUrl(String imgUrl) {
    if (imgUrl != null && !imgUrl.isEmpty()) {
        Glide.with(this).load(imgUrl).placeholder(R.drawable.avatar)
                .crossFade()
                .thumbnail(0.5f)
                .bitmapTransform(new CircleTransform(this))
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(img_profile);
    } else { 
       getUserDetails();  <-- this method is called when imgUrl is Null,
    }

}

2 个答案:

答案 0 :(得分:1)

您可以在创建导航活动时调用导航视图刷新数据....

将数据绑定部分与onCreate()

中的调用分开
private void updateNavHeaderView(){
View headerView = navigationView.inflateHeaderView(R.layout.nav_header);
        ImageView ivUserProfilePhoto = (ImageView) headerView.findViewById(R.id.ivUserProfilePhoto);
        TextView userName = (TextView) headerView.findViewById(R.id.userName);

        if (user != null && user.getUser_image() != null && !user.getUser_image().equals("")) {
            Picasso.with(MainActivity.this)
                    .load(Config.BASE_IMAGE_URL+"/"+user.getUser_image())
                    .placeholder(R.drawable.user_profile)
                    .resize(avatarSize, avatarSize)
                    .centerCrop()
                    .transform(new CircleTransformation())
                    .into(ivUserProfilePhoto);
        } else {
            ivUserProfilePhoto.setImageDrawable(getResources().getDrawable(R.drawable.user_profile));
        }
        }

答案 1 :(得分:0)

Glide可能正在缓存你的图像。

在此处查看前2个答案 Remove image from cache in Glide library

相关问题