解决RecycleView分割线不居中的三种方法

解决RecycleView分割线不居中的三种方法,第1张

概述本文为大家分享了三种RecycleView分割线不居中的解决方法,供大家参考,具体内容和如下

本文为大家分享了三种RecycleVIEw分割线不居中的解决方法,供大家参考,具体内容和如下

方法一:

public class SpacesItemdecoration extends RecyclerVIEw.Itemdecoration {  private int mSpace;  private int mSpanCount; // RecyclerVIEw有多少列  private boolean mHaspadding; // RecyclerVIEw是否有padding  public SpacesItemdecoration(int mSpace) {    this.mSpace = mSpace;    this.mHaspadding = true;  }  public SpacesItemdecoration(int mSpace,boolean haspadding) {    this.mSpace = mSpace;    this.mHaspadding = haspadding;  }  @OverrIDe  public voID getItemOffsets(Rect outRect,VIEw vIEw,RecyclerVIEw parent,RecyclerVIEw.State state) {    // 初始化列数    if (mSpanCount == 0) {      this.mSpanCount = ((GrIDLayoutManager) parent.getLayoutManager()).getSpanCount();    }    int position = parent.getChildAdapterposition(vIEw); // item position    int column = position % mSpanCount; // item column    if (mHaspadding) {      outRect.left = mSpace - column * mSpace / mSpanCount; // spacing - column * ((1f / spanCount) * spacing)      outRect.right = (column + 1) * mSpace / mSpanCount; // (column + 1) * ((1f / spanCount) * spacing)      if (position < mSpanCount) { // top edge        outRect.top = mSpace;      }      outRect.bottom = mSpace; // item bottom    } else {      outRect.left = column * mSpace / mSpanCount; // column * ((1f / spanCount) * spacing)      outRect.right = mSpace - (column + 1) * mSpace / mSpanCount; // spacing - (column + 1) * ((1f /  spanCount) * spacing)      if (position >= mSpanCount) {        outRect.top = mSpace; // item top      }    }  }  public voID setHaspadding(boolean haspadding) {    this.mHaspadding = haspadding;  }}

方法二:

public class MutiItemdecoration extends RecyclerVIEw.Itemdecoration {  public enum Type {    VERTICAL,HORIZONTAL,ALL  }  private Type type;//分割线类型  private int divIDerSize = 10;//分割线尺寸  public MutiItemdecoration(MutiItemdecoration.Type type,int divIDerSize) {    this.type = type;    this.divIDerSize = divIDerSize;  }  @OverrIDe  public voID getItemOffsets(Rect outRect,int itemposition,RecyclerVIEw parent) {    int spanCount = getSpanCount(parent);    int childCount = parent.getAdapter().getItemCount();    switch (type) {      case ALL:        if (itemposition % spanCount == 0) {//第一列          if (isLastRaw(parent,itemposition,spanCount,childCount)) {            outRect.set(0,divIDerSize / 2,0);          } else {            outRect.set(0,divIDerSize);          }        } else if (itemposition % spanCount == spanCount - 1) {//最后一列          if (isLastRaw(parent,childCount)) {            outRect.set(divIDerSize / 2,0);          } else {            outRect.set(divIDerSize / 2,divIDerSize);          }        } else {//中间列          if (isLastRaw(parent,divIDerSize);          }        }        break;      case VERTICAL:        if (isLastRaw(parent,childCount)) {          outRect.set(0,0);        } else {          outRect.set(0,divIDerSize);        }        break;      case HORIZONTAL:        if (isLastColum(parent,divIDerSize,0);        }        break;    }  }  // 是否是最后一列  private boolean isLastColum(RecyclerVIEw parent,int pos,int spanCount,int childCount) {    RecyclerVIEw.LayoutManager layoutManager = parent.getLayoutManager();    if (layoutManager instanceof GrIDLayoutManager) {      if ((pos + 1) % spanCount == 0)        return true;    } else {      if (pos == childCount - 1)        return true;    }    return false;  }  // 是否是最后一行  private boolean isLastRaw(RecyclerVIEw parent,int childCount) {    RecyclerVIEw.LayoutManager layoutManager = parent.getLayoutManager();    if (layoutManager instanceof GrIDLayoutManager) {      childCount = childCount - childCount % spanCount;      if (pos >= childCount)        return true;    } else {      if (pos == childCount - 1)        return true;    }    return false;  }  //返回列数  private int getSpanCount(RecyclerVIEw parent) {    RecyclerVIEw.LayoutManager layoutManager = parent.getLayoutManager();    if (layoutManager instanceof GrIDLayoutManager) {      return ((GrIDLayoutManager) layoutManager).getSpanCount();    }    return -1;  }}

方法三:(目前只支持2列)

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.shape_divIDer);    a.recycle();  }  @OverrIDe  public voID onDraw(Canvas c,RecyclerVIEw.State state) {    drawHorizontal(c,parent);    drawVertical(c,parent);  }  private int getSpanCount(RecyclerVIEw parent) {    // 列数    int spanCount = -1;    RecyclerVIEw.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();      if (i % 2 == 1) {        final int top = child.gettop() - params.topmargin;        final int bottom = child.getBottom() + params.bottommargin;        final int left = child.getleft() - params.rightmargin;        final int right = left + mdivIDer.getIntrinsicWIDth() / 2;        mdivIDer.setBounds(left,bottom);        mdivIDer.draw(c);      } else {        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() / 2;        mdivIDer.setBounds(left,bottom);        mdivIDer.draw(c);      }      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,bottom);      mdivIDer.draw(c);    }  }  private boolean isLastColum(RecyclerVIEw parent,int childCount) {    RecyclerVIEw.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 childCount) {    RecyclerVIEw.LayoutManager layoutManager = parent.getLayoutManager();    if (layoutManager instanceof GrIDLayoutManager) {      childCount = childCount - childCount % spanCount;      if (pos >= childCount)// 如果是最后一行,则不需要绘制底部        return true;    } else if (layoutManager instanceof StaggeredGrIDLayoutManager) {      int orIEntation = ((StaggeredGrIDLayoutManager) layoutManager)          .getorIEntation();      // StaggeredGrIDLayoutManager 且纵向滚动      if (orIEntation == StaggeredGrIDLayoutManager.VERTICAL) {        childCount = childCount - childCount % spanCount;        // 如果是最后一行,则不需要绘制底部        if (pos >= childCount)          return true;      } else      // StaggeredGrIDLayoutManager 且横向滚动      {        // 如果是最后一行,则不需要绘制底部        if ((pos + 1) % spanCount == 0) {          return true;        }      }    }    return false;  }  @OverrIDe  public voID getItemOffsets(Rect outRect,RecyclerVIEw parent) {    int spanCount = getSpanCount(parent);    int childCount = parent.getAdapter().getItemCount();    if (isLastRaw(parent,childCount))// 如果是最后一行,则不需要绘制底部    {      outRect.set(0,mdivIDer.getIntrinsicWIDth(),0);    } else if (isLastColum(parent,childCount))// 如果是最后一列,则不需要绘制右边    {      outRect.set(0,mdivIDer.getIntrinsicHeight());    } else {      outRect.set(0,mdivIDer.getIntrinsicHeight());    }  }}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的解决RecycleView分割线不居中的三种方法全部内容,希望文章能够帮你解决解决RecycleView分割线不居中的三种方法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1149207.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存