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
        }
    }
}
相关推荐
TheNextByte15 分钟前
在 PC 和Android之间同步音乐的 4 种方法
android
君莫啸ོ32 分钟前
Android基础-Activity属性 android:configChanges
android
TimeFine39 分钟前
Android AI解放生产力(七):更丰富的AI运用前瞻
android
保持低旋律节奏1 小时前
linux——进程状态
android·linux·php
明川1 小时前
Android Gradle - ASM + AsmClassVisitorFactory插桩使用
android·前端·gradle
csdn12259873362 小时前
Android将应用添加到默认打开方式
android
百锦再2 小时前
京东云鼎入驻方案解读——通往协同的“高架桥”与“快速路”
android·java·python·rust·django·restful·京东云
成都大菠萝2 小时前
1-2-3 Kotlin与C++基础-JNI原理与使用
android
TimeFine2 小时前
Android AI解放生产力(六)实战:解放页面开发前的繁琐工作
android·架构
心静财富之门2 小时前
a.py打包加密
android