通常情况下,在使用ScrollView,ListView的时候都会取消scrollbar,然而当数据量特别大的时候,滑动可能又比较费劲。这时候就会需要一个滚动条来帮助我们。
使用时也进行了搜索,这里也简单记录下自定义的滚动条。以便后续使用
效果图
代码
需要在布局文件或者代码中为ListView或者ScrollView添加
android:fastScrollEnabled=”true”
或者
mListView.setFastScrollEnabled(true);
<ListView
android:id="@+id/lv_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dp"
android:fastScrollEnabled="true"></ListView>
添加完这一行代码后就有快速滚动条了。当然有很多人会感觉自带效果不好看,或者需要自定义样式
在styles.xml里自定义一个主题。添加到所在Activity即可
<style name="FastScrollTheme" parent="AppTheme">
<item name="android:fastScrollThumbDrawable">@drawable/bg_fast_scroll_bar_thumb</item>
<item name="android:fastScrollTrackDrawable">@drawable/bg_fast_scroll_bar_track</item>
</style>
AndroidMainfest.xml中
<activity android:name=".MainActivity"
android:theme="@style/FastScrollTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
自定义的样式
//bg_fast_scroll_bar_thumb 指的是短条
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/red" />
<size
android:width="15dp"
android:height="30dp" />
</shape>
//bg_fast_scroll_bar_track 指的是长条
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/transparent" />
<size
android:width="5dp"
android:height="10dp" />
</shape>