RecyclerView divider

档添加RecyclerView的Divider时候,要加入距离,不然除了第一Item之后的item 设置的距离会有问题。

正解如下。

1,

//set recycler view

recyclerView= (RecyclerView)v.findViewById(R.id.recycler_view);

recyclerView.setLayoutManager(newLinearLayoutManager(getActivity()));

//adding custom divider

recyclerView.addItemDecoration(newSimpleDividerItemDecoration(getContext()));

2,

public classSimpleDividerItemDecorationextendsRecyclerView.ItemDecoration {

privateDrawablemDivider;

publicSimpleDividerItemDecoration(Context context) {

mDivider= context.getResources().getDrawable(R.drawable.divider);

}

@Override

public voidonDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {

intleft = parent.getPaddingLeft();

intright = parent.getWidth() - parent.getPaddingRight();

intchildCount = parent.getChildCount();

for(inti =0; i < childCount; i++) {

View child = parent.getChildAt(i);

RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();

inttop = child.getBottom() + params.bottomMargin;

intbottom = top +mDivider.getIntrinsicHeight();mDivider.setBounds(left, top, right, bottom);

mDivider.draw(c);

}

}

//adding space

@Override

public voidgetItemOffsets(Rect outRect, View view, RecyclerView parent,

RecyclerView.State state) {

outRect.bottom= (int) ZR.convertDpToPx(view.getContext(),6);

}

}

3,divider.xml

android:shape="rectangle">

你可能感兴趣的:(RecyclerView divider)