Learn more about the source code of Abslistview in Android

Source: Internet
Author: User

Do not know that the children shoes in the process of development have no interest in the ListView is how to achieve it? In fact, the parent of the ListView Abslistview is the key, but if you have seen the source, you will find abslistview nearly 7,000 lines of code, is not the head of the big Ah, oh, nothing, let's take a look at it.

Let's start with the constants in the class:

    public static final int transcript_mode_disabled = 0;    public static final int transcript_mode_normal = 1;    public static final int transcript_mode_always_scroll = 2;    static final int touch_mode_rest =-1;    static final int touch_mode_down = 0;    static final int touch_mode_tap = 1;    static final int touch_mode_done_waiting = 2;    static final int touch_mode_scroll = 3;    static final int touch_mode_fling = 4;    static final int touch_mode_overscroll = 5;    static final int touch_mode_overfling = 6;    static final int layout_normal = 0;    static final int layout_force_top = 1;    static final int layout_set_selection = 2;    static final int layout_force_bottom = 3;    static final int layout_specific = 4;    static final int layout_sync = 5;    static final int layout_move_selection = 6;    public static final int choice_mode_none = 0;    public static final int choice_mode_single = 1;    public static final int choice_mode_multiple = 2; public static final inT choice_mode_multiple_modal = 3; 
The above meanings are as follows:

1. Disable Copy mode

2. When a notification of changes in the data set is received, the list is automatically rolled to the bottom. But the condition must be that the last piece of data already appears on the screen

3, the list will automatically scroll to the bottom, regardless of whether the current data is visible.

4. Guess we are not in the middle of touching gestures.

5. Suppose we receive a touch-touching event, we wait to see if it is a swipe gesture.

6. Predict that the current touch event is a tap event, and we are waiting for it to be a long-press event.

7, the rest of the constant with this kind of this, omitted in this.


Next we'll look at a bunch of variables related to view drawing:

Drawable Mselector;    int mselectorposition = invalid_position;    Rect mselectorrect = new rect ();    Final RecycleBin Mrecycler = new RecycleBin ();    int mselectionleftpadding = 0;    int mselectiontoppadding = 0;    int mselectionrightpadding = 0;    int mselectionbottompadding = 0;    Rect mlistpadding = new rect ();    int mwidthmeasurespec = 0;    View Mscrollup;    View Mscrolldown;    Boolean mcachingstarted;    Boolean mcachingactive;    int mmotionposition;    int mmotionvieworiginaltop;    int mmotionviewnewtop;    int Mmotionx;    int mmotiony;    int mtouchmode = touch_mode_rest;    int mlasty;    int mmotioncorrection;    Private Velocitytracker Mvelocitytracker;    Private Flingrunnable mflingrunnable;    Abspositionscroller Mpositionscroller;    int mselectedtop = 0;    Boolean mstackfrombottom;    Boolean mscrollingcacheenabled;    Boolean mfastscrollenabled;    Boolean mfastscrollalwaysvisible; Private Onscrolllistener Monscrolllistener;

The above meanings are as follows:

1. The picture used to draw the selected item

2. The currently selected position in the list

3. Define the selected location and corresponding dimensions at the time of drawing

4. This data is set to store unused views, they will be reused, and in the next layout, avoid reuse.

5. Location of selected padding

6. Scroll up the flag with the scroll down view of the flag

7, when this view is scrolling, this flag bit is set to true, indicating that the child class that draws the cache will be able on its subclass.

8. Get the downward moving position

9. The annotations of the remaining variables are omitted.


Next, we look at the definitions of several interfaces

1, Onscrolllistener

This interface defines a callback when the list or nine Gongge scrolls.

There are several constants in this interface

public static int scroll_state_idle = 0;public static int scroll_state_touch_scroll = 1;public static int Scroll_state_fli NG = 2;
Each refers to the current list at rest, the swipe of the finger in the touch state, and the deceleration slide of the finger leaving, tending to be stationary.


There are also two functions defined in the interface:

1, onscrollstatechanged

2, Onscroll


The first view is a callback when view scrolling is in preparation

The second view is a callback when the view's scroll has ended


2, Selectionboundsadjuster

The meaning of this interface is to allow the top-level view of the current list item to implement this interface to modify the bounding area of its presentation.


The next code takes a look at a few APIs:

1, Setfastscrollerenableduithread

private void Setfastscrollerenableduithread (Boolean enabled) {        if (mfastscroll! = null) {            mfastscroll.setenabled (enabled);        } else if (enabled) {            Mfastscroll = new Fastscroller (this, mfastscrollstyle);            Mfastscroll.setenabled (True);        }        Resolvepadding ();        if (mfastscroll! = null) {            mfastscroll.updatelayout ();}    }

The function of this method is obvious, manipulating the object is Fastscroller, setting whether to support fast sliding, and then redraw the view.


Here are a few ways to calculate the scrolling area and show the effect of the method, we choose a few to look at:

@Override    protected int computeverticalscrollextent () {        final int count = Getchildcount ();        if (Count > 0) {            if (msmoothscrollbarenabled) {                int extent = count *;                View view = Getchildat (0);                Final int top = View.gettop ();                int height = view.getheight ();                if (height > 0) {                    extent + = (top * +)/height;                }                View = Getchildat (count-1);                final int bottom = View.getbottom ();                Height = view.getheight ();                if (height > 0) {                    Extent-= ((Bottom-getheight ()) * +)/height;                }                return extent;            } else {                return 1;            }        }        return 0;    }

This is the area that calculates the scrolling in the vertical direction.

This algorithm we can look at

1.

Extent + = (top * +)/height;
In this 100 we can understand that the system assumes that the height of the individual list of options is 100, in the principle of multiple fallback and less compensation, whether the height is greater than 100 or less than the 100/height, the resulting value can be understood as a scaling factor scalefactor, top * The scalefactor calculates that the extra margin is ultimately required.


Ok, the code is talking about it.





Learn more about the source code of Abslistview in Android

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.