recyclerview 行間距

public class BottomanSpaceItemDecoration extends RecyclerView.ItemDecoration {
    private int mSpace;
    private int mPosition;
    private Paint paint;
    private CircleSquareRvAdapter mRvAdapter;

    public BottomanSpaceItemDecoration(CircleSquareRvAdapter adapter, int space, int position) {
        mRvAdapter = adapter;
        this.mSpace = space;
        mPosition = position;
        paint = new Paint();
        paint.setColor(MainApplication.getInstance().getColor(R.color.red));
    }

    /**
     * Retrieve any offsets for the given item. Each field of <code>outRect</code> specifies
     * the number of pixels that the item view should be inset by, similar to padding or margin.
     * The default implementation sets the bounds of outRect to 0 and returns.
     * <p>
     * <p>
     * If this ItemDecoration does not affect the positioning of item views, it should set
     * all four fields of <code>outRect</code> (left, top, right, bottom) to zero
     * before returning.
     * <p>
     * <p>
     * If you need to access Adapter for additional data, you can call
     * {@link RecyclerView#getChildAdapterPosition(View)} to get the adapter position of the
     * View.
     *
     * @param outRect Rect to receive the output.
     * @param view    The child view to decorate
     * @param parent  RecyclerView this ItemDecoration is decorating
     * @param state   The current state of RecyclerView.
     */
    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        /*outRect.left = mSpace;
        outRect.right = mSpace;
        outRect.bottom = mSpace;*/
        int position = parent.getChildAdapterPosition(view);
        if(mPosition == -1){
            mPosition = mRvAdapter.mTabPosition == 0 ? -1 : mRvAdapter.mTabPosition;
        }
        if (position > mPosition && mPosition != -1) {
            outRect.bottom = mSpace;
        }

    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);
        int left = parent.getPaddingLeft();
        int right = parent.getWidth() - parent.getPaddingRight();
        int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View view = parent.getChildAt(i);
            float top = view.getTop() - mSpace;
            float bottom = view.getTop();
            int position = parent.getChildAdapterPosition(view);
            if(mPosition == -1){
                mPosition = mRvAdapter.mTabPosition == 0 ? -1 : mRvAdapter.mTabPosition;
            }
            if (position > mPosition && mPosition != -1) {
                c.drawRect(left, top, right, bottom, paint);//繪製紅色矩形
            }
            /*long groupId = callback.getGroupId(position);
            if (groupId < 0) return;
            String textLine = callback.getGroupFirstLine(position).toUpperCase();
            if (position == 0 || isFirstInGroup(position)) {
                float top = view.getTop() - topGap;
                float bottom = view.getTop();
                c.drawRect(left, top, right, bottom, paint);//繪製紅色矩形
                c.drawText(textLine, left, bottom, textPaint);//繪製文本
            }*/
        }
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章