RecyclerView detaches view still visible - android

Current scenario
I'm using this library https://github.com/kanytu/android-parallax-recyclerview to achieve a parallax effect on a RecyclerView. So far, so good. However I would like get an effect like Google Newsstand where the header is still bellow the cards.
Problem
The problem is that RecyclerView (or LinerLayoutManager) detaches the header view the moment the first element of the list touches the top of the parent view:
As you can see the moment the first cardview touches the top is the moment RecyclerView detaches the header.
I'm sure that there is no problem on the logic itself I get RecyclerView.findViewHolderForPosition(0) == null when the card reaches the top. Proving that the header is recycled.
Tried Solutions
I tried many things like:
ViewHolder.setIsRecyclable using this method to set the holder to not recycler doesn't do any effect.
LayoutManager.ignoreView I tried marking the view to be ignored from being discarded and recycling. The result was an exception saying:
Trying to recycle an ignored view holder. You should first call stopIgnoringView(view) before calling recycle.
setItemViewCacheSize Doesn't do anything. Tried calling setItemViewCacheSize(50) and it doesn't do anything to the header.
setMaxRecycledViews Tried setting the max of recycled views with viewType=HEADER to 0 and it still recycles it.
Conclusion
So I question if there is anyway to mark the item for not getting detached until I order so (like checking if it's still visible and then detach it).
There is also an issue on the github about it: https://github.com/kanytu/android-parallax-recyclerview/issues/7

You can use the new design support library's CoordinatorLayout with your recyclerview and make the same effect with no issues like this.
check this link: http://android-developers.blogspot.com/2015/05/android-design-support-library.html

Related

How to add and remove views in a ScrollView without changing the current position of the view being shown?

I have a LinearLayout inside a ScrollView and I want to create the ilusion of endless scrolling by removing the views that are not visible and putting them as the next elements in the list (basically i do a linearlayout.removeView(viewOutOfScreen) and then linearlayout.addView(viewOutOfScreen). The problem is, every time this happens the view that is currently visible ends up in the top of the ScrollView. I tried doing a scrollView.scrollTo(0, -viewOutOfScreen.contentHeight) and it kinda works but the transition generates an horrible tearing artifact.
Also I'm not able to use RecyclerView given that the views are WebViews with very specific settings and executing js, so i nedd to reutilize/reposition them manually (cant't create more than i initially have).
I recommend using a nested RecyclerView because, it does exactly what you are trying to achieve manually. It recycles views which are out of the screen.
Also I'm not able to use RecyclerView given that the views are WebViews with very specific settings and executing js, so i nedd to reutilize/reposition them manually (cant't create more than i initially have).
So what if they're WebViews? You could just manually cache the views you want to display (ignoring the "recycling" part of RecyclerView) and return the View you want for each index instead of letting the RecyclerView delete and recreate them.

How to include an unattached View in the activity transition?

I draw a View that is not attached to any parent.
It's a decoration for a RecyclerView. The view sticks to the bottom and disappears when its counter part comes up in the list.
All this works fine but:
When i leave the activity the View doesn't fade with the rest of the
views in the activity's transition.
It stays until the end of the animation and then disappears
immediately.
( see large green view in the demo )
How do i include this unattached View in the activity's exit transition?
I've create a minimal Android Studio Project to replicate the issue:
https://github.com/Ostkontentitan/transition-issue-demo
(To better see the issue possibly set your phones animation scale to >= 5)
Here is a demo:
Add transitionName to xml layout for the RecyclerView.
The transition animation you see is because of ActivityOptions.makeSceneTransitionAnimation(this#ItemListActivity) and if you add transitionName to the view, it works fine.
Not-too-educated guess
(because I haven't tried and I haven't used Transition Framework in a few months)
The way TF (Transition Framework) works is by computing the start/end values of the transition and performing the animations needed to achieve this.
RecyclerView decorations are not "views" that are part of the layout, so TF has no idea that thing exists. It does know about your RecyclerView of course, because it's contained in the ViewGroup that is animated.
You may have already know this, but in any case, what I think I'd try to do here, is create a custom transition framework transition (they are not hard to do, you can even check TransitionEverywhere and look at how that library implements some missing transitions in the framework); in your CustomTransition, you can then try to interpolate the animation values so the recycler view can redecorate as the animation progresses (like an alpha value that is animated, your custom decorator would "paint" using said alpha).
Now... in truth, I've had to do something similar once, where a custom transition was "driving" a few external views (for reasons at the time) :) but... it was not a RecyclerView item decoration, mine were just "views", so I'm not sure if you can do this this way w/a decoration.
I think it's worth trying.

Why RecyclerView items disappear immediately when using Transition API?

This is a question regarding the use of Android Transition API.
I am trying to animate the height change of a list, just like a dropdown menu.
I tried 2 approaches
Use a RecyclerView and animates its height change
Use a ScrollView > LinearLayout hierarchy and animates ScrollView's height.
The 2nd approach works perfectly.
But the 1st approach has a serious glitch - when the collapse transition starts, items disappear immediately.
By looking at the below GIF you can observe clearly the difference:
To be exact, items' visibility changes at the moment I change RecyclerView's LayoutParams, without waiting for the transition to finish, whatever it is expanding or collapsing
Code
I have created a minimal project on Github.
If you just want to look at the code, here is the MainActivity.
Question
Is it possible to achieve ScrollView's effect with a RecyclerView?
If yes, how?
My Idea is to do the transition of all the recycler view rows individual rather than the whole RecyclerView:
So when collapsing iterate through each ROW of a RecyclerView and do a transition. Remember to check for null if some rows are recycled they may return null. So after that collapse the whole recyclerView.
And like wise for the expanding do the same for the views.
This issue is cause by RecyclerView has many views with it but Scroll View has only one View nested in it.

RecyclerView remove animation bug

I have implemented a RecyclerView where I can add and delete items. I want the added item to be added on the second last position and, whenever I add a new item, the animation runs well. That is, the last item moves downwards, letting space for the new item to fade in.
When I remove an item there is a problem that I don't know how to fix. How I want it to behave is:
fade out the deleted element,
move upwards all the items below it.
What actually happens is that, first thing, the last item disappears, and then the rest of the animation takes place. When the items below the deleted element move upwards, the last item reappears as coming from behind a wall.
To me it seems as if the RecyclerView shrinks to the "post-animation" height, and then the animation is performed.
I haven't defined the ItemAnimator, so the DefaultItemAnimator must be the one used. I have watched this video, and overridden the supportsPredictiveItemAnimations method in a custom implementation of LinearLayoutManager, but it doesn't fix it.
I already reported the problem through Google Issue Tracker here
I hope we can get a fix soon! As you say It seems very related to a possible race condition between the measure updates for the recyclerview and the animation when your recyclerview is wrapping it's content to calculate it's height.
This article explains the problem in a really detailed way also.
Perhaps a bit late, but I was able to fix this in my situation by setting the RecyclerView item animator to null, and then in the setList(list) function of my Adapter scheduling a Transition as such:
Transition transition = new AutoTransition();
transition.setDuration(200); // Or any duration you want
TransitionManager.beginDelayedTransition(mRootViewGroup, transition);
where mRootViewGroup is the viewgroup containing the RecyclerView.
This problem is also fixed by setting your layout_height (or width, depending on the scrolling orientation) to something other than wrap_content, but if, like me, you need your recyclerview height set to wrap_content, this might be a solution for you.
Not certain it will fix your problem as well, but I figured I might as well share what worked for me.

RecyclerView with LinearLayoutManager with header view that doesn't recycle

I want a ListView style RecyclerView with a header on top, AND I don't want the header view to get recycled, EVER.
I'm totally new to RecyclerView and LinearLayoutManager but I think what I want shouldn't be too difficult by extending LinearLayoutManager.
Additional info in case needed, but I think the above is enough:
I already have a RecyclerView with a header view at position 0. That part was easy enough with help from SO answers. But the view gets recycled, which is causing lots of issues with the EditText views in the header (trying to keep focus, restore focus, keep the caret visible, not do weird things with auto-correct feature, keep typing while EditText is technically gone/recycled, etc). So the simple solution would be to not have that view recycled, which I think is possible with a custom layout manager, but I have no idea where to begin.
I can't simply place the header view above my list, because I want it to scroll with the list (it's too tall to be fixed at the top of screen all the time).
LayoutManager has LayoutManager#ignoreView(View) interface which according to documentation,
Flags a view so that it will not be scrapped or recycled.
Looks like what you need here
EDIT Google devs commented (here) that this flag is serving some other purpose, so it is not gonna solve "RecyclerView Header" problem. However, they are working on solution, so hold on tight and stay tuned

Categories

Resources