0

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.

Diaz diaz
  • 284
  • 1
  • 7
  • 21
  • Well Setting new Adapter each time seems working . Will get back if faced the problem again .But Setting new adapter each time seems overhead to me .Is there any elegant way to do this ? – Diaz diaz Jun 20 '18 at 11:39
  • try https://stackoverflow.com/q/42025083/8089770 – Vidhi Dave Jun 20 '18 at 11:43
  • How can signature will be help full in my case ? Can you provide an example with my code above ? And i can not use `DiskCacheStrategy.NONE` i need the cache . – Diaz diaz Jun 20 '18 at 12:13
  • 1
    Try this `view.layout(0, 0, 0, 0); Glide.with(view).load(url).apply(requestOptions).into(view);` – Zain Jan 06 '21 at 01:18

0 Answers0