[英]How to have different spacing between items in the recyclerview
我有一個UiItemDecoration類來管理間距。
public class UiItemDecoration extends RecyclerView.ItemDecoration {
private int spanCount;//一行item數目
private int spacing;//item 之間的間隔
private boolean includeEdge;//邊緣
private boolean includeTop;
public UiItemDecoration(int spanCount, int spacing, boolean includeEdge) {
this.spanCount = spanCount;
this.spacing = spacing;
this.includeEdge = includeEdge;
this.includeTop = true;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
int position = parent.getChildAdapterPosition(view); // item position
outRect.left = 0;
outRect.right = 10;
}
當我想有相同的間距時,它很好用。如下圖所示 問題是我想在類中定義的recyclerview的項目之間使用不同的間距:
public class UiItemDecoration extends RecyclerView.ItemDecoration {
private int spanCount;//一行item數目
private int spacing;//item 之間的間隔
private boolean includeEdge;//邊緣
private boolean includeTop;
public UiItemDecoration(int spanCount, int spacing, boolean includeEdge) {
this.spanCount = spanCount;
this.spacing = spacing;
this.includeEdge = includeEdge;
this.includeTop = true;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
int position = parent.getChildAdapterPosition(view); // item position
if (position%5 == 2){
outRect.right = 30;
}else {
outRect.right = 5;
}
outRect.left = 0;
}
您可以根據適配器的onBindViewHolder()方法中的位置來更改邊距,而不是更改偏移量。 如果您使用RelativeLayout創建項目布局,則此代碼將起作用。 同樣,調整邊距不會影響項目的視圖范圍。
@Override
public void onBindViewHolder(@NonNull final AdapterViewHolder holder, int position) {
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) holder.itemView.getLayoutParams();
if (position%5 == 2){
layoutParams.setMargins(0, 0, 30, 0);
}else {
layoutParams.setMargins(0, 0, 5, 0);
}
holder.itemView.setLayoutParams(layoutParams);
}
用這個,
public class ItemOffsetDecoration extends RecyclerView.ItemDecoration {
public static final int SPAN_COUNT = 4;
private static final int DEFAULT_LEFT_OFFSET = 0;
private static final int DEFAULT_RIGHT_OFFSET = 0;
private static final int DEFAULT_TOP_OFFSET = 0;
private static final int DEFAULT_BOTTOM_OFFSET = 0;
private int mItemOffset;
public ItemOffsetDecoration(int itemOffset) {
mItemOffset = itemOffset;
}
/**
* @brief Method to get offset for item views(child)
* and modifying all bounds- top, bottom, left and right for equal spacing.
* @param outRect : Rect, the number of pixels in which a view should be inserted.
* @param view : Views (child) which to be modified or decorated.
* @param parent : Corresponding recycler view where decorations being applied.
* @param state : Recycler view State state, contains focus, scroll etc info.
*/
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
int position = parent.getChildAdapterPosition(view); // item position
// column count fixed = 5
if (position >= 0) {
int column = position % SPAN_COUNT; // item column
outRect.left = mItemOffset - column * mItemOffset / SPAN_COUNT;
outRect.right = (column + 1) * mItemOffset / SPAN_COUNT;
if (position < SPAN_COUNT) { // top edge
outRect.top = mItemOffset;
}
outRect.bottom = mItemOffset; // item bottom
} else {
outRect.left = DEFAULT_LEFT_OFFSET;
outRect.right = DEFAULT_RIGHT_OFFSET;
outRect.top = DEFAULT_TOP_OFFSET;
outRect.bottom = DEFAULT_BOTTOM_OFFSET;
}
}
}
我正在使用mItemOffset = -6; //自己修改
別忘了
GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), SPAN_COUNT);//columns
mRecyclerView.setLayoutManager(gridLayoutManager);
mRecyclerView.addItemDecoration(new ItemOffsetDecoration(SPACING));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.