Custom listview fastscroller slider Image

Source: Internet
Author: User

Use listview fastscroller to display the default slider and custom slider image:

It is easy to set the quick rolling attribute. You only need to set the attribute in the XML file of the layout:

<Listview Android: Id = "@ + ID/listview" Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"Android: fastscrollenabled = "true"
Android: focusable = "true"
/>

However, sometimes you will find that the setting property is invalid and the slider does not appear in the scrolling listview. The reason is that this attribute has a minimum record limit. When the listview record can be displayed within 4 screens (that is, four pages are rolled), no slider will appear. It may be that API designers think that so few records do not need to be quickly rolled.

My basis is the android source code. See the constant declaration of fastscroller:

// Minimum number of pages to justify showing a fast scroll thumb
Private Static int min_pages = 4;

 

And:

// Are there enough pages to require fast scroll? Recompute only if total count changes
If (mitemcount! = Totalitemcount & visibleitemcount> 0 ){
Mitemcount = totalitemcount;
Mlonglist = mitemcount/visibleitemcount> = min_pages;
}

After reading the listview and its over-tired abslistview, none of them found the interface for setting custom images. It seems that the developer has no intention of making changes. However, you need to customize the image. It can only be used with great means.

After analysis, it is found that the image is a member of the mfastscroler object of the listview super class abslistview, and the member of the mfastscroler object is mshumbdrawable. In this example, the value of the mthumbdrawable type is drawable. Mfastscroller is the fastscroller type. This type is troublesome. The class declaration does not have modifier, that is, default (Package). It can only be called by classes in the package.

Therefore, writing reflection code is a little troublesome:

Try {
Field F = abslistview. Class. getdeclaredfield ("mfastscroller ");
F. setaccessible (true );
Object o = f. Get (listview );
F = f. GetType (). getdeclaredfield ("mthumbdrawable ");
F. setaccessible (true );
Drawable = (drawable) F. Get (O );
Drawable = getresources (). getdrawable (R. drawable. Icon );
F. Set (O, drawable );
Toast. maketext (this, F. GetType (). getname (), 1000). Show ();
} Catch (exception e ){
Throw new runtimeexception (E );
}

In this way, you can change the default slider image.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.