44. アイテム用のViewのOffsetを指定
final int offset = (int) (8 * getResources().getDisplayMetrics().density);?
?
RecyclerView.ItemDecoration itemDecoration = new RecyclerView.ItemDecoration() {?
@Override?
public void getItemOffsets(Rect outRect, View view,?
RecyclerView parent, RecyclerView.State state) {?
outRect.set(offset, offset, offset, offset);?
}?
};?
recyclerView.addItemDecoration(itemDecoration);
46. アイテム用のViewのOffsetを指定
RecyclerView.ItemDecoration itemDecoration = new RecyclerView.ItemDecoration() {?
@Override?
public void getItemOffsets(Rect outRect, View view,?
RecyclerView parent, RecyclerView.State state) {?
int position = ((RecyclerView.LayoutParams) view.getLayoutParams())?
.getViewLayoutPosition();?
if (position == 0) {?
outRect.set(offset, offset, offset, offset);?
} else {?
outRect.set(offset, 0, offset, offset);?
}?
}?
};?
?
recyclerView.addItemDecoration(itemDecoration);
48. public class DividerDecoration extends RecyclerView.ItemDecoration {?
?
private final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);?
private final int dividerHeight;?
?
public DividerDecoration(Resources res) {?
paint.setColor(Color.GRAY);?
dividerHeight = (int) (4 * res.getDisplayMetrics().density);?
}?
?
@Override?
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, ?
RecyclerView.State state) {?
final int position = ((RecyclerView.LayoutParams) view.getLayoutParams())?
.getViewLayoutPosition();?
// 位置が2番目以降なら上部にdividerを描画したいので、?
// divider分だけ上をあける?
?
int top = position == 0 ? 0 : dividerHeight;?
outRect.set(0, top, 0, 0);?
}
Dividerを描画
49. @Override?
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {?
super.onDrawOver(c, parent, state);?
// アイテムのビューより上に描画される?
?
?
final RecyclerView.LayoutManager manager = parent.getLayoutManager();?
final int left = parent.getPaddingLeft();?
final int right = parent.getWidth() - parent.getPaddingRight();?
final int childCount = parent.getChildCount();?
for (int i = 1; i < childCount; i++) {?
final View child = parent.getChildAt(i);?
final RecyclerView.LayoutParams params =?
(RecyclerView.LayoutParams) child.getLayoutParams();?
if (params.getViewLayoutPosition() == 0) {?
continue;?
}?
?
// ViewCompat.getTranslationY()を入れないと?
// 追加?削除のアニメーション時の位置が変になる?
final int top = manager.getDecoratedTop(child)?
- params.topMargin + Math.round(ViewCompat.getTranslationY(child));?
final int bottom = top + dividerHeight;?
c.drawRect(left, top, right, bottom, paint);?
}?
}?
}?
Dividerを描画
61. public abstract class ArrayAdapter<T, VH extends RecyclerView.ViewHolder> extends
RecyclerView.Adapter<VH> {?
?
private final Object lock = new Object();?
private final List<T> objects;?
?
public ArrayAdapter() {?
this(new ArrayList<T>());?
}?
?
public ArrayAdapter(List<T> objects) {?
this.objects = objects;?
}?
?
public void add(@NonNull T object) {?
final int position;?
synchronized (lock) {?
position = objects.size();?
objects.add(object);?
}?
notifyItemInserted(position);?
}?
ArrayAdapter的なRecyclerView用Adapter
62. public void addAll(@NonNull Collection<? extends T> collection) {?
final int itemCount = collection.size();?
final int positionStart;?
synchronized (lock) {?
positionStart = objects.size();?
objects.addAll(collection);?
}?
notifyItemRangeInserted(positionStart, itemCount);?
}?
?
public void insert(@NonNull T object, int index) {?
synchronized (lock) {?
objects.add(index, object);?
}?
notifyItemInserted(index);?
}?
?
public void remove(@NonNull T object) {?
final int position = objects.indexOf(object);?
synchronized (lock) {?
objects.remove(object);?
}?
notifyItemRemoved(position);?
}?
}
ArrayAdapter的なRecyclerView用Adapter