scrollToPosition() "expand" RecyclerView - android

I have a RecyclerView with custom LayoutManager that handles the photo laying out so the result is different in portrait and landscape as you can see in the images below.
Now everything is working fine except for when I handle onConfigurationChanged() callback from portrait to landscape and vice-versa.
I want to save state of recyclerView so when user flips the device I will scroll the recyclerview to the same spot he was before rotating.
If you will notice the photos below, the landscape working fine but when I switch to portrait it does scroll to the last position the user where (notice the red car and it is rationally logical since this is what I wanted) but except for one problem, it looks as if the recyclerview is getting suddenly extra white space at the bottom.
I would like to avoid it and get the recyclerview to the bottom of its content (the red car is not the first item in the data set, I have plenty more before it) without actually taking the user to his last item if the position scrolled to is getting the recyclerview to "expand".
How can I calculate if the scrollToPosition() is "expanding" the recyclerview over its content height ? If I remember correctly it does not happen in LinearLayoutManager or GridLayoutManager.
I only need to know if the position that is laying out has enough space before actually scrolling to it and eventually scrolls to a minimum one that does not gives the white space at the bottom, but I don't know how.
Side note: If I scroll it up and then down again it does stay at the bottom without the extra white space.

Related

Scroll position of LazyColumn or LazyRow in pixel?

I want to detect scroll position of my LazyRow.
For example I scroll 2000px in a LazyRow and then navigate to another screen and come back. when I come back, LazyRow is scrolled 2000px and state is saved correctly. However I need that initial 2000px scroll position. how can I achieve that?
rememberLazyListState().firstVisibleItemScrollOffset
As shown above, LazyListSate only gives firstVisibleItemScrollOffset and not the whole scrolling position.
Update: I want this scroll position to react based on it. As in the image below, I have a LazyRow and an Image together. an example usecase is when user scrolls the list, I want to change image's alpha.

recyclerview Limit results to size of the screen, no vertical scrolling,

[Sliding left takes to next page of results and sliding right takes to prev page of results (of the same search), dashes at the bottom shows the page position please look into the png]
https://i.stack.imgur.com/fyxac.png
If you know the number of elements per screen, you can use LinearLayout and add the elements to it at runtime. You anyway won't be recycling views if there is no scroll.
Even if you don't know the number of elements per screen, the next best thing to would be to calculate the number of elements you can completely fit by dividing the left over screen size by the size of one element.
For horizontal pages, you can make use of ViewPager.

Android drag and drop, endless layout

I want a screen to arrange rectangles with drag & drop. Elements can be moved freely, so it's not a grid or list. I think I should use absolute positions.
My main problem is that I need an endless screen (when drag to right, scroll the screen to the right). One solution I found was to set width and height to 10000dp and a 2d scroll. Another way is to resize the container each time the user drags an element.
I need a better solution if possible. Any suggestions?
Resize the container each time the user drags an element out of the current view bounds should be the most performant idea.
By the way you should put some max size limit to the view, otherwise you will run out of RAM fast and your app will crash.

Problems using RecyclerView with StaggeredLayoutManager

I succesfully implemented RecyclerView using StaggeredLayoutManager with 2 col/spans. I have variable heighted items in each col, heights are not fixed.
Problem is, when I scroll down and up in RecyclerView, sometimes a gap is occured at the top item of one of the rows. I added a video describing my problem.
I simply added a StaggeredLayout with span value 2, and adapted my items with an adapter.
+-+-+-+-+-+-+-+-+-+-+-+-+ <------ Top of RecyclerView
--------------                    <- Top item in left column
                   ------------- <- Top item in right column, has unexpected gap/offset/margin at top.
Gap sometimes occurs. Sometimes, everything is well aligned and no problems.
Here is the video I take from my application.
https://youtu.be/gmB7h73YR3M
Look at the topmost 2 pictures(LinearLayouts) in the list, cup and monitor. Their top position differs after each scroll down and then up event, and an unwanted grey area (Backgroundcolor of RecyclerView) appears at there.
Between 0-3 sec, there is a gap on top of cup, left column.
Between 7-8, there is a gap but little than 0-3
Between 10-12, everything is fine, their tops are at same level.
Between 19-21, this time right column has a gap on top.
Any reasons why I have this problem, any solutions?
EDIT:
Because I don't want so much moving items, I set gapping strategy as GAP_HANDLING_NONE. However, when I set strategy as GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS, toppest line of two items get aligned when Scrolling Up ends at top. My observation is, when scrolling to the elements at top, a last calculation is made to Align top lines of 2 items if strategy is GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS, but not in GAP_HANDLING_NONE. I can see it is aligned with my eyes, but it doesn't seem to happen when strategy is GAP_HANDLING_NONE.

Can I pin an item inside RecyclerView to the centre while scaling

I have a list of items in a RecyclerView, which is scalable when pinching, and it currently scales the items fine, with the first list item staying when it is (the far left one).
What I'm looking to do is to keep the centre item pinned in the centre while I scale. I'm hoping that RecyclerView makes this easy for me.
If item is focused, LinearLayoutManager will preserve its position when layout occurs. (L-preview does not do this but L release will have it). This may not be enough for you if you want to keep item's center stable because LLM will keep the items reference point stable. (e.g. reference point is the top pixel of the item if layout is vertical and reverseLayout is false).
As a workaround, you can call LLM#scrollToPositionWithOffset with proper values each time you resize the View.

Categories

Resources