1

Recently I'm playing around with some popular android libs, and I need a help with StaggeredGridLayout & Fresco.

Let me show you what I'm getting wrong

As you can see, these images height is streched

I've tried changing layout_height params in XML/via java, but I'm not getting good results.

Everything is up on my Github repo

Some snippets

public class GiphyView
    extends RecyclerView
    implements GiphyPresenter.ViewBind {

@Inject
public GiphyPresenter mGiphyPresenter;

private GiphyAdapter mAdapter;

public GiphyView(Context context) {
    super(context);
    initView(context);
}

public GiphyView(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    initView(context);
}

public GiphyView(Context context, @Nullable AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    initView(context);
}

private void initView(Context context) {
    CustomApplication.get(context).getGiphyComponent().inject(this);

    mAdapter = new GiphyAdapter();
    setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
    setAdapter(mAdapter);
}

@Override
protected void onAttachedToWindow() {
    super.onAttachedToWindow();
    mGiphyPresenter.attachVu(this);
}

@Override
protected void onDetachedFromWindow() {
    ImagePipeline imagePipeline = Fresco.getImagePipeline();
    imagePipeline.clearCaches();

    mGiphyPresenter.detachVu();

    super.onDetachedFromWindow();
}

@Override
public void notifyRangeInserted(int start, int count) {
    mAdapter.notifyItemRangeInserted(start, count);
}

private class GiphyAdapter extends RecyclerView.Adapter<GiphyViewHolder> {
    @Override
    public GiphyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new GiphyViewHolder(
                LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.giphy_cell, parent, false));
    }

    @Override
    public void onBindViewHolder(GiphyViewHolder holder, int position) {
        holder.setGif(mGiphyPresenter.getItemImage(position));
    }

    @Override
    public int getItemCount() {
        return mGiphyPresenter.getSize();
    }
}

class GiphyViewHolder extends ViewHolder {
    @BindView(R.id.gif)
    SimpleDraweeView mGif;

    GiphyViewHolder(View itemView) {
        super(itemView);
        ButterKnife.bind(this, itemView);
    }

    void setGif(String url) {
        /*ImageRequest request = ImageRequestBuilder.newBuilderWithSource(Uri.parse(url))
                .setRotationOptions(RotationOptions.autoRotate())
                .setLowestPermittedRequestLevel(ENCODED_MEMORY_CACHE)
                .build();*/

        DraweeController controller = Fresco.newDraweeControllerBuilder()
                .setUri(url)
                .setAutoPlayAnimations(true)
                //.setImageRequest(request)
                .build();
        mGif.setController(controller);
    }
}

}

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fresco="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    card_view:cardCornerRadius="6dp"
    card_view:cardUseCompatPadding="true">

    <!-- 4:3 aspect ratio
           fresco:viewAspectRatio="1.33" -->
    <com.facebook.drawee.view.SimpleDraweeView
        android:id="@+id/gif"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        fresco:failureImage="@drawable/ic_error_black_24dp"
        fresco:placeholderImage="@drawable/ic_android_black_24dp"/>
</android.support.v7.widget.CardView>

PSA: I've already tried to play around with wrap_content, but got no luck (yes, I know Fresco doesn't support it).

1 Answers1

0

I guess you need to set the dimensions of the parent card view to wrap_content and you do not need the parent LinearLayout. Just use something like:

<android.support.v7.widget.CardView
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:card_view="http://schemas.android.com/apk/res-auto"
  android:id="@+id/card_view"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  card_view:cardCornerRadius="6dp"
  card_view:cardUseCompatPadding="true">

  <com.facebook.fresco.SimpleDraweeView ... />
</android.support.v7.widget.CardView>

I have added a sample app that shows how to use CardView in a recycler view with a grid layout here: https://github.com/facebook/fresco/blob/master/samples/showcase/src/main/java/com/facebook/fresco/samples/showcase/drawee/DraweeRecyclerViewFragment.java

Alexander Oprisnik
  • 1,212
  • 9
  • 9
  • Thank you for responding, but I've already tried it. And setting layout_width="wrap_content" doesn't work, as Fresco doesn't support wrap content (well, I coul add it specifying a 4:3 view) – MaccheroniCoding Feb 21 '17 at 18:14
  • You have to change the parent `CardView` to use wrap content and keep the Drawee view with match_parent and aspect ratio. – Alexander Oprisnik Feb 21 '17 at 18:30
  • [with your config I get this](http://i.imgur.com/3TWV6fe.png), without cardView there are still [these withe spaces](https://imgur.com/JWXc9Jz) – MaccheroniCoding Feb 21 '17 at 22:41
  • Did you set an aspect ratio and set the Drawee height to wrap content? – Alexander Oprisnik Feb 21 '17 at 22:51
  • Yes, what's strange is that with other libraries like Picasso or Glide it's all displayed well – MaccheroniCoding Feb 22 '17 at 10:45
  • what if you set a fixed height? e.g. 100dp – Alexander Oprisnik Feb 22 '17 at 11:09
  • Nothing changes, I should ask to Fresco support via Github – MaccheroniCoding Feb 22 '17 at 16:11
  • I am actually working on Fresco and Stackoverflow is the right place for these kind of questions. Can you provide us with a small example so that we can reproduce the problem? Your sample code above has a lot of unrelated things and for example the layout XML is missing a closing tag. Also, why did you wrap the `CardView` with a `LinearLayout`? – Alexander Oprisnik Feb 22 '17 at 16:26
  • Well that LinearLayout was unintended, my bad, and stackoverflow messed up my snippets. As an example just try to start Giphy's activity, as soon as the images are downloaded I get this strange pagination. PSA: resolved, the culprit was linearLayout at root, Thanks for the support! – MaccheroniCoding Feb 22 '17 at 20:12
  • Glad to see that is resolved now. I've added a sample for this and also updated my answer. Sample: https://github.com/facebook/fresco/blob/master/samples/showcase/src/main/java/com/facebook/fresco/samples/showcase/drawee/DraweeRecyclerViewFragment.java – Alexander Oprisnik Feb 23 '17 at 11:50