Android ListView fastScrollThumbDrawable not working

只谈情不闲聊 提交于 2019-12-10 11:05:34

问题


i am doing an app where i want the fastscroll tab on my listview to be a custom thumb tab. reading the docs online i thought this would do it:

<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
android:cacheColorHint="@color/myWhite"
android:scrollbars="vertical"
android:scrollbarThumbVertical="@drawable/scrollthumb"
android:scrollbarSize="12dip"
android:fastScrollThumbDrawable="@drawable/scrollbar_thumb"
android:fastScrollEnabled="true"
/>

The list view is fine, the custom scrollbar colours are working and the fastscrollbar tab displays, but it is using the default thumb image and not the png file scrollbar_thumb.

does the thumb image need to be in a certain format or size ? can it be changed to a custom graphic, if not can the colour of the thumb be changed at least ?

any help will be much appreciated


回答1:


In the ListView XML definition, add

android:fastScrollEnabled="true"

or in code

listView.setFastScrollEnabled(true);

Create file fastscroll_thumb.xml in the res/drawable folder as follows:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/fastscroll_pressed" />
    <item android:drawable="@drawable/fastscroll" />
</selector>

In AndroidManifest.xml, set a custom theme for your application:

<application
    android:theme="@style/ApplicationTheme"
    ...>

Create a values folder in the res folder. Create themes.xml files in res/values as follows:

<resources>
    <style name="ApplicationTheme">
        <item name="android:fastScrollThumbDrawable">@drawable/fastscroll_thumb</item>
    </style>
</resources>

Lastly make sure that fastscroll.png and fastscroll_pressed.png exist in your drawable folder




回答2:


Note that the android:fastScrollThumbDrawable attribute only applies for Android API Level 11 and later.

http://developer.android.com/reference/android/R.attr.html




回答3:


I'm using the android:fastScrollThumbDrawable but I not know why it isn't working, so searching on web i found here a hard code solution, I not know if it works on old API but in my case was solved the problem. Note I'm using API 18 like target and a device with API 17 to test.

the code:

try {
    Field f = AbsListView.class.getDeclaredField("mFastScroller");
    f.setAccessible(true);
    Object o = f.get(<<your listView here>>);
    f = f.getType().getDeclaredField("mThumbDrawable");
    f.setAccessible(true);
    Drawable drawable = (Drawable) f.get(o);
    drawable = getResources().getDrawable(R.drawable.<<your thumb drawable here can be a selector>>);
    f.set(o, drawable);
} catch (Exception e) {
    e.printStackTrace();
}



回答4:


In order to change the fastScrollThumbDrawable, the fastScrollTrackDrawable, or the text color of the fastscroll SectionIndexer you have to use a Context Theme. The other answers recommend overriding the application's theme via the AndroidManifest to do this. That does work but if you want different scrollbar appearances per ListView you can't do that. Also, the way you change the text color on SectionIndexer shouldn't be done in your app theme because it may have other undesired effects.

The best way to style a ListView for fastscrolling is to create a custom ListView that uses a ContextThemeWrapper.

Here is an example:

public class FastscrollThemedListView extends ListView {
    public FastscrollThemedListView(Context context, AttributeSet attrs) {
        super(new ContextThemeWrapper(context, R.style.FastScrollTheme), attrs);
    }
}

That is all you need. Your style will look like this:

<style name="FastScrollTheme">
    <item name="android:textColorPrimary">?android:textColorPrimaryInverse</item>
    <item name="android:fastScrollThumbDrawable">@drawable/fast_scrollbar_thumb</item>
    <item name="android:fastScrollTrackDrawable">@drawable/fast_scrollbar_track</item>
</style>

textColorPrimary is how you hook is how you hook into the SectionIndexer font color if you use it.

Your ListView would look like this:

<com.yourapp.view.FastscrollThemedListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:drawSelectorOnTop="false"
    android:cacheColorHint="@color/myWhite"
    android:scrollbars="vertical"
    android:scrollbarSize="12dip"
    android:fastScrollEnabled="true"/>



回答5:


I tested the previous answers and came up with this simplified version w/minimum requirements for a custom thumb icon for fast scrolling:

Android Manifest: (application tag)

android:theme="@style/ApplicationTheme"

res/values/themes.xml:

<resources>
  <style name="ApplicationTheme">
    <item name="android:fastScrollThumbDrawable">@drawable/scrollbar</item>
  </style>
</resources>

layout\activity_main.xml: (or wherever your view is that you want to apply this to & enable fast scrolling) ADD this attibute:

<ListView android:fastScrollEnabled="true" />



来源:https://stackoverflow.com/questions/9348291/android-listview-fastscrollthumbdrawable-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!