RecyclerView間距設定
適用於GridLayoutManager橫豎間隔均可設定;
用法:
rv.addItemDecoration(new DividerGridItemDecoration(Color.TRANSPARENT, 30, 30, Color.TRANSPARENT, 0));
DividerGridItemDecoration類:
public class DividerGridItemDecoration extends RecyclerView.ItemDecoration { private Drawable mDivider; private int mDividerHSpacing;private int mDividerVSpacing; private Drawable mLastDivider; private int mLastDividerSize; public DividerGridItemDecoration() { set(new ColorDrawable(Color.TRANSPARENT), mDividerHSpacing, mDividerVSpacing, new ColorDrawable(Color.TRANSPARENT), mLastDividerSize); } publicDividerGridItemDecoration(int horizaontalSpacing, int verticalSpacing, int lastDividerSize) { set(new ColorDrawable(Color.TRANSPARENT), horizaontalSpacing, verticalSpacing, new ColorDrawable(Color.TRANSPARENT), lastDividerSize); } public DividerGridItemDecoration(Drawable divider, int horizaontalSpacing, intverticalSpacing, Drawable lastDivider, int lastDividerSize) { set(divider, horizaontalSpacing, verticalSpacing, lastDivider, lastDividerSize); } public DividerGridItemDecoration(@ColorInt int dividerColor, int horizaontalSpacing, int verticalSpacing, @ColorInt int lastDividerColor, int lastDividerSize) { set(new ColorDrawable(dividerColor), horizaontalSpacing, verticalSpacing, new ColorDrawable(lastDividerColor), lastDividerSize); } /** * @param divider * @param horizaontalSpacing 橫向間距 * @param verticalSpacing 縱向間距 * @param lastDivider * @param lastDividerSize 最後一行的後邊距離,大於0才會生效 */ private void set(Drawable divider, int horizaontalSpacing, int verticalSpacing, Drawable lastDivider, int lastDividerSize) { this.mDivider = divider; this.mDividerHSpacing = horizaontalSpacing; this.mDividerVSpacing = verticalSpacing; this.mLastDivider = lastDivider; this.mLastDividerSize = lastDividerSize; } @Override public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) { super.onDraw(c, parent, state); if (mDivider == null) { return; } if (mDividerHSpacing > 0) { drawHorizontalSpacing(c, parent); } if (mDividerVSpacing > 0) { drawVerticalSpacing(c, parent); } if (mLastDividerSize > 0) { drawLastSpacing(c, parent); } } /** * 繪製橫向間距,不包括最後一列 * * @param c * @param parent */ private void drawHorizontalSpacing(Canvas c, RecyclerView parent) { int childCount = parent.getChildCount(); for (int i = 0; i < childCount; i++) { final View child = parent.getChildAt(i); // 前邊不用考慮是否是最後一列 if (i < childCount - ((GridLayoutManager) parent.getLayoutManager()).getSpanCount() || (parent.getLayoutManager().canScrollVertically() || !isLastGridRaw(parent, child))) { drawHorizontal(c, child, mDivider, mDividerHSpacing); } } } private void drawHorizontal(Canvas c, View child, Drawable divider, int spacing) { final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams(); final int left = child.getRight() + params.rightMargin; final int right = left + spacing; final int top = child.getTop() - params.topMargin; final int bottom = child.getBottom() + params.bottomMargin; divider.setBounds(left, top, right, bottom); divider.draw(c); } /** * 繪製縱向間距,不包括最後一行 * * @param c * @param parent */ private void drawVerticalSpacing(Canvas c, RecyclerView parent) { int childCount = parent.getChildCount(); for (int i = 0; i < childCount; i++) { final View child = parent.getChildAt(i); // 前邊不用考慮是否是最後一行 if (i < childCount - ((GridLayoutManager) parent.getLayoutManager()).getSpanCount() || (parent.getLayoutManager().canScrollHorizontally() || !isLastGridRaw(parent, child))) { drawVertical(c, child, mDivider, mDividerVSpacing); } } } private void drawVertical(Canvas c, View child, Drawable divider, int spacing) { final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams(); final int left = child.getLeft() - params.leftMargin; final int right = child.getRight() + params.rightMargin; final int top = child.getBottom() + params.bottomMargin; final int bottom = top + spacing; divider.setBounds(left, top, right, bottom); divider.draw(c); } /** * 繪製最後 * * @param c * @param parent */ private void drawLastSpacing(Canvas c, RecyclerView parent) { int childCount = parent.getChildCount(); int i = childCount - ((GridLayoutManager) parent.getLayoutManager()).getSpanCount(); for (; i < childCount; i++) { final View child = parent.getChildAt(i); if (isLastGridRaw(parent, child)) { if (parent.getLayoutManager().canScrollHorizontally()) { drawHorizontal(c, child, mLastDivider, mLastDividerSize); } else if (parent.getLayoutManager().canScrollVertically()) { drawVertical(c, child, mLastDivider, mLastDividerSize); } } } } @Override public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { super.getItemOffsets(outRect, view, parent, state); GridLayoutManager gridLayoutManager = (GridLayoutManager) parent.getLayoutManager(); if (gridLayoutManager.canScrollHorizontally()) { if (isLastGridRaw(parent, view)) {// 如果是最後一列 if (parent.getChildAdapterPosition(view) == gridLayoutManager.getItemCount() - 1) {// 最後一個 outRect.set(0, 0, mLastDividerSize, 0); } else { outRect.set(0, 0, mLastDividerSize, mDividerVSpacing); } } /* * else if (RecyclerViewUtil.isLastGridColum(parent, view)) {// * 如果是最後一行,則不需要繪製底部 outRect.set(0, 0, mDividerHSpacing, 0); } */ else { outRect.set(0, 0, mDividerHSpacing, mDividerVSpacing); } } else if (gridLayoutManager.canScrollVertically()) { if (isLastGridRaw(parent, view)) {// 如果是最後一行,則不需要繪製底部 if (parent.getChildAdapterPosition(view) == gridLayoutManager.getItemCount() - 1) { outRect.set(0, 0, 0, mLastDividerSize); } else { outRect.set(0, 0, mDividerHSpacing, mLastDividerSize); } } /* * else if (RecyclerViewUtil.isLastGridColum(parent, view)) {// * 如果是最後一列,則不需要繪製右邊 outRect.set(0, 0, 0, mDividerVSpacing); } */ else { outRect.set(0, 0, mDividerHSpacing, mDividerVSpacing); } } } /** * 是否是最後一行,相對於recyclerView的方向而言,橫向表示最右邊,豎向表示最下邊 * * @param parent * @param view * @return */ public boolean isLastGridRaw(RecyclerView parent, View view) { RecyclerView.LayoutManager layoutManager = parent.getLayoutManager(); if (layoutManager instanceof GridLayoutManager) { GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager; GridLayoutManager.SpanSizeLookup spanSizeLookUp = gridLayoutManager.getSpanSizeLookup(); int childPosition = parent.getChildAdapterPosition(view); int itemCount = gridLayoutManager.getItemCount(); int spanCountTotal = 0; int spanCountChild = 0; if (childPosition >= itemCount - gridLayoutManager.getSpanCount()) { for (int i = 0; i < itemCount; i++) { spanCountTotal += spanSizeLookUp.getSpanSize(i); if (i <= childPosition) { spanCountChild += spanSizeLookUp.getSpanSize(i); } } int lastRowCount = spanCountTotal % gridLayoutManager.getSpanCount(); if (lastRowCount == 0) { lastRowCount = gridLayoutManager.getSpanCount(); } if (spanCountChild > spanCountTotal - lastRowCount) { return true; } } } else if (layoutManager instanceof StaggeredGridLayoutManager) { // TODO: 17/1/16 } return false; } }
相關推薦
RecyclerView間距設定
適用於GridLayoutManager橫豎間隔均可設定;用法:rv.addItemDecoration(new DividerGridItemDecoration(Color.TRANSPARENT, 30, 30, Color.TRANSPARENT, 0));Divid
RecyclerView使用GridLayoutManager間距設定
使用RecyclerView設定間距,需要重寫RecyclerView.ItemDecoration這個類。有如下的效果圖需要實現,間距只有中間的格子和底部的格式之間有。 實現方法很簡單,因為這個
Android RecyclerView、設定佈局管理器、設定Item增加、移除動畫、新增分割線
概述 RecyclerView出現已經有一段時間了,相信大家肯定不陌生了,大家可以通過匯入support-v7對其進行使用。 據官方的介紹,該控制元件用於在有限的視窗中展示大量資料集,其實這樣功能的控制元件我們並不陌生,例如:ListView、GridView。 那麼有了ListView、G
Android Recyclerview間距 均分 完美佈局 支援兩端間距
最近專案中要在RecyclerView的Grid中均分間距,看了原始碼和一些文章,下面的這篇很有啟發,在此引用一下,並且在他的基礎上支援任意列數和兩端間距。最後附上程式碼: 簡介: App中,用到最多的設計就是列表形式的佈局,而RecyclerView的出現,也完完全
RecyclerView--GridLayoutManager設定某個item佔據的列數
/** * 設定瀑布流佈局中的某個item,獨佔一行、佔一列、佔兩列、等等 * @param mStaggeredGridLayoutManager * @param po
解決RecyclerView GridLayoutManager 設定分割線出現item寬度不等的問題
一、概述 最近研究RecyclerView給GridLayoutManager模式設定分割線的問題,在網上找了一些案例,卻都有著item寬度大小不等的問題,以鴻洋大神的這篇http://blog.csdn.net/lmj623565791/article
一句話解決RecyclerView 高度設定wrap_content 不顯示 無作用的bug
最近用RecyclerView,高度設定為wrap_content,結果整個模組什麼也不顯示,在網上找了好久,終於找到解決方案 這是RecyclerView相容包的bug,23.2.0後官方已經修
一句話解決RecyclerView 高度設定wrap_content 無作用的bug
這是RecyclerView相容包的bug,23.2.0後官方已經修復了。 所以直接在gradle裡設定用23.2.0及以上的RecyclerView: compile 'com.an
Android中GridView區中且行間距設定
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_he
RecyclerView佈局設定聊天介面
先上兩張效果圖: ——————————————————分割線————————————————————- 實體類 class Msg { static final int TYPE_TIME = 0; static
Recyclerview滑動設定標題欄漸變效果
前言 最近有這麼個需求,通過Recyclerview滑動監聽來設定標題欄漸變,整個介面是一個RecyclerView,一開始是沒有標題欄的,向上滑動到一定程度標題欄漸變。需求是不難,但是我想記錄一下這個基本的過程,有人需要了可以快速拿走,如果幫到你了,點個贊留個言都行,
ToolBar間距設定
1、沒有NavigationIcon 此時,contentInsetStart 會發揮作用,什麼作用。就是你的自定義View左邊總會多出一塊空白。 為什麼?看原始碼。 int contentInsetStart = a.getDimensionPixelOffs
RecyclerView常見問題解決方案,RecyclerView巢狀自動滾動,RecyclerView 高度設定wrap_content 無作用等問題
1,ScrollView或者RecyclerView1 巢狀RecyclerView2 進入頁面自動跳轉到recyclerView2上面頁面會自動滾動貌似是RecyclerView 自動獲得了焦點兩
【Qt開發】佈局控制元件之間的間距設定
void QLayout::setContentsMargins ( int left, int top, int right, int bottom ) Sets the left, top, right, and bottom margins to use ar
div css文字字型行高行距 深入理解css行間距設定
div css文字字型行高行距 DIVCSS5帶您深入理解css行間距設定,字型行距如何最簡單地設定。 在文章中,要實現調節設定每行文字字型間距(行距)是使用line-height屬性。 要使得每行的文字一定的間距距離所以可以通過line-height樣式實
android RecyclerView 頻繁設定資料閃退問題
最近專案中一個播放的列表,有上下切換功能。測試頻繁切換更換重新整理資料時,報出以下錯誤:java.lang.IllegalArgumentExceptionScrapped or attached views may not be recycled. isScrap:false isAt
RecyclerView GridLayoutManager 設定分割線 萬能分割
萬能的分割線,如有另需請自行修改 上效果圖 下面是原始碼 package com.qianfandu.adapter; import android.graphics.Canvas; import android.graphics.Paint; import a
解決RecyclerView 高度設定wrap_content 無作用的bug
這是RecyclerView相容包的bug,23.2.0後官方已經修復了。所以直接在gradle裡設定用23.2.0及以上的RecyclerView: compile 'com.android.support:recyclerview-v7:23.2.0'12PS:如
RecyclerView 間距
RecyclerViews support the concept of ItemDecoration: special offsets and drawing around each element. As seen in this answer, you can
latex 表格行間距設定
用到的package是:\usepackage{setspace} 命令: \begin{spacing}{1.19} \end{spaceing} 示例: \makeatletter\def\@captype{table}\makeatother \begin{mini