With the latest update to RecyclerView, it can now wrap_content within a NestedScrollView. However, there is some behavior that doesn't sit well with me, and I'm looking for answers.
Problems 1: The wrapping does not account for empty spaces. This means if there are empty spaces after the RecyclerView content, the dragging animation will clip the item mid screen as depicted here:
UPDATE: Resolved problem 1. I just needed to add the layout_weight="1" attribute to the RecyclerView.
Problem 2: Similar to problem one, when deleting an item, the RecyclerView will wrap immediately at the start of the animation - causing the animation to clip as depicted here:
UPDATE: Problem 2 is still not resolved, but it is not as noticeable as before. When item is removed, the item now clips to the view port, making it look like the screen jumps to the new height with the animation pulling up from below the view port.
Problem 3: When adding a new item, I want to scroll to the end of the ScrollView. However, upon running the following code in my add event, the scrolling immediately jumps to the current end of the scrollview. Is there a callback for "after the item has been inserted"?
public void addItem(T item) {
items.add(item);
notifyItemInserted(items.size() - 1);
scroll.fullScroll(View.FOCUS_DOWN)
}
UPDATE: Problem 3 still not resolved but I found a workaround that reverse the order like this:
layoutManager.setReverseLayout(true);
layoutManager.setStackFromEnd(true);
And then the below code when adding new item. This way, the scrolling always work regardless of the height.
nestedScrollView.fullScroll(View.FOCUS_UP);
The article quoted:
Note that although RecyclerView animates its children, it does not animate its own bounds changes. If you would like to animate the RecyclerView bounds as they change, you can use the Transition APIs.
Well I don't need to animate the bounds, but I do want it to snap and fill the the view port (I can't use match_parent in this case as I have some static views above the recycler view). Thus solving problem 1.
I want to do somethings prior to the bounds being changed. Example: on remove item animate the removal, then apply the bound change. Thus solving problem 2.
Conversely, I want to do some things when the bounds have already changed. Example: on add a new item, apply the new bound first, then start the scroll animation. Thus solving problem 3.
Any idea how I can achieve this?
I've found a better solution for what I wanted to do. Much simpler than trying to figure out how the new LinearLayoutManager wraps the content.
Instead of having this:
<NestScrollView>
<LinearLayout>
<View></View>
<View></View>
<RecyclerView></RecyclerView>
</LinearLayout>
</NestScrollView>
I went with just the recycler alone and manage the header within the RecyclerView itself:
<FrameLayout>
<RecyclerView></RecyclerView>
</FrameLayout>
It's so much easier to manage now as I can put any view above or below the items.
Using this solution here: Link