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
        }
    }
}
相关推荐
程序员陆业聪2 小时前
两次Flutter全屏白踩坑复盘:Layout的静默失败,以及AI结对编程的认知盲区
android
程序员陆业聪3 小时前
Compose Strong Skipping Mode 的真相:它并不会让你的类型变 Stable
android
shaoming37768 小时前
浏览器动作开发:地址栏图标点击事件、弹出页面设计
android·mysql·adb
赏金术士8 小时前
Kotlin 协程与挂起函数(Coroutines & suspend)入门到实战
android·开发语言·kotlin
泡泡以安11 小时前
Unidbg学习笔记(十三):固定随机干扰项
android·逆向
泡泡以安11 小时前
Unidbg学习笔记(十六):Console Debugger
android·逆向
赏金术士11 小时前
Room + Flow 完整教程(现代 Android 官方方案)
android·kotlin·room·compose
泡泡以安11 小时前
Unidbg学习笔记(八):文件系统层补环境
android·逆向
泡泡以安11 小时前
Unidbg学习笔记(六):补环境的思维框架
android·逆向
通往曙光的路上11 小时前
mysql2
android·adb