标签:上下左右 put hashmap cimage logs rect 设置 cycle auto
想要设置item之间的间距需要自己创建一个继承自RecyclerView.ItemDecoration的类
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); } }
HashMap<String, Integer> stringIntegerHashMap = new HashMap<>(); stringIntegerHashMap.put(RecyclerViewSpacesItemDecoration.TOP_DECORATION,50);//top间距 stringIntegerHashMap.put(RecyclerViewSpacesItemDecoration.BOTTOM_DECORATION,100);//底部间距 stringIntegerHashMap.put(RecyclerViewSpacesItemDecoration.LEFT_DECORATION,50);//左间距 stringIntegerHashMap.put(RecyclerViewSpacesItemDecoration.RIGHT_DECORATION,100);//右间距 mRecyclerView.addItemDecoration(newRecyclerViewSpacesItemDecoration(stringIntegerHashMap));
可以根据自己的实际情况去设置想要的间距,也可以去单独设置
下面是设置间距后的效果图
标签:上下左右 put hashmap cimage logs rect 设置 cycle auto
原文地址:http://www.cnblogs.com/zhujiabin/p/7436580.html