My problem is I am switching between List
and Grid
layout manager in grid view. The item just hold a ImageView
.
<layout>
<data>
<variable
name="photoUrl"
type="String"/>
</data>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:id="@+id/frameRootPhotos"
android:layout_height="wrap_content">
<com.talkinginvite.widgets.SquareImageView
android:layout_width="match_parent"
app:squareImage="@{photoUrl}"
android:scaleType="fitXY"
android:src="@drawable/placeholder_list"
android:layout_margin="@dimen/_6sdp"
app:placeholder="@{@drawable/placeholder_list}"
android:layout_height="wrap_content" />
</LinearLayout>
And I am loading image as.
@BindingAdapter(value = { "squareImage", "placeholder" }, requireAll = false)
public static void loadSquareImage(ImageView view, String url, Drawable placeholder){
if(!TextUtils.isEmpty(url)) {
RequestOptions requestOptions = new RequestOptions();
requestOptions.placeholder(placeholder);
requestOptions.diskCacheStrategy(DiskCacheStrategy.ALL);
requestOptions.transform(new RoundedCornersTransformation(AppUtil.getRoundedRadiusLarge(),0));
requestOptions.error(placeholder);
Glide.with(view).load(url)
.apply(requestOptions)
.into(view);
}
My problem is during switching sometimes ListView
shows the image in size of Grid View item. As you can see my list item is a square image and grid contain 3 columns.
Also I think during transformation sometimes Glide used old image and not the resultant image is dualy transformed and its showing blur and corner radius is double.
Below is how I am switching.
private void switchLayoutManager(){
if(isListView){
LinearLayoutManager layoutManager=new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false);
binding.rvPhotos.setLayoutManager(layoutManager);
}else{
GridLayoutManager layoutManager=new GridLayoutManager(getActivity(),3);
binding.rvPhotos.setLayoutManager(layoutManager);
}
}
Is there something wrong with my approach? How can I avoid such behaviour from Glide. Also is this the right way to switch layoutManager? Or I should use two RecyclerView
?
NOTE:- I also need to maintain cache that's the very basic requirement. So i can not skip the cache of Glide
.