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:
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
