RecycleView的Item文字超过边界

使用RecycleView,可能遇到Item的文字描述超过其边界的情况,当Content111111是从Item的中间往后展示的,要再遇到多语言翻译......

可以使用LinearLayoutManager的翻转属性 rv.layoutManager = LinearLayoutManager( requireContext(), LinearLayoutManager.HORIZONTAL, true )

并配合rightSpacing属性

rv.addItemDecoration(SpacingItemDecoration().apply { rightSpacing = CommonUtils.dp2px(context, -100f) })

注意,adpter的xml中Item的宽要比预设的多100dp,为了正常展示文字

最后,adapter的list顺序也要翻转:

ini 复制代码
val list = listof("Content4", "Content3333", "Content2", "Content111111")

SpacingItemDecoration的代码如下:

kotlin 复制代码
open class SpacingItemDecoration : RecyclerView.ItemDecoration() {
    var leftSpacing:Int = 0
    var rightSpacing:Int = 0
    var topSpacing:Int = 0
    var bottomSpacing:Int = 0
    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
        super.getItemOffsets(outRect, view, parent, state)
        if (leftSpacing != 0) {
            outRect.left = leftSpacing
        }
        if (rightSpacing != 0) {
            outRect.right = rightSpacing
        }
        if (topSpacing != 0) {
            outRect.top = topSpacing
        }
        if (bottomSpacing != 0) {
            outRect.bottom = bottomSpacing
        }
    }
}
相关推荐
JohnnyDeng947 小时前
【Android】Hilt 依赖注入:原理与最佳实践
android·kotlin·mvvm·hilt
星间都市山脉10 小时前
Android STS(Security Test Suite)完整介绍与测试流程
android·java·linux·windows·ubuntu·android studio·androidx
Yeyu11 小时前
你真的了解AIDL吗? 附:AIDL 与 Binder 交互全解析
android
dualven_in_csdn13 小时前
一键起飞调用示例
android·java·javascript
故渊at13 小时前
第十板块:Android 系统稳定性与调试 | 第二十五篇:Watchdog 与 ANR 的系统级监控
android·watchdog·系统稳定性·anr·超时监控
故渊at14 小时前
第十板块:Android 系统稳定性与调试 | 第二十六篇:Systrace 与 Perfetto 的系统级性能分析
android·perfetto·性能分析·systrace·系统稳定性
吕工-老船长199814 小时前
20260610----S905Y5(Android14)-----连接网络自动更新时间,时间设置为24小时
android
杉氧15 小时前
Kotlin 协程深度解析④:架构实战——在 MVVM/MVI 中的进阶应用
android·kotlin
Ab_stupid15 小时前
CTF-Android培训笔记
android·笔记