今天在用RecyclerView做一個(gè)橫向視圖的時(shí)間軸控件時(shí)發(fā)現(xiàn)item之間總有一些空隙,而且明明子item之間的padding為0,在上網(wǎng)找到可以通過(guò)addItemDecoration來(lái)改變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;
//解決Item之間總是有空隙的問(wèn)題,根據(jù)需要設(shè)置左右間隔為負(fù)數(shù)解決問(wèn)題
//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;
}
}
先重寫(xiě)RecyclerView.ItemDecoration方法設(shè)置Item邊距
mRecyclerView.addItemDecoration(new SpacesItemDecoration(-3));
最后為RecyclerView加一個(gè)addItemDecoration為-3解決間隙的問(wèn)題