如何删除recycleview中图像之间的间距?

时间:2016-06-17 08:00:27

标签: android imageview android-recyclerview

您想问一下是否有人知道什么是'在图像之间有如此大的间距的recycleview错了吗?以下是我尝试使用保证金来控制空格的方法,但它似乎不是解决我问题的正确方法。

<android.support.v7.widget.RecyclerView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/galleryRecyclerView"
    android:clipToPadding="false"
    android:layout_below="@id/displayGalleryView"
    android:longClickable="false" />

图片xml:

  <ImageView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_marginLeft="-70dp"
        android:id="@+id/imageGalleryView"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

我使用marginLeft来控制空间,并且在我整合其他功能之前它已经工作了,它很奇怪。

Image here

    mRecyclerView = (RecyclerView) findViewById(R.id.galleryRecyclerView);
    LinearLayoutManager layoutManager = new LinearLayoutManager(this);
    layoutManager.setOrientation(layoutManager.HORIZONTAL);
    RecyclerView.LayoutParams params = new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.WRAP_CONTENT);
    params.setMargins(0, 0, 0, 0);
    layoutManager.canScrollHorizontally();
    mRecyclerView.setLayoutManager(layoutManager);

    mRecyclerView.addItemDecoration(new RecyclerViewItemDecorator(1));

    RecyclerView.Adapter imageAdapter = new GalleryImageAdapter(mGalleryFolder);
    mRecyclerView.setAdapter(imageAdapter);

1 个答案:

答案 0 :(得分:0)

您可以减少回收商视图中项目之间的差距......

将此作为内部类使用。

public class RecyclerViewItemDecorator extends RecyclerView.ItemDecoration {
        private int spaceInPixels;

        public RecyclerViewItemDecorator(int spaceInPixels) {
            this.spaceInPixels = spaceInPixels;
        }

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

呼叫

list.addItemDecoration(new RecyclerViewItemDecorator(1));

在设置适配器之前