RecyclerView沒(méi)有可以直接設(shè)置間距的屬性深胳,但看了源碼之后可以發(fā)現(xiàn)RecyclerView有個(gè)內(nèi)部類(lèi)ItemDecoration撑蚌,可以用ItemDecoration來(lái)裝飾一個(gè)item厅须,所以繼承重寫(xiě)ItemDecoration就可以實(shí)現(xiàn)間距了丸逸。我看了以下脖律,網(wǎng)上很多類(lèi)似的介紹牺弹,但是大多都只考慮到LinearLayoutManager這種
這是LinearLayoutManager設(shè)置Item間距的的一個(gè)輔助類(lèi)
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;
}
}
設(shè)置item間距
int space = 8;
mRecyclerView.addItemDecoration(new SpacesItemDecoration(spacingInPixels));
但是我的項(xiàng)目是網(wǎng)格布局啊喇澡,GridLayoutManager迅栅,上面這種辦法也不行啊,先看看效果晴玖,再講講我的辦法
我的間距只有10dp读存,看上去不是特別明顯,但是效果是有的呕屎,是吧
這里是GridLayoutManager或者StaggeredGridLayoutManager 設(shè)置Item間距的辦法
/**
* GridLayoutManager(網(wǎng)格布局)設(shè)置item的間隔
*
* 作者: 周旭 on 2017年7月20日 0020.
* 郵箱:374952705@qq.com
* 博客:http://www.reibang.com/u/56db5d78044d
*/
public class GridSpacingItemDecoration extends RecyclerView.ItemDecoration {
private int spanCount; //列數(shù)
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) {
//這里是關(guān)鍵让簿,需要根據(jù)你有幾列來(lái)判斷
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
}
}
}
}
調(diào)用的地方
int spanCount = 3; // 3 columns
int spacing = 50; // 50px
boolean includeEdge = false;
mRecyclerView.addItemDecoration(new GridSpacingItemDecoration(spanCount, spacing, includeEdge));
如果你了別人的RecycleView 上拉加載下拉刷新,addItemDecoration 這個(gè)方法 不一定會(huì)給你加上秀睛,你可以在源碼上自行添加尔当。
public void addItemDecoration(RecyclerView.ItemDecoration decor) {
mRecyclerView.addItemDecoration(decor,-1);
}
其實(shí)還有一種比較巧妙的辦法,就是在item的布局里面搞事情
圖畫(huà)的有點(diǎn)丑蹂安,用代碼來(lái)說(shuō)話(huà)
<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>
里面這個(gè)RelativeLayout就是你的item正常的布局椭迎,而LinearLayout 這個(gè)根部局我設(shè)置了他的背景色為透明的,再加一個(gè)padding就行了田盈,這個(gè)padding就是設(shè)置item的間距畜号,這樣設(shè)置item的間距了。
所以說(shuō)RecyclerView的Item的間距都可以通過(guò)item里面設(shè)置padding允瞧,margin來(lái)解決简软,這種辦法相對(duì)巧妙。