简体   繁体   中英

Android how to make animated setSelection() for listfragment?

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.

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).

我不认为你可以为API级别7执行此操作,但看看你是否不能通过这样的顺利滚动:

listFragment.getListView().smoothScrollToPosition(0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM