1

I need to make a gallery of images. I make it with the help of RecyclerView. Project downloads images form the Internet, adds it to ImageView and than adds it to RecyclerView. But there are very big indents between items in RecyclerView and I don't know why does it happen. I thought that it's because of the Image dimension, but than added scaleType and nothing happened. Also I tried to add padding/margin = 0, but it also didn't help. Here's the ImageView where the image is placed:

<ImageView
        android:id="@+id/avatar"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:scaleType="centerCrop"
        android:padding="4dp"/>

It's an example of the image: https://image.ibb.co/jmHhB8/alicia_vikander_large.jpg

And here's a RecyclerView

<android.support.v7.widget.RecyclerView
        android:id="@+id/previewImages"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layoutManager="android.support.v7.widget.GridLayoutManager" />

This is the screen of my problem:

enter image description here

Here's a RecyclerView adapter class:

package asus.example.com.exercise3;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;

import java.util.List;

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {

    private final List<Actor> actors;
    private final LayoutInflater inflater;
    private final Context context;

    RecyclerViewAdapter(List<Actor> actors, Context context){
        this.context = context;
        this.actors = actors;
        inflater = LayoutInflater.from(context);
    }



    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new ViewHolder(inflater.inflate(R.layout.preview_image_layout, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        Actor actor = actors.get(i);
        Glide.with(context).load(actor.getSmallImage()).into(viewHolder.avatar);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return actors.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder{

        public final ImageView avatar;

        ViewHolder(@NonNull View itemView) {
            super(itemView);
            avatar = itemView.findViewById(R.id.avatar);
        }
    }

}

What's the matter?

UPD

I added such class:

package asus.example.com.exercise3;

import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.view.View;

    public class SpacesItemDecoration extends RecyclerView.ItemDecoration {
        private int space;

        SpacesItemDecoration(int space){
            this.space = space;
        }

        @Override
        public void getItemOffsets(Rect outRec, View view, RecyclerView parent, RecyclerView.State state){
            outRec.left = space;
            outRec.right = space;
            outRec.bottom = space;
            if (parent.getChildLayoutPosition(view) == 0){
                outRec.top = space;
            }
            else {
                outRec.top = space;
            }
        }
    }

But it also didn't help

Sergei Mikhailovskii
  • 2,100
  • 2
  • 21
  • 43
  • set gridmanager span count https://stackoverflow.com/questions/26666143/recyclerview-gridlayoutmanager-how-to-auto-detect-span-count – rajahsekar Feb 26 '19 at 19:22
  • You set your ImageView dimens to `100dp` and `centerCrop` does not do what you think it does. Are you trying to make it 2 columns and have the images stretch max width to fit the columns? – TWL Feb 26 '19 at 19:30
  • @TWL,there are should be 2 columns,but there's not only one line. If you will the activity you will see another photos, but another lines are not seen on the screen because of the indents – Sergei Mikhailovskii Feb 26 '19 at 20:28
  • @rajahsekar there's no problem with columns,there are big indents between the lines. How can I remove them? – Sergei Mikhailovskii Feb 26 '19 at 20:29
  • This is how you do grid https://stackoverflow.com/questions/40587168/simple-android-grid-example-using-recyclerview-with-gridlayoutmanager-like-the Also this https://stackoverflow.com/questions/28531996/android-recyclerview-gridlayoutmanager-column-spacing so I feel it's a duplicate. – CmosBattery Feb 26 '19 at 20:49
  • @CmosBattery, updated the question – Sergei Mikhailovskii Feb 26 '19 at 21:27

0 Answers0