zoukankan      html  css  js  c++  java
  • Grid RecyclerView.ItemDecoration


    /**
    * @author zhy
    */
    public class DividerGridItemDecoration extends RecyclerView.ItemDecoration {

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

    public DividerGridItemDecoration(Context context) {
    final TypedArray a = context.obtainStyledAttributes(ATTRS);

    mDivider = ContextCompat.getDrawable(context, R.drawable.divider_present);
    //mDivider = a.getDrawable(0);
    a.recycle();
    }

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

    drawHorizontal(c, parent);
    drawVertical(c, parent);

    }

    private int getSpanCount(RecyclerView parent) {
    // 列数
    int spanCount = -1;
    LayoutManager layoutManager = parent.getLayoutManager();
    if (layoutManager instanceof GridLayoutManager) {

    spanCount = ((GridLayoutManager) layoutManager).getSpanCount();
    } else if (layoutManager instanceof StaggeredGridLayoutManager) {
    spanCount = ((StaggeredGridLayoutManager) layoutManager)
    .getSpanCount();
    }
    return spanCount;
    }

    public void drawHorizontal(Canvas c, RecyclerView parent) {
    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.getLeft() - params.leftMargin;
    final int right = child.getRight() + params.rightMargin
    + mDivider.getIntrinsicWidth();
    final int top = child.getBottom() + params.bottomMargin;
    final int bottom = top + mDivider.getIntrinsicHeight();
    mDivider.setBounds(left, top, right, bottom);
    mDivider.draw(c);
    }
    }

    public void drawVertical(Canvas c, RecyclerView parent) {
    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.getTop() - params.topMargin;
    final int bottom = child.getBottom() + params.bottomMargin;
    final int left = child.getRight() + params.rightMargin;
    final int right = left + mDivider.getIntrinsicWidth();

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

    private boolean isLastColum(RecyclerView parent, int pos, int spanCount,
    int childCount) {
    LayoutManager layoutManager = parent.getLayoutManager();
    if (layoutManager instanceof GridLayoutManager) {
    if ((pos + 1) % spanCount == 0)// 如果是最后一列,则不需要绘制右边
    {
    return true;
    }
    } else if (layoutManager instanceof StaggeredGridLayoutManager) {
    int orientation = ((StaggeredGridLayoutManager) layoutManager)
    .getOrientation();
    if (orientation == StaggeredGridLayoutManager.VERTICAL) {
    if ((pos + 1) % spanCount == 0)// 如果是最后一列,则不需要绘制右边
    {
    return true;
    }
    } else {
    childCount = childCount - childCount % spanCount;
    if (pos >= childCount)// 如果是最后一列,则不需要绘制右边
    return true;
    }
    }
    return false;
    }

    private boolean isLastRaw(RecyclerView parent, int pos, int spanCount,
    int childCount) {
    LayoutManager layoutManager = parent.getLayoutManager();
    if (layoutManager instanceof GridLayoutManager) {
    // 如果是最后一行,则不需要绘制底部
    if (pos + spanCount >= childCount)
    return true;
    } else if (layoutManager instanceof StaggeredGridLayoutManager) {
    int orientation = ((StaggeredGridLayoutManager) layoutManager)
    .getOrientation();
    // StaggeredGridLayoutManager 且纵向滚动
    if (orientation == StaggeredGridLayoutManager.VERTICAL) {
    // 如果是最后一行,则不需要绘制底部
    if (pos + spanCount >= childCount)
    return true;
    } else
    // StaggeredGridLayoutManager 且横向滚动
    {
    // 如果是最后一行,则不需要绘制底部
    if ((pos + 1) % spanCount == 0) {
    return true;
    }
    }
    }
    return false;
    }

    @Override
    public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {
    int spanCount = getSpanCount(parent);
    int childCount = parent.getAdapter().getItemCount();
    // 如果是最后一行,则不需要绘制底部
    if (isLastRaw(parent, itemPosition, spanCount, childCount)) {
    // 最后一行且为最后一列则不绘制
    if (isLastColum(parent, itemPosition, spanCount, childCount)) {
    outRect.set(0, 0, 0, 0);
    } else {
    outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
    }
    } else if (isLastColum(parent, itemPosition, spanCount, childCount)) {
    // 如果是最后一列,则不需要绘制右边
    outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
    } else {
    outRect.set(0, 0, mDivider.getIntrinsicWidth(),
    mDivider.getIntrinsicHeight());
    }
    }
    }
  • 相关阅读:
    navicat 创建查询失败 can not create file
    使用Themeleaf时, HTML内嵌的JS代码需要注意< 和 >的问题
    window下查杀占用端口的进程
    Spring MVC的Rest URL 被错误解析成jsp, 导致404错误(XML方式下@Controller和@RestController需要配置<mvc:annotation-driving/>)
    一个本地DNS解析和mysql授权导致的Mysq连接失败问题(Access denied for user 'loan'@'kfcsdb1' (using password: YES))
    taglib报错The content of element type "taglib" must match "(tlib-version,...)
    cvc-complex-type.2.4.a: Invalid content was found starting with element 'display-name'
    在eclipse中运行spring web application时的异常: java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
    Spring3升级到Spring4时, 运行时出现找不到MappingJacksonHttpMessageConverter的情况
    如何在Spring MVC Test中避免”Circular view path” 异常
  • 原文地址:https://www.cnblogs.com/a0000/p/5361190.html
Copyright © 2011-2022 走看看