將分隔符新增到 RecyclerView

首先,你需要建立一個擴充套件 RecyclerView.ItemDecoration 的類:

public class SimpleBlueDivider extends RecyclerView.ItemDecoration {
private Drawable mDivider;

public SimpleBlueDivider(Context context) {
    mDivider = context.getResources().getDrawable(R.drawable.divider_blue);
}

@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
    //divider padding give some padding whatever u want or disable
    int left =parent.getPaddingLeft()+80;
    int right = parent.getWidth() - parent.getPaddingRight()-30;

    int childCount = parent.getChildCount();
    for (int i = 0; i < childCount; 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);
    }
}

}

divider_blue.xml 新增到你的 drawable 資料夾:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<size android:width="1dp" android:height="4dp" />
<solid android:color="#AA123456" />
</shape>

然後使用它像:

recyclerView.addItemDecoration(new SimpleBlueDivider(context));

結果如下:

StackOverflow 文件

這個影象只是分頻器如何工作的一個例子,如果你想在新增分頻器時遵循 Material Design 規範,請看一下這個連結: 分頻器並通過提供連結感謝 @Brenden Kromhout