RecyclerView設定verticalSapcingなど
2073 ワード
RecyclerViewはGridViewのようにverticalSpacingプロパティを提供していません.StackOverflowでは、次のような代替方法が見つかりました.
RecyclerViewでの使用
public class SpacesItemDecoration extends RecyclerView.ItemDecoration {
private int mVerticalSpacing, mHorizontalSpacing;
public SpacesItemDecoration(int verticalSpacing, int horizontalSpacing){
mHorizontalSpacing = horizontalSpacing;
mVerticalSpacing = verticalSpacing;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
outRect.left = mHorizontalSpacing;
outRect.right = mHorizontalSpacing;
outRect.bottom = mVerticalSpacing;
outRect.top = mVerticalSpacing;
}
}
RecyclerViewでの使用
int verticalSpacing = getResources().getDimensionPixelSize(R.dimen.my_items_grid_vertical_spacing);
int horizontalSpacing = getResources().getDimensionPixelSize(R.dimen.my_items_grid_horizontal_spacing);
recyclerView.addItemDecoration(new SpacesItemDecoration(verticalSpacing, horizontalSpacing));