当我从Internet下载图像时,如何从RecyclerView中删除缩进?

时间:2019-02-26 19:19:20

标签: android android-recyclerview

我需要制作一个图片库。我在RecyclerView的帮助下做到了。 Project从Internet下载图像,将其添加到ImageView,然后将其添加到RecyclerView。但是RecyclerView中项目之间的缩进很大,我不知道为什么会这样。我以为是因为图像尺寸,但是没有添加scaleType却什么也没发生。我也尝试添加padding / margin = 0,但这也无济于事。这是放置图像的ImageView:

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

这是图像的示例:https://image.ibb.co/jmHhB8/alicia_vikander_large.jpg

这是一个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" />

这是我的问题的屏幕:

enter image description here

这是RecyclerView适配器类:

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);
        }
    }

}

怎么了?

UPD

我添加了这样的课程:

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;
            }
        }
    }

但这也没有帮助

0 个答案:

没有答案