以下代码根本不创建分隔符:

override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_notifications)

        val titleList = mutableListOf<NotificationsItem>()
        titleList.add(NotificationsItem("Title 1", "Description is good!", true))
        titleList.add(NotificationsItem("Title 2", "Description is good!", true))
        titleList.add(NotificationsItem("Title 3", "Description is good!", false))
        titleList.add(NotificationsItem("Title 4", "Description is good!", false))
        titleList.add(NotificationsItem("Title 5", "Description is good!", true))
        titleList.add(NotificationsItem("Title 6", "Description is good!", true))
        titleList.add(NotificationsItem("Title 7", "Description is good!", false))
        titleList.add(NotificationsItem("Title 8", "Description is good!", true))
        titleList.add(NotificationsItem("Title 9", "Description is good!", false))
        titleList.add(NotificationsItem("Title 10", "Description is good!", true))
        titleList.add(NotificationsItem("Title 11", "Description is good!", false))
        titleList.add(NotificationsItem("Title 12", "Description is good!", false))


        val recycler = findViewById<RecyclerView>(R.id.recyclerNotifications)
        recycler.adapter = NotificationsAdapter(titleList)
        recycler.layoutManager = LinearLayoutManager(this)

        val dividerItemDecoration = DividerItemDecoration(recycler.getContext(), LinearLayoutManager(this).orientation)
        dividerItemDecoration.setDrawable(ColorDrawable(recycler.getContext().resources.getColor(R.color.orange)))
        recycler.addItemDecoration(dividerItemDecoration)
    }

这将产生以下输出:

enter image description here

如果我删除 colored颜色 更改,它将使用默认 colored颜色 :

val dividerItemDecoration = DividerItemDecoration(recycler.getContext(), LinearLayoutManager(this).orientation)
recycler.addItemDecoration(dividerItemDecoration)

enter image description here

为什么它不能适应 colored颜色 的变化?

推荐答案

要将自定义 colored颜色 设置为DividerItemDecoration,您需要创建一个新的ColorDrawable,覆盖两个公共方法getIntrinsicWidth()getIntrinsicHeight(),并分别返回可绘制对象的固有宽度和高度.

Java Example:

DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.VERTICAL);
dividerItemDecoration.setDrawable(new ColorDrawable(ContextCompat.getColor(recyclerView.getContext(), android.R.color.holo_red_light)){
    @Override
    public int getIntrinsicWidth(){
        return getResources().getDisplayMetrics().widthPixels / getResources().getDisplayMetrics().densityDpi;
    }

    @Override
    public int getIntrinsicHeight(){
        return (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1f, getResources().getDisplayMetrics());
    }
});
recyclerView.addItemDecoration(dividerItemDecoration);

Kotlin Example:

val dividerItemDecoration = DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.VERTICAL)
dividerItemDecoration.setDrawable(object : ColorDrawable(ContextCompat.getColor(recyclerView.getContext(), android.R.color.holo_red_light)) {
    override fun getIntrinsicWidth(): Int {
        return resources.displayMetrics.widthPixels / resources.displayMetrics.densityDpi
    }
    
    override fun getIntrinsicHeight(): Int {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1f, resources.displayMetrics).toInt()
    }
})
recyclerView.addItemDecoration(dividerItemDecoration)

Another way with 100:

使用MaterialDividerItemDecoration,您只需使用setDividerColorResource更改分隔符 colored颜色 ,如下面的Kotlin示例所示:

val dividerItemDecoration = MaterialDividerItemDecoration(recyclerView.context, DividerItemDecoration.VERTICAL)
dividerItemDecoration.setDividerColorResource(recyclerView.context, android.R.color.holo_red_light)
recyclerView.addItemDecoration(dividerItemDecoration)

Result:

divider

Android相关问答推荐

如何在Reaction Native中显示Google Map iFrame?

为什么Ionic-checkbox不总是刷新Android上的视图?

Android-LVL库始终返回NOT_SUBLISTED

在Jetpack Compose中实现焦点突出的最佳方式?

在 kotlin 上向适配器添加绑定视图功能

如何在同一行中滚动自定义布局和惰性列,就好像它们是一个组件一样

通过 setIntentScanningStrategyEnabled(true) 未检测到信标的 Android Beacon 库后台扫描

在 MVVM Jetpack Compose 上添加依赖项时出现重复类错误

Compose 状态不是 recomposing

[Android][Room] 将密封类存储到 Room 数据库中

在 Android Studio 中获取更新版本的 Picasso 库的错误警告消息

使用 Kotlin 在 Android 中导航时如何防止 ViewModel 被杀死?

如何在 Android Jetpack compose 中为列表初始填充设置动画

自定义 Recyclerview [Android & Kotlin]

如何在 TextButton 中分隔文本和图标

JetPack Compose - 卡片行中的权重()不起作用

使用 Jetpack Compose 时,如何以简单的方式在 Color.kt 中定义 colored颜色 ?

Jetpack Compose:如何绘制这样的路径/线

未解决的参考:getIntentSender / try 在 Jetpack Compose 中获取电话号码时

单独窗口中的 Android Studio 模拟器