Android: Recyclerview In A Scrollview
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 stop
Solution 1:
This problem has wrecked my night. :)
Observations
When RecyclerView
is on the first screen (visible without scrolling), preceding views impact its height. When RecyclerView
is initially fully invisible (requires scrolling the whole screen down), its height is correct.
Reason
When LinearLayout
inside ScrollView
measures its children, it passes how many pixels left as a MeasureSpec
with mode UNSPECIFIED
.
RecyclerView
passes this spec to LayoutManager
, and it obeys passed height, if it is not null, even it is UNSPECIFIED
.
Solution
Extend RecyclerView
and override RecyclerView#onMeasure
and, when height measure spec is UNSPECIFIED
, just pass zero to super.onMeasure
.
Java:
@OverrideprotectedvoidonMeasure(int widthSpec, int heightSpec) {
super.onMeasure(
widthSpec,
MeasureSpec.getMode(heightSpec) == MeasureSpec.UNSPECIFIED ? 0 : heightSpec
)
}
Kotlin:
overridefunonMeasure(widthSpec: Int, heightSpec: Int) {
super.onMeasure(
widthSpec,
if (MeasureSpec.getMode(heightSpec) == MeasureSpec.UNSPECIFIED) 0else heightSpec
)
}
Post a Comment for "Android: Recyclerview In A Scrollview"