RecyclerView SimpleItemDivider之间不添加分隔符

时间:2017-07-26 14:55:17

标签: android android-recyclerview divider

我正在尝试在回收站视图中的每个列表项之间添加一些空格,但由于某些原因它无法正常工作。我一直在关注实施此解决方案的指南,但似乎无法找到答案。它添加了空格,但空间放在下一个列表项上。

这就是我的Recyclerview目前的样子: - My RecyclerView

以下是我的课程: - divider.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <size
        android:width="40dp"
        android:height="40dp" />

    <solid 
        android:color="@color/white"/>

</shape>

SimpleDividerItemDecoration类

public class SimpleDividerItemDecoration extends RecyclerView.ItemDecoration {

    private Drawable mDivider;

    public SimpleDividerItemDecoration(Context context) {
        mDivider = ContextCompat.getDrawable(context,R.drawable.divider);
    }

    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        int left = parent.getPaddingLeft();
        int right = parent.getWidth() - parent.getPaddingRight();

        int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View child = parent.getChildAt(i);

            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();

            int top = child.getBottom() + params.bottomMargin;
            int bottom = top + mDivider.getIntrinsicHeight();

            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
    }
}
我的Fragment类中的

onCreate方法,我得到了对象

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_popular_picks, container, false);

        recyclerView = (RecyclerView)v.findViewById(R.id.popular_pick_recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.addItemDecoration(new SimpleDividerItemDecoration(getContext()));

        getPopularPost();

        return v;
    }

3 个答案:

答案 0 :(得分:0)

如果您只需要添加空间,请考虑将其添加到项目布局(列表项的.xml文件)。这可以是RecyclerView中每个项目的一些填充。

如果您需要添加常规分频器,请考虑使用默认DividerItemDecoration。它可从appcompat库25.0.0获得。

您可以这样添加:

mDividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(),
         mLayoutManager.getOrientation());
recyclerView.addItemDecoration(mDividerItemDecoration);

答案 1 :(得分:0)

添加以下行代码,希望它能解决您的问题

recyclerView.addItemDecoration(new GridSpacingItemDecoration(2,dpToPx(10),true));

这里是GridSpacingItemDecoration类

/ **      * RecyclerView项目装饰 - 在网格项目周围给出相等的边距      * /

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

/**
 * Converting dp to pixel
 */
private int dpToPx(int dp) {
    Resources r = getResources();
    return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics()));
}

答案 2 :(得分:-1)

为什么不只在具有您创建背景的recyclerview项目底部添加自定义视图?