Recyclerview詳細(二)ItemDecoration定義分割線
3548 ワード
Recyclerviewで分割効果を実現するにはItemDecorationが必要です.これは抽象的なクラスで、分割効果を描画するために使用されます.使用するときはこの方法を書き換える必要があります.次に、彼のソースコードを見てみましょう.
もう推奨されていないものを取り除くには、3つの主な方法があります.
これらを理解すると、単純な分割効果が得られ、ここではVertical方向のRecyclerviewに対して黒の分割を描画する.
まず分割線を定義し、ShapeDrawableを使用して5 pxの高さの黒いグラフィック(divider.xml)を定義します.
次に、ItemDecorationを実装し、分割線を描画します.
最後にRecyclerviewに分割を設定し、
分割線の詳細については鴻揚大神のブログAndroid RecyclerViewを参考に完全解析体験芸術のようなコントロールを使い、themeの
public static abstract class ItemDecoration {
@param c Canvas to draw into
@param parent RecyclerView this ItemDecoration is drawing into
@param state The current state of RecyclerView
public void onDraw(Canvas c, RecyclerView parent, State state) {
onDraw(c, paren);
@param c Canvas to draw into
@param parent RecyclerView this ItemDecoration is drawing into
@param state The current state of RecyclerView.
public void onDrawOver(Canvas c, RecyclerView parent, State state) {
onDrawOver(c, parent);
}
@param outRect Rect to receive the output.
@param view The child view to decorate
@param parent RecyclerView this ItemDecoration is decorating
@param state The current state of RecyclerView.
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, State state) {
getItemOffsets(outRect, ((LayoutParams) view.getLayoutParams()).getViewLayoutPosition(),
parent);
}
}
もう推奨されていないものを取り除くには、3つの主な方法があります.
onDraw(Canvas c, RecyclerView parent, State state)
は分割線を描画するために使用され、ItemViewが描画される前に、分割線が境界を越えた場合、ItemViewの上部onDrawOver(Canvas c, RecyclerView parent, State state)
に分割線を描画するために使用され、ItemViewが描画された後に表示されます.分割線が境界を越えた場合、itemviewに対応する分割線の位置とサイズを指定するために、ItemView下層getItemOffsets
に表示されます.outRectにはdecorationの4つの角座標が格納されています.outRect.set(l, t, r, b)
で設定します.その原理は実際にはItemViewにPaddingを設定し、decorationに対応する空間を残し、単位はpxである.これらを理解すると、単純な分割効果が得られ、ここではVertical方向のRecyclerviewに対して黒の分割を描画する.
まず分割線を定義し、ShapeDrawableを使用して5 pxの高さの黒いグラフィック(divider.xml)を定義します.
次に、ItemDecorationを実装し、分割線を描画します.
private class BlackDecoration extends RecyclerView.ItemDecoration{
private Drawable mDivider;
public BlackDecoration(Context context) {
// Drawable
mDivider = context.getDrawable(R.drawable.divider);
}
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
super.onDraw(c, parent, state);
// Drawable , , , Recyclerview padding , ItemView
int left = parent.getPaddingLeft();
int right = parent.getWidth() - parent.getPaddingRight();
// , Item top Bottom
for(int i = 0; i
最後にRecyclerviewに分割を設定し、
mRecyclerView.setAdapter(new RecycleAdapter());
を実行すると効果が見え、itemごとに黒い分割線が表示されます.分割線の詳細については鴻揚大神のブログAndroid RecyclerViewを参考に完全解析体験芸術のようなコントロールを使い、themeの
- @drawable/divider_bg
を指定することで異なる分割効果を実現できます