在LinearLayoutManager中设置分隔线可以通过ItemDecoration来实现。首先,你需要创建一个自定义的ItemDecoration类,并重写它的onDraw方法来绘制分隔线。以下是一个示例代码:
public class DividerItemDecoration extends RecyclerView.ItemDecoration { private Drawable mDivider; public DividerItemDecoration(Context context) { mDivider = ContextCompat.getDrawable(context, R.drawable.divider); } @Override public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) { int left = parent.getPaddingLeft(); int right = parent.getWidth() - parent.getPaddingRight(); for (int i = 0; i < parent.getChildCount() - 1; i++) { View child = parent.getChildAt(i); RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams(); int top = child.getBottom() + params.bottomMargin; int bottom = top + mDivider.getIntrinsicHeight(); mDivider.setBounds(left, top, right, bottom); mDivider.draw(c); } } }
然后,在你的RecyclerView中设置这个ItemDecoration:
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext()); recyclerView.addItemDecoration(dividerItemDecoration);
这样就可以在LinearLayoutManager中设置分隔线了。你也可以根据需求自定义分隔线的样式和颜色。