RecyclerViewは各item間の間隔を設定します
9461 ワード
RecyclerViewは各item間の間隔を設定します
private void initDecoration() {
HashMap<String, Integer> stringIntegerHashMap = new HashMap<>();
stringIntegerHashMap.put(RecyclerViewSpacesItemDecoration.TOP_DECORATION, 0);//top
stringIntegerHashMap.put(RecyclerViewSpacesItemDecoration.BOTTOM_DECORATION, 0);//
stringIntegerHashMap.put(RecyclerViewSpacesItemDecoration.LEFT_DECORATION, 6);//
stringIntegerHashMap.put(RecyclerViewSpacesItemDecoration.RIGHT_DECORATION, 6);//
firstRecycler.addItemDecoration(new RecyclerViewSpacesItemDecoration(stringIntegerHashMap));
secondRecycler.addItemDecoration(new RecyclerViewSpacesItemDecoration(stringIntegerHashMap));
}
public class RecyclerViewSpacesItemDecoration extends RecyclerView.ItemDecoration {
private HashMap<String, Integer> mSpaceValueMap;
public static final String TOP_DECORATION = "top_decoration";
public static final String BOTTOM_DECORATION = "bottom_decoration";
public static final String LEFT_DECORATION = "left_decoration";
public static final String RIGHT_DECORATION = "right_decoration";
public RecyclerViewSpacesItemDecoration(HashMap<String, Integer> mSpaceValueMap) {
this.mSpaceValueMap = mSpaceValueMap;
}
@Override
public void getItemOffsets(Rect outRect, View view,
RecyclerView parent, RecyclerView.State state) {
if (mSpaceValueMap.get(TOP_DECORATION) != null)
outRect.top = mSpaceValueMap.get(TOP_DECORATION);
if (mSpaceValueMap.get(LEFT_DECORATION) != null)
outRect.left = mSpaceValueMap.get(LEFT_DECORATION);
if (mSpaceValueMap.get(RIGHT_DECORATION) != null)
outRect.right = mSpaceValueMap.get(RIGHT_DECORATION);
if (mSpaceValueMap.get(BOTTOM_DECORATION) != null)
outRect.bottom = mSpaceValueMap.get(BOTTOM_DECORATION);
}
}