简体   繁体   English

在 recylerview android 中以编程方式更改布局管理器

[英]Change layout Manager programatically in recylerview android

I want to change layoutmanager stackFromEnd and reverseLayout change when view is inflated.我想在视图膨胀时更改layoutmanager stackFromEndreverseLayout更改。 I have nested recylerview.我嵌套了 recylerview。 So when recylerview inflated, i need to check that recylerview has more item than single screen ie I have more than items to scroll.因此,当 recylerview 膨胀时,我需要检查 recylerview 是否有比单个屏幕更多的项目,即我有多个项目要滚动。 I will show in image.我将在图像中显示。

在此处输入图片说明

Also other condition when item is half cut from bottom edge.当项目从底部边缘切半时的其他情况。

在此处输入图片说明 . .

On that basis I want to change layout on that.在此基础上,我想更改布局。 Is it possible to that and how?有可能吗? I tried to some stack overflow Example but it not's work.我尝试了一些堆栈溢出示例,但它不起作用。 So anyone know how to achieve that thing?.所以有人知道如何实现那件事吗?

So I wonder if there is any listener that is called when the RecyclerView has finished laying down its items for the first time, making it possible to set the layout manager.所以我想知道是否有任何侦听器在RecyclerView第一次完成放置其项目时调用,从而可以设置布局管理器。

Im assuming you have a parent view for your recycler views, eg NestedScrollView .我假设您有一个用于回收器视图的父视图,例如NestedScrollView Then you will need to get the height of your NestedScrollView after it being created in your screen然后,您需要在屏幕中创建NestedScrollView后获取它的高度

var nestedScrollViewHeight = 0
yourNestedScrollView.viewTreeObserver.addOnGlobalLayoutListener(
            object : ViewTreeObserver.OnGlobalLayoutListener {
                override fun onGlobalLayout() {
                    yourNestedScrollView.viewTreeObserver.removeOnGlobalLayoutListener(this)
                    nestedScrollViewHeight = yourNestedScrollView.height
                }
            }
        )

And then you can check whether this NestedScrollView height surpass your screen height, you can get your screen height using windowManager in Activity然后你可以检查这个NestedScrollView高度是否超过你的屏幕高度,你可以在 Activity 中使用windowManager获取你的屏幕高度

val screenHeight = windowManager.currentWindowMetrics.bounds.height()

So, after obtaining both screenHeight and nestedScrollViewHeight you can then substract to check whether nestedScrollViewHeight is larger than screenHeight因此,在获得screenHeightnestedScrollViewHeight您可以减去以检查nestedScrollViewHeight是否大于screenHeight

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM