Android RecyclerView 設定item之間的間距
阿新 • • 發佈:2018-11-21
轉載:https://www.jianshu.com/p/e372cec819db
RecyclerView沒有可以直接設定間距的屬性,但看了原始碼之後可以發現RecyclerView有個內部類ItemDecoration,可以用ItemDecoration來裝飾一個item,所以繼承重寫ItemDecoration就可以實現間距了。我看了以下,網上很多類似的介紹,但是大多都只考慮到LinearLayoutManager這種
這是LinearLayoutManager設定Item間距的的一個輔助類
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.getChildPosition(view) == 0) outRect.top = space; } }
設定item間距
int space = 8;
mRecyclerView.addItemDecoration(new SpacesItemDecoration(spacingInPixels));
但是我的專案是網格佈局啊,GridLayoutManager,上面這種辦法也不行啊,先看看效果,再講講我的辦法
RecyclerView設定Item的間距.jpg
我的間距只有10dp,看上去不是特別明顯,但是效果是有的,是吧
這裡是GridLayoutManager或者StaggeredGridLayoutManager 設定Item間距的辦法
/** * GridLayoutManager(網格佈局)設定item的間隔 * * 作者: 周旭 on 2017年7月20日 0020. * 郵箱:
[email protected] * 部落格:http://www.jianshu.com/u/56db5d78044d */ 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, 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) if (position >= spanCount) { outRect.top = spacing; // item top } } } }
呼叫的地方
int spanCount = 3; // 3 columns
int spacing = 50; // 50px
boolean includeEdge = false;
mRecyclerView.addItemDecoration(new GridSpacingItemDecoration(spanCount, spacing, includeEdge));
如果你了別人的RecycleView 上拉載入下拉重新整理,addItemDecoration 這個方法 不一定會給你加上,你可以在原始碼上自行新增。
public void addItemDecoration(RecyclerView.ItemDecoration decor) {
mRecyclerView.addItemDecoration(decor,-1);
}
其實還有一種比較巧妙的辦法,就是在item的佈局裡面搞事情
舉個例子.jpg
圖畫的有點醜,用程式碼來說話
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="185dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/transparent"
android:padding="10dp">
<RelativeLayout
android:layout_width="165dp"
android:layout_height="280dp"
android:background="@color/white"
android:orientation="vertical">
</RelativeLayout>
</LinearLayout>
裡面這個RelativeLayout就是你的item正常的佈局,而LinearLayout 這個根部局我設定了他的背景色為透明的,再加一個padding就行了,這個padding就是設定item的間距,這樣設定item的間距了。
所以說RecyclerView的Item的間距都可以通過item裡面設定padding,margin來解決,這種辦法相對巧妙。