在RecyclerView背景上画多条线 [英] Draw multiple lines on a RecyclerView background

查看:65
本文介绍了在RecyclerView背景上画多条线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在RecyclerView背景上绘制多条水平线. 这些行必须位于精确的位置,因为在它们之间必须有一系列元素.我可以只将线条添加到每个元素中,但是即使没有元素添加到列表中,我也需要绘制这些线条.

如何在背景上绘制线条? (我无法通过.xml执行此操作)谢谢您的宝贵时间!

示例图片

解决方案

您似乎想绘制列表分隔符.我认为您想使用 ItemDecoration

在编写装饰器时,您要确保考虑到translationY(处理项目添加/删除动画)和与其他装饰的项目偏移(例如layoutManager.getDecoratedBottom(view))

public class DividerItemDecoration extends RecyclerView.ItemDecoration {
    private static final int[] ATTRS = new int[]{
            android.R.attr.listDivider
    };

    private Drawable mDivider;

    public DividerItemDecoration(Context context) {
        final TypedArray a = context.obtainStyledAttributes(ATTRS);
        mDivider = a.getDrawable(0);
        a.recycle();
    }

    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        int left = parent.getLeft();
        int right = parent.getRight();
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();

        int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View child = parent.getChildAt(i);
            int ty = (int) (child.getTranslationY() + 0.5f);
            int top = layoutManager.getDecoratedBottom(child) + ty;
            int bottom = top + mDivider.getIntrinsicHeight();
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
    }
}


recyclerView.addItemDecoration(new DividerItemDecoration(context));

I'm trying to draw multiple horizontal lines on a RecyclerView background. These lines have to be at a precise position, because there is a list of elements which have to fit between them. I could just add the lines to each element but I need those lines drawn, even if there are no elements added to the list.

How can I draw lines on the background? (I can't do it from the .xml) Thank you for your time!

Example image

解决方案

It looks like you want to draw list dividers. I think you want to use ItemDecoration

When writing a decorator you want to make sure you account for translationY (handles item add/remove animation) and item offsets from other decorations (e.g. layoutManager.getDecoratedBottom(view))

public class DividerItemDecoration extends RecyclerView.ItemDecoration {
    private static final int[] ATTRS = new int[]{
            android.R.attr.listDivider
    };

    private Drawable mDivider;

    public DividerItemDecoration(Context context) {
        final TypedArray a = context.obtainStyledAttributes(ATTRS);
        mDivider = a.getDrawable(0);
        a.recycle();
    }

    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        int left = parent.getLeft();
        int right = parent.getRight();
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();

        int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View child = parent.getChildAt(i);
            int ty = (int) (child.getTranslationY() + 0.5f);
            int top = layoutManager.getDecoratedBottom(child) + ty;
            int bottom = top + mDivider.getIntrinsicHeight();
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
    }
}


recyclerView.addItemDecoration(new DividerItemDecoration(context));

这篇关于在RecyclerView背景上画多条线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆