简体   繁体   English

如果RecyclerView太短,则Android LinearLayoutManager scrollToPositionWithOffset无法正常工作

[英]android LinearLayoutManager scrollToPositionWithOffset not work if RecyclerView is too short

Problem description 问题描述

LinearLayoutManager.scrollToPositionWithOffset(pos, 0) works great if the sum of RecyclerView 's all children's height is big than screen height. 如果RecyclerView的所有子项的高度之和大于屏幕高度LinearLayoutManager.scrollToPositionWithOffset(pos, 0)效果很好。 But it does not work if the sum of RecyclerView 's all children's height is small than screen height. 但是,如果RecyclerView的所有子项的高度之和小于屏幕高度,那将不起作用。

Problem description in detail 问题详细描述

Let's say I have an Activity and a RecyclerView as it's root view. 假设我有一个Activity和一个RecyclerView作为其根视图。 RecyclerView 's width and height are both match_parent . RecyclerViewwidthheight均为match_parent This RecyclerView has 3 items and the sum of these 3 child view's height is small than screen height. RecyclerView有3个项目,这3个子视图的高度之和小于屏幕高度。 I want to hide first item when Activity is onCreated . 我想在ActivityonCreated时隐藏第一项。 User will see second item at start. 用户将在开始时看到第二项。 If user scroll down, he still can see first item. 如果用户向下滚动,他仍然可以看到第一项。 So I call LinearLayoutManager.scrollToPositionWithOffset(1, 0) . 所以我叫LinearLayoutManager.scrollToPositionWithOffset(1, 0) But it won't work since the sum of RecyclerView 's all children's height is small than screen height. 但是,由于RecyclerView的所有子项的高度之和小于屏幕高度,所以它不起作用。

Question

How can I make RecyclerView scroll to specific position even though the sum of RecyclerView 's all children's height is small than screen height. 即使RecyclerView的所有子项的高度之和小于屏幕高度,如何使RecyclerView滚动到特定位置。


Following is my code according to @Haran Sivaram's answer: 以下是根据@Haran Sivaram的回答我的代码:

Item first = new Item();
Item second = new Item();
Item third = new Item();
List<Item> list = Arrays.asList(first, second, three);

adapter.add(list);
adapter.notifyDataSetChanged();

recyclerView.post(new Runnable() {
            @Override
            public void run() {
                int sumHeight = 0;
                for (int i = 0; i < recyclerView.getChildCount(); i++) {
                    View view = recyclerView.getChildAt(i);
                    sumHeight += view.getHeight();
                }
                if (sumHeight < recyclerView.getHeight()) {
                    adapter.addItem(new VerticalSpaceViewModel(recyclerView.getHeight() - sumHeight + recyclerView.getChildAt(0).getHeight()));
                }
                linearLayoutManager.scrollToPositionWithOffset(1, 0);
            }
        });

It worked. 有效。 But has some small issues. 但是有一些小问题。

What you need to do is to increase the height of the recycler view to a minimum height which will allow scrolling and hide your first element (screen height + height of the first item) . 您需要做的是将回收器视图的minimum height增加到minimum height ,这将允许滚动并隐藏您的第一个元素(screen height + height of the first item) You can achieve this by adding a dummy element as the last element and setting it's height or you could also do this using padding/margins (Have not tried this). 您可以通过添加一个虚拟元素作为最后一个元素并设置其高度来实现此目的,或者也可以使用padding / margins来做到这一点(尚未尝试过)。

This also needs to be done dynamically once the view is drawn (You can do it statically if you are aware of the sizes of each item beforehand - I will not recommend this method though). 绘制视图后,还必须动态完成此操作(如果您事先知道每个项目的大小,则可以静态地进行操作-不过,我不建议您使用此方法)。

Use an onGLobalLayoutListner to get a callback once the view is drawn, do your measurements here and update the height. 绘制视图后,请使用onGLobalLayoutListner获取callback ,在此处进行测量并更新高度。 Now the scroll with offset should work fine. 现在,具有偏移量的滚动应该可以正常工作。

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

相关问题 RecyclerView 上 LinearLayoutManager 的 scrollToPositionWithOffset 不起作用 - scrollToPositionWithOffset from LinearLayoutManager on RecyclerView not working LinearLayoutManager#scrollToPositionWithOffset()有时无法正常工作 - LinearLayoutManager#scrollToPositionWithOffset() not working sometimes android recyclerview 中的 DividerItemDecoration 与 LinearLayoutManager - DividerItemDecoration vs LinearLayoutManager in android recyclerview 关于LinearLayoutManager.scrollToPositionWithOffset(int,int) - About LinearLayoutManager.scrollToPositionWithOffset(int,int) LinearLayoutManager在Android(Xamarin)的RecycleView中不起作用 - LinearLayoutManager is not work in RecycleView in Android (Xamarin) Android:自定义LinearLayoutManager不允许recyclerView滚动 - Android: Custom LinearLayoutManager not allowing recyclerView to scroll RecyclerView scrollToPositionWithOffset与动画 - RecyclerView scrollToPositionWithOffset with animation RecyclerView-Horizo​​ntal LinearLayoutManager创建/绑定方法的方式经常被调用 - RecyclerView - Horizontal LinearLayoutManager create / bind methods called way too often Kotlin/RecyclerView:scrollToPositionWithOffset 未显示 - Kotlin/RecyclerView: scrollToPositionWithOffset Not Showing Up android.support.v7.widget.RecyclerView null吗? 使用LinearLayoutManager - android.support.v7.widget.RecyclerView null ? with LinearLayoutManager
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM