Question

How is it possible to make a smooth setSelection(position) for listFragment. For regular listviews it is possible to call smoothScrollToPosition(position) but this only works for api lvl 8 and above but this doesn't matter because it doesn't work for listfragment and not down to api lvl 7.

Any ideas, suggestions that will help implement this is greatly appreciated.

Was it helpful?

Solution

Mark D is correct that, if you want to smooth scroll the ListView in a ListFragment, you need to call getListView() on the ListFragment and then call smoothScrollToPosition(int) on the ListView that is returned. Of course, this only works down to API level 8 because that is when smoothScrollToPosition(int) was introduced to AbsListView, the superclass of ListView.

I suppose you could look at the code in AbsListView.java to see what it does and try to replicate it in your own subclass of AbsListView or ListView. It's immediately clear that smoothScrollToPosition(pos) just calls start(pos) on an instance of an inner class PositionScroller, but it looks somewhat complicated to replicate that behavior in your own subclass since the PositionScroller gets called from a several other spots like onDetachedFromWindow(), public void onWindowFocusChanged(boolean hasWindowFocus). It's not at all clear to me how you would cleanly integrate your PositionScroller behavior into your subclass based on the API 7 version of AbsListView.

If it were my decision, with API 7 and earlier making up less than 8% of devices accessing the Android Market in the latest data set, I'd just punt and do something simpler like setSelectionFromTop whenever the API is lower than 8 (detected by Build.VERSION.SDK_INT).

OTHER TIPS

I don't think you can do this for API level 7, but see if you can't smooth scroll by going like this:

listFragment.getListView().smoothScrollToPosition(0);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top