I know that this question was asked many time and sorry for repeating it but I simply don't know what to do.
I am using Glide for loading images from server into my RecycleView
and it's listener so when my images are loaded, it should save int 1 and then in my fragment, where my RecycleView
is, I am getting that SharedPreferences int and check if it is not 1 than my RecycleView
xml go INVISIBLE/GONE and then finally I check if some of my RecycleView
are GONE/INVISIBLE all my layout should be gone, so that my layout is loaded after all my images are loaded.
But the problem is that my default value in my SharedPreference
is always triggered, and I don't know why...
I also tried with PreferenceManager.getDefaultSharedPreferences()
but nothing different.
My adapter glide:
Glide.with(context)
.load(listaSvihMembersa.get(i).getImageUrl())
.circleCrop()
.placeholder(R.drawable.ic_joint)
.listener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
SharedPreferences sp = context.getSharedPreferences("mortgage", MODE_PRIVATE);
sp.edit().putInt("motgageList", 1).apply();
return true;
}})
.into(binding.detailedListIMG);
}
and my fragment where I get SharedPref
private void setMortgageRecycleView(List<MortgageLiabilities> mortgageLiabilitiesList) {
mortgageRecycleView = binding.rvMortgageList;
mortgageRecycleView.animate().translationY(0);
MortgageAdapter adapter = new MortgageAdapter(mortgageLiabilitiesList, context, this, listaSvihMembersa,mortgageRecycleView);
mortgageRecycleView.setAdapter(adapter);
SharedPreferences sp = context.getSharedPreferences("mortgage",MODE_PRIVATE);
int idNmbr = sp.getInt("motgageList", 0);
if (idNmbr != 1){
mortgageRecycleView.setVisibility(View.INVISIBLE);
Log.i(TAG, "visibility motgageListe: " + mortgageRecycleView.getVisibility());
}
else {
mortgageRecycleView.setVisibility(View.VISIBLE);
Log.i(TAG, "visibility motgageListe: " + mortgageRecycleView.getVisibility());
}
mortgageRecycleView.setLayoutManager(new LinearLayoutManager(context));
mortgageRecycleView.setHasFixedSize(true);
}
Here, I check if any of Recycleviews
are invisible:
if (mortgageRecycleView.getVisibility() == View.INVISIBLE || loansRecycleViews.getVisibility() == View.INVISIBLE
|| otherRecycleView.getVisibility() == View.INVISIBLE || creditCardRecycleView.getVisibility() == View.INVISIBLE){
binding.constraintLayout.setVisibility(View.GONE);
Toast.makeText(context, "Ne moze da ucita,root view je GONE", Toast.LENGTH_SHORT).show();
}
EDIT: My shared_pref.xml file
<map>
<int name="motgageList" value="1" />
</map>
Glide is executing an asynchronous task, so the most of the cases your code will reach int idNmbr = sp.getInt("motgageList", 0);
before the callback onResourceReady
is called.
Shared preferences are not a good choice here, you can use a custom callback and pass it to your adapter.