J. Hegg
J. Hegg

Reputation: 2133

Scroll to child Recyclerview item in Nested Recyclerview

Context

I have a nested Recyclerview like the following sketch shows you:

Sketch of my nested Recyclerview

In the drawing you can see a RecyclerView, which in turn holds one RecyclerView each as item.

Question

My question now is: How can I get the outer, parent Recyclerview scroll to a position of one of the child RecyclerViews. In the drawing I marked you Content pos 5 as my target position for example the Child RecyclerView 2.

What I already found out

One approach I've been thinking about was using scrollToPositionWithOffset(position, offset) on the parent RecyclerView, then just scroll to the position of the child RecyclerView 2 and then have the offset as the size of the list of items in the Child RecyclerView. But how can I get the size when the view isn't even inflated?

I would be very happy if one of you could give me some help that also ran into this problem of scrolling to a position of the child RecyclerView in such a nested Recyclerview.

Upvotes: 1

Views: 356

Answers (1)

tao shi
tao shi

Reputation: 1

I have same issue, i caculate the total scrolling length including the section header (Fixed height) and child recycleview's item, you my ask the question of "how can I get the size when the view isn't even inflated or draw?". you can use view.measure() to get cotent item's measured height:

int widthMeasureMode = View.MeasureSpec.makeMeasureSpec(getResources().getDisplayMetrics().widthPixels, View.MeasureSpec.EXACTLY);
int heightMeasureMode = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
contentView.measure(widthMeasureMode, heightMeasureMode);
int measureHeight = contentView.getMeasureHeight();

the need to scroll distantce: account all the single item's measured height, and plus all the section header's height.

for example, you need to scorll to the second section, and make this section's second child content view scroll to the screen top, suppose the first section has 6 content view, the total scroll distance is : 2 section's height plus 7 item content' height.

In fact, you could get and save all the child's absolute Y position on the screen Coordinate. so you can scroll to any child content in nested recycleview.

recycleView.scrollBy(0, needToScrollDistance);

Upvotes: 0

Related Questions