Android ListView resets scroll position after first scroll - android

I have a list view in an Android (JB) app where clicking on a row replaces the list view with another view.
Pressing separate button, adds the list view back to the activity (the idea is that the parent container toggles between the list view and the contents of a single row).
The problem happens when I scroll to a row near the bottom and press it (to replace the list view). After hitting the other button to show the list view again, everything looks fine (including the row states and the scroll position). But as soon as I try to scroll again, it resets the list view to the top. Scrolling after that works normally.
The data source for the list view doesn't change at all. Weirdly, if I change the orientation of the tablet, then scrolling for the first time doesn't reset the list view to the top.
I'm guessing it has something to do with removing the view and then adding back that same view (which is done by calling removeAllViews and addView on the parent).

I'm not sure but I think when you call removeAllViews for some reason it can't save the previous state.
I'm not sure again if it is the best way to solve the problem but what you can do is to save your scroll position before remove it, and after add your views again you can set it.
Something like this:
int posX = listView.getScrollX();
//remove your view
and after:
//add your views
listView.setScrollX(posX);

Related

Talkback doesn't navigate to the view above recycler view

I am using Recycler view for showing list items. I have made the first view item 'GONE' and set its height to 0. Currently focus is on 2nd item (technically its a 1st item, since the actual 1st item is not there). What I observed is, if I swipe left, the talkback doesn't navigate to the item above recycler view. It gets stuck there.
Note: I have not made any attribute changes, everything is set as default.

RecyclerView LayoutManager - force to keep a view even if not visible

I am animating views between two RecyclerView. The first one is something like a list of folders showing the first item as cover, clicking it opens a new view showing the folders content animating the cover to the first item. Clicking back animates all visible views back to the folder where they came from (the cover being the top most view). This looks great as long as the opened folder shows the first item. If I scroll down the first item will be offscreen and the back animation does not look that good anymore because the cover view is not animated (I'm only animating all visible views currently).
What I think would work is following: the LayoutManager could position the first item at a position shortly offscreen and keeps it as a special view in it's pool so that u always can access the first view and when I animate back to the folder view I can animate the cover in addition to all other currently visible items ( the cover will be animated from top of the screen).
This means I need following:
the LayoutManager must handle the first item as a special one that is not recycled (I may need it any time for the back animation)
the first item must always be layed out (either at the default position in the list, if it is visible or offscreen directly above the screen), again because I may need it at any time for the back animation
Can someone help me where to start here? I think this is possible with extending the LayoutManager but I don't know where to start...
Have you tried the following?
recView.getRecycledViewPool().setMaxRecycledViews(TYPE_XXXX, 0);

ListView drag up the list view

I have a listview as the main view in a fragment. I also have 2 FloatingActionButtons used for editing, one at the left and the other on the right. Sometimes, the FloatingActionButton hide part of the last view of the listview. I want the user to be able to "drag up" the listview to make the last view more visible. When the user releases it, it should go back to its position.
Listview has an option for this called
listView.setOverscrollFooter()
You just pick a drawable into it like loading etc. or just a blank drawable would do your work.

Listview with one sticky header for second row

My layout is a bit complex.
I have a SwipeRefreshLayout in which I host a ListView. Whenever the user drags the Listview's top, the SwipeRefreshLayout performs a refresh. I also listen for the last visible item of the ListView to load next page of records (Endless scroll)
In the list's adaptor I have 2 views that I am using. The first one will only be visible in first row, the other view will remain the same for all other rows.
What I want to achieve:
On top of the row with position = 1 I want to have a sticky header. This means that when I scroll Up, the header will scroll to the top of the screen and will remain in there.
This sticky header will only be at one row
if possible I'd like to use a simple implementation as my layouts and adapters are already complex enough.
Waiting for your suggestions.
I didnt quite get your question the first time, heres the answer attempt round 2.
In your layout add an empty viewgroup (whichever you prefer, though linearlayout seems to work just great), add a scrollListener to your listView and check the position of your sticky view. If its top anchor is below (meaning its visible in the listview) the top of the screen you set the viewgroup visibility to gone, if the top anchor is either touching the top of the screen or below it, you add that view or one just like it to the viewgroup and set its visibility to visible.
You can adjust the position 2 view visibility accordingly to allow for this change to appear seamless. Can help you a bit more once you have some code and are on your way with this change.

Button in ListView malfunctioning

I have a button in each item of a ListView whose background is defined by an XML, one background when enabled and another when disabled. When the ListView loads, it comes out correct. But, for some reason I can't figure out, if I scroll down and then scroll back up, the wrong background shows up.
I'd like to know the solution to this problem, but besides that, in general what I want to accomplish is this:
I have a button in the ListView to take the user to the website for the given item. If there is no website, I want the button to disappear, or be disabled. I seem to have the same problem with both options.
Thanks in advance for your efforts
It seems most likely that the problem lies with your getView() method. Android recycles views to save memory, so, for example, when you scroll down, it calls getView(int, View, ViewGroup) on your adapter where View is the item that just left the top of the screen. If you're not re-populating the item with the new data from the adapter, (ie, just returning convertView) it will put the View that left the top of the screen where the "new" one should be.

Categories

Resources