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
        }
    }
}
相关推荐
Deryck_德瑞克2 小时前
【已解决】MySQL连接出错 1045 - Access denied for user ‘root‘@‘::1‘
android·mysql·adb
2501_915918413 小时前
iOS性能测试工具 Instruments、Keymob的使用方法 不局限 FPS
android·ios·小程序·https·uni-app·iphone·webview
.豆鲨包3 小时前
【Android】组件化搭建的一般流程
android
心有—林夕4 小时前
MySQL 误操作恢复完全指南
android·数据库·mysql
忙什么果4 小时前
Mamba学习笔记2:Mamba模型
android·笔记·学习
Wyawsl5 小时前
MySQL故障排查与优化
android·adb
私人珍藏库7 小时前
[Android] 后台视频录制 FadCam v3.0.1
android·app·工具·软件·多功能
Z_Wonderful7 小时前
在 **Next.js** 中使用 `mysql2` 连接 MySQL 数据库并查询 `xxx` 表的数据
android·数据库
FirstFrost --sy7 小时前
MySql 内外连接
android·数据库·mysql
激昂网络7 小时前
在Ubuntu 24.04上编译T527 Android系统:遇到的几个问题及解决方法
android·linux·ubuntu