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
        }
    }
}
相关推荐
y = xⁿ43 分钟前
MySQL:count(1)与count(*)有什么区别,深分页问题
android·数据库·mysql
程序员陆业聪2 小时前
Android启动全景图:一次冷启动背后到底发生了什么
android
安卓程序员_谢伟光4 小时前
m3颜色定义
android·compose
麻辣璐璐5 小时前
EditText属性运用之适配RTL语言和LTR语言的输入习惯
android·xml·java·开发语言·安卓
北京自在科技5 小时前
谷歌 Find Hub 网页端全面升级:电脑可直接管理追踪器与耳机
android·ios·安卓·findmy
Rush-Rabbit5 小时前
魅族21Pro刷ColorOS16.0操作步骤
android
爪洼传承人6 小时前
AI工具MCP的配置,慢sql优化
android·数据库·sql
学习使我健康6 小时前
MVP模式
android·github·软件工程
xiangxiongfly9156 小时前
Android MMKV
android·mmkv
北漂Zachary7 小时前
PHP3.0:改变Web开发的里程碑
android·php·laravel