Add ScrollBar to Android Horizontallistview

Source: Internet
Author: User

recently due to the project needs, to implement a horizontal scrolling list, is intended to use Horizontalscrollview directly, but because there is no view reuse, the list of items will occupy more memory, exclude; and then think of Gallery, But Gallery has an animated effect that automatically locates to the center, and the work to remove this effect is more complicated. Last search. Found this android-horizontallistview, realized the most functions of the ListView, but does not support header, footer and scrollbar, project requirements to provide Scroolbar display, So just go ahead and add the horizontal scrollbar.

ScrollBar is implemented as a basic function in view, subclasses need to implement the three methods of Computexxxscrollrange, Computexxxscrollextent and Computexxxscrolloffset, including:

1, computexxxscrollrange, calculate scroll bar scroll range

2, Computexxxscrollextent, calculate the size of the slider display

3, Computexxxscrolloffset, calculate the rolling offset, that is, the current scroll position

The above three methods return the values are relative values, that is, when calculating the use of a unified measure, the specific view of the display in the Ondrawscrollbar () method of view has been implemented.

With these three methods, you also need to call Awakenscrollbars () in the process of scrolling, because ScrollBar is automatically hidden when you stop scrolling, so you need to constantly evoke scrollbar when scrolling to let it show, that's the way.

Horizontallistview by listening to the scrolling gesture to constantly requestlayout, re-layout list display to achieve scrolling effect, so awakenscrollbars this method is added in OnLayout.

protected void OnLayout (Boolean changed, int left, int top, int. Right,int bottom) {super.onlayout (changed, left, top, righ T, bottom); if (madapter = = null) {return;} Force the OS to redraw this viewif (!awakenscrollbars ()) {postinvalidate ();} ...}

Add the scrollbar related calculations below

@Overrideprotected int Computehorizontalscrollrange () {int count = Madapter.getcount (); return Math.max (count * 100, 0);} @Overrideprotected int computehorizontalscrollextent () {int count = Getchildcount (); if (Count > 0) {int extent = count * 100;  View view = Getchildat (0), final int left = View.getleft (), int width = view.getwidth (), if (Width > 0) {extent + = (left * ()/width;} View = Getchildat (count-1), final int right = View.getright (), width = View.getwidth (), if (Width > 0) {extent-= ((righ T-getwidth ()) * ()/width;} return extent;} return 0;} @Overrideprotected int Computehorizontalscrolloffset () {final int firstposition = mleftviewadapterindex;final int            ChildCount = Getchildcount (); if (firstposition >= 0 && childCount > 0) {final view view = Getchildat (0);            Final int left = View.getleft ();            int width = view.getwidth ();        if (Width > 0) {int result = Math.max (Firstposition *-(Left *)/width, 0);        return result; }}return 0;}

This reference to the implementation in Abslistview, the above calculation process is relatively easy to understand, x100 is to maintain the accuracy of two decimal places. The value of range is the total number of list items (Madapter.getcount ()), the size of the slider extent is the current number of displays, and then the portion of the left and right bounds is removed to improve accuracy; Offset is the index value of the first list displayed. In other words, the "number" is used to calculate the unit of measure.


Add ScrollBar to Android Horizontallistview

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.