RecyclerView ItemDecoration不能正确绘制垂直分隔线

时间:2016-10-23 16:24:05

标签: android android-recyclerview android-canvas android-drawable

我在recyclerview中无法实现旋转版水平分割器。当我使用LinearLayoutManager.HORIZONTAL时,我需要一个项目之间的垂直线。我试图轮换或使用不同的drawable.xml,但没有成功。

您可以在下面找到代码

public class Divider extends RecyclerView.ItemDecoration {
private Drawable mDivider;
private int orientation;

public Divider(Context context,int orientation) {
    this.orientation = orientation;
    mDivider = ContextCompat.getDrawable(context,R.drawable.line_divider);
}

@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
    if(orientation == LinearLayoutManager.VERTICAL) {
        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);
        }
    }else{
        int top = parent.getPaddingTop();
        int bottom = parent.getHeight() - parent.getPaddingBottom();

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

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

            int left = child.getRight() + params.rightMargin;
            int right = left + mDivider.getIntrinsicWidth();

            mDivider.setBounds(left, top, right, bottom);
            c.rotate(90, mDivider.getIntrinsicWidth() / 2, mDivider.getIntrinsicHeight() / 2);
            mDivider.draw(c);
        }

    }
}

}

我的抽奖

 <inset xmlns:android="http://schemas.android.com/apk/res/android"
android:insetTop="2dip"
android:insetBottom="2dip"
android:visible="true" >

<shape
    android:shape="rectangle">

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

        <solid android:color="#cfcfcf" />

 </shape>

我该怎么办?

0 个答案:

没有答案