ItemDecoration-如何创建垂直分隔线?

时间:2019-06-23 15:15:45

标签: android android-recyclerview recycler-adapter item-decoration

我正在尝试为recyclerView中的每个ViewHolder创建多个垂直分隔线,但不知道如何操作。

这里有我的工作水平分隔线的代码:

@Override
public void onDrawOver(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {


    int left = parent.getPaddingLeft();
    int right = parent.getWidth() - parent.getPaddingRight();

    int count = parent.getChildCount();
    for(int i = 0; i < count; 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);
    }
}

可绘制分隔线:

<?xml version="1.0" encoding="utf-8"?>

<size
    android:width="10dp"
    android:height="10dp" />
<solid android:color="@color/standardBlue" />

1 个答案:

答案 0 :(得分:0)

DividerItemDecoration是一个RecyclerView.ItemDecoration,可以用作LinearLayoutManager的各项之间的分隔符。它支持水平方向和垂直方向。

用法:

DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(),
    layoutManager.getOrientation());
recyclerView.addItemDecoration(dividerItemDecoration);

上一个答案

在项目视图之前(在onDraw()中)和项目之后(在onDrawOver(Canvas,RecyclerView,RecyclerView.State)中),将按添加顺序绘制所有ItemDecorations。

垂直间距ItemDecoration 扩展ItemDecoration,添加自定义构造函数,该构造函数将空间高度作为  参数并覆盖getItemOffsets()方法:

公共类VerticalSpaceItemDecoration扩展了RecyclerView.ItemDecoration {

    private final int verticalSpaceHeight;

    public VerticalSpaceItemDecoration(int verticalSpaceHeight) {
        this.verticalSpaceHeight = verticalSpaceHeight;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
            RecyclerView.State state) {
        outRect.bottom = verticalSpaceHeight;
    }
}
    If you don't want to insert space below the last item, add the following condition:

如果(parent.getChildAdapterPosition(view)!= parent.getAdapter()。getItemCount()-1){                 outRect.bottom = verticalSpaceHeight;     }         注意:您也可以修改outRect.top,outRect.left和outRect.right属性以获得所需的效果。

除法器项装饰     扩展ItemDecoration并覆盖onDraw()方法:

public class DividerItemDecoration extends RecyclerView.ItemDecoration {

    private static final int[] ATTRS = new int[]{android.R.attr.listDivider};

    private Drawable divider;

    /**
     * Default divider will be used
     */
    public DividerItemDecoration(Context context) {
        final TypedArray styledAttributes = context.obtainStyledAttributes(ATTRS);
        divider = styledAttributes.getDrawable(0);
        styledAttributes.recycle();
    }

    /**
     * Custom divider will be used
     */
    public DividerItemDecoration(Context context, int resId) {
        divider = ContextCompat.getDrawable(context, resId);
    }

    @Override
    public void onDraw(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 + divider.getIntrinsicHeight();

            divider.setBounds(left, top, right, bottom);
            divider.draw(c);
        }
    }
}

您可以调用第一个使用默认Android除法器属性的构造函数,也可以调用第二个使用您自己的drawable的构造函数,例如drawable / divider.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">
    <size android:height="1dp" />
    <solid android:color="#ff992900" />
</shape>

注意:如果要在项目上绘制分隔线,请改用onDrawOver()方法。

用法 要使用新类,请在RecyclerView中添加VerticalSpaceItemDecoration或DividerSpaceItemDecoration,例如在片段的onCreateView()方法中:

private static final int VERTICAL_ITEM_SPACE = 48;
private RecyclerView recyclerView;
private LinearLayoutManager linearLayoutManager;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_feed, container, false);

    recyclerView = (RecyclerView) rootView.findViewById(R.id.fragment_home_recycler_view);
    linearLayoutManager = new LinearLayoutManager(getActivity());
    recyclerView.setLayoutManager(linearLayoutManager);

    //add ItemDecoration
    recyclerView.addItemDecoration(new VerticalSpaceItemDecoration(VERTICAL_ITEM_SPACE));
    //or
    recyclerView.addItemDecoration(new DividerItemDecoration(getActivity()));
    //or
    recyclerView.addItemDecoration(
            new DividerItemDecoration(getActivity(), R.drawable.divider));

    recyclerView.setAdapter(...);

    return rootView;
}