Home > Mobile >  How to add and remove views in a ScrollView without changing the current position of the view being
How to add and remove views in a ScrollView without changing the current position of the view being

Time:02-14

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).

CodePudding user response:

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.

CodePudding user response:

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.

  • Related