浅谈Android RecyclerView 间距全适配

浅谈Android RecyclerView 间距全适配,第1张

概述本文介绍了AndroidRecyclerView间距全适配,分享给大家,具体如下:LinearLayoutManagerspacing

本文介绍了AndroID RecyclerVIEw 间距全适配,分享给大家,具体如下:

linearlayoutmanager spacing

import androID.graphics.Rect;import androID.support.v7.Widget.RecyclerVIEw;import androID.vIEw.VIEw;/** * Created by anonymous on 2016/9/9 0009. * <p> * SpacesItemdecoration * <p> * mRecyclerVIEw.addItemdecoration(new SpacesItemdecoration(5)); */public class SpacesItemdecoration extends RecyclerVIEw.Itemdecoration {  private int space;  public SpacesItemdecoration(int space) {    this.space = space;  }  @OverrIDe  public voID getItemOffsets(Rect outRect,VIEw vIEw,RecyclerVIEw parent,RecyclerVIEw.State state) {    outRect.left = space;    outRect.right = space;    outRect.bottom = space;    // Add top margin only for the first item to avoID double space between items    if (parent.getChildLayoutposition(vIEw) == 0) {      outRect.top = space;    } else {      outRect.top = 0;    }  }}

使用方法很简单,上面注释中也已经给出:

mRecyclerVIEw.addItemdecoration(new SpacesItemdecoration(5));

GrIDLayoutManager or StaggeredGrIDLayoutManager spacing

import androID.graphics.Rect;import androID.support.v7.Widget.RecyclerVIEw;import androID.vIEw.VIEw;/** * Created by anonymous on 2016/9/9 0009. * <p> * GrIDSpacingItemdecoration * <p> * int spanCount = 3; // 3 columns * int spacing = 50; // 50px * boolean includeEdge = false; * recyclerVIEw.addItemdecoration(new GrIDSpacingItemdecoration(spanCount,spacing,includeEdge)); */public class GrIDSpacingItemdecoration extends RecyclerVIEw.Itemdecoration {  private int spanCount;  private int spacing;  private boolean includeEdge;  public GrIDSpacingItemdecoration(int spanCount,int spacing,boolean includeEdge) {    this.spanCount = spanCount;    this.spacing = spacing;    this.includeEdge = includeEdge;  }  @OverrIDe  public voID getItemOffsets(Rect outRect,RecyclerVIEw.State state) {    int position = parent.getChildAdapterposition(vIEw); // item position    int column = position % spanCount; // item column    if (includeEdge) {      outRect.left = spacing - column * spacing / spanCount; // spacing - column * ((1f / spanCount) * spacing)      outRect.right = (column + 1) * spacing / spanCount; // (column + 1) * ((1f / spanCount) * spacing)      if (position < spanCount) { // top edge        outRect.top = spacing;      }      outRect.bottom = spacing; // item bottom    } else {      outRect.left = column * spacing / spanCount; // column * ((1f / spanCount) * spacing)      outRect.right = spacing - (column + 1) * spacing / spanCount; // spacing - (column + 1) * ((1f /  spanCount) * spacing)      if (position >= spanCount) {        outRect.top = spacing; // item top      }    }  }}

此处不仅对间距进行了处理,而且对spanCount也进行了处理,使用方法:

    int spanCount = 3; // 3 columns    int spacing = 50; // 50px    boolean includeEdge = false;    mRecyclerVIEw.addItemdecoration(new GrIDSpacingItemdecoration(spanCount,includeEdge));

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

总结

以上是内存溢出为你收集整理的浅谈Android RecyclerView 间距全适配全部内容,希望文章能够帮你解决浅谈Android RecyclerView 间距全适配所遇到的程序开发问题。

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

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

原文地址: http://outofmemory.cn/web/1143455.html

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

发表评论

登录后才能评论

评论列表(0条)

保存