將分隔線新增到 RecyclerView 專案

只需將這些行新增到初始化

RecyclerView mRecyclerView = (RecyclerView) view.findViewById(recyclerView);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
mRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));

新增 adapter 並像往常一樣呼叫 .notifyDataSetChanged();
這不是 Recyclerview 的內建函式,而是新增到支援庫中。所以不要忘記在你的應用程式級 build.gradle 檔案中包含它

compile "com.android.support:appcompat-v7:25.3.1"
compile "com.android.support:recyclerview-v7:25.3.1"

可以將多個 ItemDecorations 新增到單個 RecyclerView 中。

改變分頻器顏色

為 itemDecoration 設定顏色非常簡單。

  1. 步驟是:建立一個位於 drawable 資料夾的 divider.xml 檔案
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="line">
    <size
        android:width="1px"
        android:height="1px"/>
    <solid android:color="@color/divider_color"/>
</shape>
  1. 步驟是:設定 drawable
    // Get drawable object
    Drawable mDivider = ContextCompat.getDrawable(m_jContext, R.drawable.divider);
    // Create a DividerItemDecoration whose orientation is Horizontal
    DividerItemDecoration hItemDecoration = new DividerItemDecoration(m_jContext,
            DividerItemDecoration.HORIZONTAL);
    // Set the drawable on it
    hItemDecoration.setDrawable(mDivider);

StackOverflow 文件

    // Create a DividerItemDecoration whose orientation is vertical
    DividerItemDecoration vItemDecoration = new DividerItemDecoration(m_jContext,
            DividerItemDecoration.VERTICAL);
    // Set the drawable on it
    vItemDecoration.setDrawable(mDivider);

StackOverflow 文件