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
        }
    }
}
相关推荐
恋猫de小郭2 分钟前
小米 HyperOS 4 大变样?核心应用以 Rust / Flutter 重写,不兼容老系统
android·前端·人工智能·flutter·ios
JMchen1235 分钟前
Android TCP连接实战:详解一个高效可靠的TCP客户端实现
android·java·经验分享·网络协议·tcp/ip·移动开发·android-studio
灯火不休ᝰ8 分钟前
[安卓] Kotlin中的架构演进:从MVC到MVVM
android·架构·kotlin
JMchen12310 分钟前
AR Core与CameraX的融合:测量应用从原理到实现
android·经验分享·程序人生·ar·移动开发·android studio·camerax
June bug26 分钟前
【领域知识】一个休闲游戏产品(安卓和iOS)从0到1
android·ios
zgyhc205026 分钟前
【Android Audio】Android Audio有线设备插拔监听机制
android
ZHANG13HAO27 分钟前
android13 系统强制wifi连接到内网,假装具有互联网能力
android
2501_9151063237 分钟前
iOS 如何绕过 ATS 发送请求,iOS调试
android·ios·小程序·https·uni-app·iphone·webview
JMchen12342 分钟前
Android相机硬件抽象层(HAL)逆向工程:定制ROM的相机优化深度指南
android·开发语言·c++·python·数码相机·移动开发·android studio
ANYOUZHEN9 小时前
bugku shell
android