Android:ScrollView中的RecyclerView [英] Android: RecyclerView in a ScrollView
问题描述
我正在尝试在ScrollView中使用RecyclerView.我的问题是,当我滚动RecyclerView时,它不是平滑"的:只要松开手指,滚动就会立即停止.
I'm trying to have a RecyclerView inside a ScrollView. My problem is that when i scroll the RecyclerView this is not "smooth": as soon as you release your finger the scrolling stops immediately.
我在片段中的布局是:
- 滚动视图
- LinearLayout
- TextView
- RecyclerView
- 按钮
- Scrollview
- LinearLayout
- TextView
- RecyclerView
- Button
我尝试禁用嵌套滚动:
mRecyclerView.setNestedScrollingEnabled(false)
并包装mRecyclerView的内容:
and wrapping content of mRecyclerView:
android:layout_height="wrap_content"
获得了我想要的平滑滚动,但是通过这种方式,RecyclerView的高度比它应该的要小(并且它不再滚动了,所以我看不到某些项目).
obtaining the smooth scrolling i wanted, but in this way the height of the RecyclerView is smaller than it should (and it doesn't scroll anymore, so i can't see some items).
我当前正在使用支持库23.4.0(我也尝试过23.2.1,同样的问题)
I'm currently using support library 23.4.0 (i tried also 23.2.1, same problems)
有帮助吗?
推荐答案
这个问题毁了我的夜晚. :)
This problem has wrecked my night. :)
当
RecyclerView
在第一个屏幕上(不滚动即可显示)时,先前的视图会影响其高度.当RecyclerView
最初完全不可见(需要向下滚动整个屏幕)时,其高度是正确的.When
RecyclerView
is on the first screen (visible without scrolling), preceding views impact its height. WhenRecyclerView
is initially fully invisible (requires scrolling the whole screen down), its height is correct.当
ScrollView
中的LinearLayout
测量其子级时,它将在模式UNSPECIFIED
下传递剩余多少像素作为MeasureSpec
.RecyclerView
将此规范传递给LayoutManager
,并且它遵循传递的高度(如果它不为null,甚至为UNSPECIFIED
).When
LinearLayout
insideScrollView
measures its children, it passes how many pixels left as aMeasureSpec
with modeUNSPECIFIED
.RecyclerView
passes this spec toLayoutManager
, and it obeys passed height, if it is not null, even it isUNSPECIFIED
.扩展
RecyclerView
并覆盖RecyclerView#onMeasure
,并且当高度测量规格为UNSPECIFIED
时,只需将零传递给super.onMeasure
.Extend
RecyclerView
and overrideRecyclerView#onMeasure
and, when height measure spec isUNSPECIFIED
, just pass zero tosuper.onMeasure
.Java:
@Override protected void onMeasure(int widthSpec, int heightSpec) { super.onMeasure( widthSpec, MeasureSpec.getMode(heightSpec) == MeasureSpec.UNSPECIFIED ? 0 : heightSpec ) }
科特琳:
override fun onMeasure(widthSpec: Int, heightSpec: Int) { super.onMeasure( widthSpec, if (MeasureSpec.getMode(heightSpec) == MeasureSpec.UNSPECIFIED) 0 else heightSpec ) }
这篇关于Android:ScrollView中的RecyclerView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- LinearLayout
- LinearLayout