Quantcast
Channel: Active questions tagged listview - Stack Overflow
Viewing all articles
Browse latest Browse all 619

Android - ListView smoothScrollToPositionFromTop(pos, 0) does not work for short list, circular or looping listView not working

$
0
0

I have a listView, and whenever the user clicks on any item, I that selected item to scroll automatically to the top position. So far, I've tried smoothScrollToPositionFromTop(pos, 0) and it does work. However, it only works when the listView items are many enough to actually enable the selected item to scroll upwards to the 0 position. However, when the listView items aren't that plenty, or when the selected item is near the bottom, the selected item can no longer scroll up to the 0/top position.

The first solution I thought of was to add blank rows to enable the selected item to go to the top position. However, that solution is not elegant and I think that it's bad coding practice.

The second solution I thought of was to perfectly implement a looping/closed/circular listView. What I mean is that if I have a list of N items, the following shall occur:

As I'm scrolling towards the end of the list or downwards, I should see the starting items:

......Item N - 2Item N - 1Item NItem 0 //start......

Similarly, as I'm scrolling from the start to the end of the list, or upwards, I should see the ending items:

......Item N - 1Item N //endingItem 0 //startItem 1Item 2......

and the same should be the case for when the user scrolls up from the default position, the last items should be shown.

I know that there are solutions for this already as shown in this question. However, I couldn't implement the answer because it "cheats" by placing a huge amount of items (around 2 Billion) and repopulating those items with the same data source over and over again. Also, as you can see in the getItem function, it returns a fake position on the Array.

The reason I couldn't use this solution is that I use a certain number of data objects. In the getView of my Adapter, I do the following:

int factor = listData.get(position).getUom().getFactor();

Where listData is my data object. The issue here is that I got an IndexOutOfBoundsException since I exceeded the original list count (319 for this case, sometimes less, sometimes more). However, since I still had items (around 2 billion), there were still rows below and when they looked for item number 319 on my listData arrayList, I got the out of bounds exception.

How can I cleanly and properly implement a closed/looping/circular listView? I'm surprised that there's no intuitive way on doing this like setting a class property or something.

I do not want to resort to adding blank rows at the end of my listView to make the bottom items scroll to the top position.


Viewing all articles
Browse latest Browse all 619

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>