RecyclerView列表的分割線
阿新 • • 發佈:2018-11-16
RecyclerView結構佈局:線性、網格、瀑布流,其中最常用的就是線性和網格佈局。
設定線性列表Item分割線一般比較容易,可以通過兩種方案來做:
- 直接佈局item的時候,把要作為分割線的線條或者其他形式的,直接新增在item佈局中。並且可以動態控制分割線的顯示與否。
- 通過RecyclerView.ItemDecoration來設定,無非就是設定左、右、上、下間隔
public class RecyclerLinearSpaceItemDecoration extends RecyclerView.ItemDecoration { int mSpace; int type; //線性橫向:左間距 private final int HORIZONTAL_LEFT = 1; //線性橫向:右間距 private final int HORIZONTAL_RIGHT = 2; //線性縱向:top間距 private final int VERTICAL_TOP = 3; //線性縱向:bottom間距 private final int VERTICAL_BOTTOM = 4; /** * 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); // int position = parent.getChildAdapterPosition(view); switch (type) { case HORIZONTAL_LEFT: outRect.left = mSpace; break; case HORIZONTAL_RIGHT: outRect.right = mSpace; break; case VERTICAL_TOP: outRect.top = mSpace; break; case VERTICAL_BOTTOM: outRect.bottom = mSpace; break; } } public RecyclerLinearSpaceItemDecoration(int space, int type) { this.mSpace = space; this.type = type; } }
設定網格佈局Item的分割線,目前只實踐過可以通過RecyclerView.ItemDecoration來設定,無非就是設定左、右、上、下間隔
public class RecyclerGridSpacingItemDecoration extends RecyclerView.ItemDecoration { private int spanCount; private int spacing; private boolean includeEdge; private Context context; private int styleType; //相同的間隔:橫向、縱向、以及頭部和底部 private final int STYLE_TYPE_HEAD_FOOT_HORIZONTAL_VERTIAL = 1; //相同的間隔:只有橫向、縱向, 沒有頭部和底部 private final int STYLE_TYPE_HORIZONTAL_VERTIAL = 2; public RecyclerGridSpacingItemDecoration(Context context, int spanCount, int spacing, int styleType ,boolean includeEdge) { this.spanCount = spanCount; this.spacing = spacing; this.includeEdge = includeEdge; this.context = context; this.styleType = styleType; } @Override public void getItemOffsets(Rect outRect, View view, RecyclerView parent, 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) switch (styleType) { case STYLE_TYPE_HEAD_FOOT_HORIZONTAL_VERTIAL: //底部有間隔的話,就用top outRect.bottom = spacing; if (position < spanCount) { outRect.top = spacing; } break; case STYLE_TYPE_HORIZONTAL_VERTIAL: if (position >= spanCount) { //底部沒有間隔的話,就用top outRect.top = spacing; } break; } } } }