其他分享
首页 > 其他分享> > android-RecyclerView.itemDecoration onDraw c.drawLine与预棒棒糖设备不兼容?

android-RecyclerView.itemDecoration onDraw c.drawLine与预棒棒糖设备不兼容?

作者:互联网

我正在尝试创建自己的CustomDivider类,该类扩展到RecyclerView. ItemDecoration基于Google带有分隔线的示例.幸运的是,我尝试的代码可以运行,但是我只在运行于Lollipop的仿真器中对其进行了尝试,但发现它在我的Kitkat设备中不起作用.

这是模拟器和结果的屏幕截图.

这是我的运行Kitkat的华硕Zenfone 4的屏幕截图.

这是我的代码:

public class CustomDivider extends RecyclerView.ItemDecoration {

public static final int LINEAR_HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;
public static final int LINEAR_VERTICAL_LIST = LinearLayoutManager.VERTICAL;
public static final int LINE_FLAT = 0;

private Paint paint = new Paint();
private int color;
private int orientation;
private int top, bottom, left, right;
private int lineType; //TODO LineTypes
private View child;

public CustomDivider(Context context, int orientation, int resourceColor) {
    color = context.getResources().getColor(resourceColor);
    paint.setColor(color);
    paint.setStrokeWidth(3);
    checkOrientation(orientation);
    lineType = LINE_FLAT;
}

private void checkOrientation(int orientation) {
    if (orientation != LINEAR_HORIZONTAL_LIST && orientation != LINEAR_VERTICAL_LIST) {
        throw new IllegalArgumentException("Invalid Orientation");
    } else
        this.orientation = orientation;
}

@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
    super.onDraw(c, parent, state);

    if (orientation == LINEAR_HORIZONTAL_LIST) {
        //drawLinearHorizontalDivider(c, parent);
    } else if (orientation == LINEAR_VERTICAL_LIST) {
        drawLinearVerticalDivider(c, parent);
    }

}

private void drawLinearVerticalDivider(Canvas c, RecyclerView parent) {
    left = parent.getLeft();
    right = parent.getWidth();

    for (int i = 0; i < parent.getChildCount(); i++) {
        child = parent.getChildAt(i);
        top = child.getTop();
        bottom = child.getBottom();
        Log.d("Dimensions: ", "Top is: " + top + ", Bottom is " + bottom);
        if (i != parent.getChildCount() - 1) {
            c.drawLine(left, bottom, right, bottom, paint);
        }
    }
}

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

任何人都可以提供有关我是否错过任何东西的更多信息吗?

解决方法:

这是一个很好的示例,请看一下:

public class DividerItemDecoration extends RecyclerView.ItemDecoration {


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

    public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;

    public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;

    private Drawable mDivider;

    private int mOrientation;

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

    public void setOrientation(int orientation) {
        if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) {
            throw new IllegalArgumentException("invalid orientation");
        }
        mOrientation = orientation;
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent) {
        if (mOrientation == VERTICAL_LIST) {
            drawVertical(c, parent);
        } else {
            drawHorizontal(c, parent);
        }
    }

    public void drawVertical(Canvas c, RecyclerView parent) {
        final int left = parent.getPaddingLeft();
        final int right = parent.getWidth() - parent.getPaddingRight();

        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                    .getLayoutParams();
            final int top = child.getBottom() + params.bottomMargin;
            final int bottom = top + mDivider.getIntrinsicHeight();
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
    }

    public void drawHorizontal(Canvas c, RecyclerView parent) {
        final int top = parent.getPaddingTop();
        final int bottom = parent.getHeight() - parent.getPaddingBottom();

        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                    .getLayoutParams();
            final int left = child.getRight() + params.rightMargin;
            final int right = left + mDivider.getIntrinsicHeight();
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
    }

    @Override
    public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {
        if (mOrientation == VERTICAL_LIST) {
            outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
        } else {
            outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
        }
    }
}

这是我的片段,它运作良好,分隔线清晰细腻:

public static class RecyclerViewFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_recyclerview, container, false);

        RecyclerView recyclerView = (RecyclerView) root.findViewById(R.id.recycler_view);
        recyclerView.setHasFixedSize(true);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));

        RecyclerViewAdapter adapter = new RecyclerViewAdapter(getActivity(), getResources()
            .getStringArray(R.array.countries));
        recyclerView.setAdapter(adapter);

        return root;
    }
}

希望您会受到启发.

标签:android-recyclerview,android
来源: https://codeday.me/bug/20191120/2042676.html