android:stackFromBottom="true" does not seem to work perfectly (reverse ListView with Adapter)
Asked Answered
B

5

5

Ok let me explain what "does not work perfectly" means.

I have a listview without the android:stackFromBottom="true" parameter. I also have an ArrayAdapter with the regular getItem(position). I call my webserver, get the data on an asc date order.

This way the item in position 0 (top) of the list has the smallest date. And I can scroll top->bottom. Good.

I added android:stackFromBottom and now I see that now you can start scolling from bottom->top but the item in position 0 is still the top item and has the smallest date. So the adapter has not changed its order.

The only way to solve this is to change getItem(getCount()-1-position) inside the adapter. However I add and notify the adapter and the adapter will still add the items to the bottom.

I guess that the structure is ListView -> Adapter -> Row Views so no matter the stackFromBottom value, there is only one child, the adapter. Right?

Anyhow, how can have a ListView with Adapter in reverse order?

Blackandblue answered 28/3, 2014 at 16:27 Comment(0)
B
5

A nice elegant way to do this is to override your Adapter.getItem() method to return the items themselves in reverse order.

This is a good pattern for an Adapter as it does not require your data to be sorted differently just for display purposes (there could be overhead in sorting the list, or some other reason why the original order is important in another part of the app).

Instead it is the adapter that displays the change without affecting the underlying data set.

Here is what I used:

@Override
public MyListItemClass getItem(int position)
{
    return super.getItem(getCount() - 1 - position);
}

This is based on @mes's answer to this related question:

Have a look at that thread for some discussion about other possible solutions.

Brigand answered 18/6, 2014 at 16:13 Comment(0)
B
3

I overestimated android:stackFromBottom power. What it basically does is a similar to "gravity : bottom". It does not add items in reverse order but it just makes the content aligned to the bottom of the listview.

I'm still searching for a reverse adapter solution.

Blackandblue answered 28/3, 2014 at 17:4 Comment(1)
It can be easily implemented by overriding getItem method.Dayak
T
0

Maybe you should use it with AbsListView.setTranscriptMode(int) I have a chat app and I offer two modes of displaying conversation - from bottom or from top by using AbsListView.setStackFromBottom(boolean)

Tsan answered 26/12, 2015 at 23:53 Comment(0)
S
0
 LinearLayoutManager mLinearLayoutManager = new LinearLayoutManager(Application.get());
            mLinearLayoutManager.setStackFromEnd(true);
            mLinearLayoutManager.setReverseLayout(true);
list.setLayoutManager(mLinearLayoutManager);

Try this hope this will help you.

Swenson answered 29/6, 2016 at 11:24 Comment(0)
R
0

I would suggest to use comparator to return the object based on date in which ever order you want. Doing it will make sure that the data is always in the order you want and not what server is sending. You can check how to use sort here

Risser answered 29/6, 2016 at 11:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.