如何在Recyclerview中删除此填充?

时间:2016-09-24 10:10:11

标签: android android-recyclerview

代码:

private void configViews(View view) {
    int spanCount = 3; // 3 columns
    int spacing = 3;
    boolean includeEdge = true;
    mRecyclerView = (RecyclerView) view.findViewById(R.id.rv_flower_red);
    mRecyclerView.addItemDecoration(new GridSpacingItemDecoration(spanCount, spacing, includeEdge));
    mRecyclerView.setHasFixedSize(true);
    mRecyclerView.setRecycledViewPool(new RecyclerView.RecycledViewPool());
    mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity().getApplicationContext(), spanCount));
    mFlowerAdapter = new FlowerAdapter_grid(this);
    mRecyclerView.setAdapter(mFlowerAdapter);
}

public class GridSpacingItemDecoration extends RecyclerView.ItemDecoration {

    private int spanCount;
    private int spacing;
    private boolean includeEdge;

    public GridSpacingItemDecoration(int spanCount, int spacing, boolean includeEdge) {
        this.spanCount = spanCount;
        this.spacing = spacing;
        this.includeEdge = includeEdge;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        int position = parent.getChildAdapterPosition(view); // item position
        int column = position % spanCount; // item column

        if (includeEdge) {
            outRect.left = spacing - column * spacing / spanCount; // spacing - column * ((1f / spanCount) * spacing)
            outRect.right = (column + 1) * spacing / spanCount; // (column + 1) * ((1f / spanCount) * spacing)

            if (position < spanCount) { // top edge
                outRect.top = spacing;
            }
            outRect.bottom = spacing; // item bottom
        } else {
            outRect.left = column * spacing / spanCount; // column * ((1f / spanCount) * spacing)
            outRect.right = spacing - (column + 1) * spacing / spanCount; // spacing - (column + 1) * ((1f /    spanCount) * spacing)
            if (position >= spanCount) {
                outRect.top = spacing; // item top
            }
        }
    }
}

该代码的核心来自https://stackoverflow.com/a/30701422/6736285

但是,当我使用该代码时,enter image description here

Gridlayout在图像的顶部或底部有填充。

如您所见,垂直填充太多了。

问题:如何删除该填充?请你告诉我答案吗?

这是我的 FlowerAdapter_grid:

public class FlowerAdapter_grid extends RecyclerView.Adapter<FlowerAdapter_grid.Holder> {

    private static final Object TAG = FlowerAdapter_grid.class.getSimpleName();
    private final FlowerClickListener mListener;
    private List<Flower> mFlowers;
    public FlowerAdapter_grid(FlowerClickListener listener) {
        mFlowers = new ArrayList<>();
        mListener = listener;
    }
    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        View row = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_item_grid, null, false);
        return new Holder(row);
    }
    @Override
    public void onBindViewHolder(Holder holder, int position) {
        Flower currFlower = mFlowers.get(position);
        Glide.with(holder.itemView.getContext())
                .load(currFlower.getImage())
                .thumbnail(0.5f)
                .override(300, 300)
                .centerCrop()
                .into(holder.mImage);
    }

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

    public void addFlower(Flower flower) {
        mFlowers.add(flower);
        notifyDataSetChanged();
    }
    public void clear(){
        mFlowers.clear();
        notifyDataSetChanged();
    }

    public Flower getSelectedFlower(int position) {
        return mFlowers.get(position);
    }

    public class Holder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private ImageView mImage;
        public Holder(View itemView) {
            super(itemView);
            mImage = (ImageView) itemView.findViewById(R.id.iv_photo);
            itemView.setOnClickListener(this);
        }
        @Override
        public void onClick(View view) {
            mListener.onClick(getLayoutPosition());
        }
    }

    public interface FlowerClickListener {
        void onClick(int position);
    }
}

row_item_grid

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/iv_photo"
        />
</LinearLayout>

我的片段布局

<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/swipeContainer_red"
    >
    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_flower_red"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="none">
    </android.support.v7.widget.RecyclerView>
</android.support.v4.widget.SwipeRefreshLayout>

尝试后,它返回了这个。有什么问题?

enter image description here

1 个答案:

答案 0 :(得分:3)

当我开始使用Recycler视图时,我也遇到了这个问题。

问题是您将网格设置为垂直滚动网格并将行计数设置为3。

因此,它会调整row_item_grid布局的大小以适应连续的3个项目。所以这里的问题是你在同一个Layout中使用了匹配父级为父级的LinearLayout。但是图像是300dpx300dp,它位于LinearLayout的中心。

因为你的LinearLayout因为额外的填充。 相反,你可以简单地拥有

<强> row_item_grid.xml

<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/iv_photo"
        />

老实说,当您的视图包含单个布局时,您不需要父布局。我没有检查你的itemDecoration代码,对于我的知识,如果你按照这些步骤你甚至不需要垂直间距