概述
从别的文章看到的 但是不满足需求 或者我写错了 就我改了过来
public class SpacesItemDecoration extends RecyclerView.ItemDecoration {
private HashMap<String, Integer> spaceValue; //间隔
private boolean includeEdge; //是否包含四周边距
private int spanCount; //列数
public static final String TOP_SPACE = "top_space";
public static final String BOTTOM_SPACE = "bottom_space";
public static final String LEFT_SPACE = "left_space";
public static final String RIGHT_SPACE = "right_space";
public SpacesItemDecoration(int spanCount, HashMap<String, Integer> spaceValue, boolean includeEdge) {
this.spanCount = spanCount;
this.spaceValue = spaceValue;
this.includeEdge = includeEdge;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
//这里是关键,需要根据你有几列来判断
int position = parent.getChildAdapterPosition(view); // item position
int column = position % spanCount; // item column
if (includeEdge) {
outRect.left = spaceValue.get(LEFT_SPACE) - column * spaceValue.get(LEFT_SPACE) / spanCount;
outRect.right = (column + 1) * spaceValue.get(RIGHT_SPACE) / spanCount;
if (position < spanCount) { // top edge 判断是首行
outRect.top = spaceValue.get(TOP_SPACE);
}
outRect.bottom = spaceValue.get(BOTTOM_SPACE); // item bottom
} else {
//outRect.left = column * spaceValue.get(LEFT_SPACE) / spanCount;
if((column+1)/spanCount==0){
outRect.right=spaceValue.get(RIGHT_SPACE);
}else {
outRect.right=0;
}
//outRect.right = spaceValue.get(RIGHT_SPACE) - (column + 1) * spaceValue.get(RIGHT_SPACE) / spanCount;
/* if (position >= spanCount) {//不是首行
outRect.top = spaceValue.get(TOP_SPACE); // item top
}*/
outRect.bottom = spaceValue.get(BOTTOM_SPACE); // item bottom
//只有首行设置上边距
//if (position >= spanCount) {//不是首行
// outRect.top = spaceValue.get(TOP_SPACE); // item top
//}else {
// outRect.top = 20; //首行
//}
//只有下边距
// if (position > (parent.getAdapter().getItemCount() - spanCount)-1) {
// outRect.bottom = 100;
// }
}
}
}
我是不要外部边距,只要右下间距
HashMap<String, Integer> spacesVelue = new HashMap<>();
spacesVelue.put(SpacesItemDecoration.TOP_SPACE,0);
spacesVelue.put(SpacesItemDecoration.BOTTOM_SPACE,4);
spacesVelue.put(SpacesItemDecoration.LEFT_SPACE, 0);
spacesVelue.put(SpacesItemDecoration.RIGHT_SPACE,2);
recyclerView.addItemDecoration(new SpacesItemDecoration(3,spacesVelue, false));
最后
以上就是重要太阳为你收集整理的Android RecyclerView item 间距 及是否包含四周边距 的设置的全部内容,希望文章能够帮你解决Android RecyclerView item 间距 及是否包含四周边距 的设置所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复