开发者

Autohide scrollbars when not scrolling in a ListView

In the new official Twitter app, the scrollbars in all the ListViews the app uses are hidden unless the user is scrolling through the list.

When you start scrolling, the scrollbars appear. When you stop, they fade out with an animation until they are gone completely.

I can't seem to find anything in th开发者_开发问答e documentation that indicates this as being a standard feature.

Is this something included in the API? If not, anyone know how this might be done?


Confirmed : either use android:fadeScrollbars ( if you're API level 5 ) or try to use setOnScrollListener to check scroll status and hide/show the bars . Some code examples are in this thread: how to detect Android ListView Scrolling stopped?


You can enable scroll bar fading for your entire app on API level 5 and newer via a custom theme and the fadeScrollbars style attribute by adding this to styles.xml:

 <style name="Theme.App" parent="android:Theme.Light">
    <item name="android:fadeScrollbars">true</item>
 </style>

Then set the new theme for your application in AndroidManifest.xml:

<application android:icon="@drawable/app_icon" 
             android:label="@string/app_name"
             android:description="@string/description" 
             android:theme="@style/Theme.App"> 

Just be sure you're not overriding this global theme on individual activities. Earlier Android versions will safely ignore this unknown XML attribute and not fade the scrollbars.


I haven't used them yet, but you might play around with android:scrollbarDefaultDelayBeforeFade and android:scrollbarFadeDuration, available on all widgets (i.e., subclasses of View).


I followed Alex's answer and it worked using both the theme settings and through code.

GridView gridview = (GridView) findViewById(R.id.mygridView);
gridview.setScrollbarFadingEnabled(false);

I did encounter a problem however with the Gallery Component. Whilst the following will compile fine, it will throw a NullPointerException. I assume this is to do with a Gallery not having scrollbars to show/hide.

Gallery gallery = (Gallery) findViewById(R.id.myGallery);
gallery.setScrollbarFadingEnabled(false); // <-- this will throw an exception

Android 2.2

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜