Dynamically adding items to top of listview - android

Is it possible to insert a ListView item to the top of the list, with the position of the most recent item going at the top? I am aware of other methods of achieving this, such as items.add(0, item);, getItem(getCount() - position - 1); as well as using listView.smoothScrollToPosition(0); within a runnble(), but the results are as shows in this image, where the numbers represent an int position index. New items added to the list are placed at the bottom, pushing all previous items upward.
I would like a result like this?
Thanks in advance.

I solved my own problem! Basically I used the method which involves placing each item in the list at index 0, items.add(0, layer); which visually, does indeed stacks the elements in my list, but at the cost of reversing the position index. All I needed to do was modify the recieved int position index so that it corresponds to the positions of the reversed list.

You can't normally do this with listview. Try bottom sheet which is explained here.

Related

RecycleView: how to go to a row without scrolling

If the recyclerview has a list of items and I want to go to a specific row immediately when user enters the UI, so I don't want the user to see it scroll to reach that row. Is it achievable?
Thanks.
You can use method scrollToPositionWithOffset (int position, int offset)
Scroll to the specified adapter position with the given offset from
resolved layout start.
See documentation
Example:
//Scroll to item position 2 with offset 0
RECYCLERVIEW_LAYOUT_MANAGER.scrollToPositionWithOffset(2, 0);
Hope this will help~
I would say it is impossible.
RecyclerView#scrollToPosition causes sort of animation because it does not know the height of all rows initially. Not all items are loaded immediatly but one by one as you scroll down the list.
I use ListView and I solved the issue by calling setSelection(position)

Get position of WearableListView

I need your help!
I have three WearableListViews side by side, filled with the values from 0-9.
The idea is that you can scroll the three different lists to create fast and simple a number between 000 and 999.
Now I need the selected values, but no method I tried get me the right values.
There were:
getScrollX(),
getScrollY(),
getScrollState(),
...
If you perform a click on an item within the list, you can get the position with viewHolder.getPosition(); in the onClick-method.
Does anybody know, how to get the current position scrolled to of all three WearableListView simultaneously?
Look at the answer in How can I change a Preference when WearableListView snaps to new list item?
basically add a WearableListView.OnScrollListener to your WearableListViews and implement
onCentralPositionChanged(int centralPosition);
this will do the trick.

Keep the scroll position of a list while adding item to the top in Android

Here and in some other places I found a proposed solution for the problem of keeping the scroll position while adding items to the top of a list.
I tested it and the result is that the list scrolls and then the original position is restored but the user can see the list jumping.
Another problem with this approach is that if I add the items very quickly it does not work. The reason is that the new cursor can have more than one new item because some previous cursors did not manage to be used.
Is there a better solution?
Can try this
listPos = getListView().getFirstVisiblePosition();
int newPos = listPos + (customAdapter.getCount() - oldListSize);
getListView().setSelectionFromTop(newPos, 0)

Android ListView with CursorAdapter has Incorrect Scroll Position after Requery is called when new items are added

Here's the scenario: I've got a simple ListView displaying a twitter feed. I have a CursorAdapter which is fetching tweets from sqlite. When I call requery() on my cursor, I expect that new tweets will be fetched that have since been added to the database. This all works fine, and the new items are even visible in the ListView after this happens.
The Problem is the scroll position seems to be saved based on the item position offset. So let's say the first visible position in my ListView is 4. When I requery and 2 new items are added to the top of the list, the ListView keeps the first visible item scroll position as 4, however since there's two new items in the list, I now see a different item at position 4 than before I refreshed. This image illustrates the before and after:
Notice how Before, Tweet D is the first visible item and afterwards, Tweet B now becomes the first visible item.
My Question is, how do I keep the same scroll position based on cursor positon when calling requery, so that in this example, Tweet D would still be the first visible item after a requery?
I had a similar issue in one of my projects. Here's how I solved it. Although it's not exactly the same case. Hope it helps.
Before you update the list, save the scroll position this way -
int position = getListView().getFirstVisiblePosition();
View v = getListView().getChildAt(0);
int top = v == null ? 0 : v.getTop();
And then after you update the list, you will know how many items you've added. So you can call
yourlistview.setSelectionFromTop(position + <number of items added>, top);
My suggestion would be to keep track of the number of items in the list. Once you know that, as part of the update you can add the difference to between the old count and the new count.
Psuedo Code
on_update
offset+= (list_view.length() - list_size)
list_size = list_view.length()

How can I "set View" in right place where I remove an item in ListView (Android)

I choose an item to remove from my ListView. And after the Item was removed, my ListView was scrolled back and display at the first Item.
I want my ListView display in right place where the Item I had removed (It like remove a contact in Android Contact list). How can I do that?
I suppose you want to update your ListView display, after you remove an item, right?
When you remove the item, you have to modify your data adapter, and let the ListView change accordingly (something like the MVC pattern behavior). I've seen somewhat similar questions (and the corresponding answers :) ) here and here.
Edit:
Aha, in that case, try using setSelection(int), after recalculating new indices and new item count.

Categories

Resources