Detailed explanation of the AbsListView source code in Android

Source: Internet
Author: User

Detailed explanation of the AbsListView source code in Android

I wonder if you are interested in developing ListView. How does ListView work? In fact, the parent class AbsListView of ListView is the key, but if you have read the source code, you will find that AbsListView has nearly 7000 lines of code. Is it too big, let's take a look.

We will first analyze 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 preceding meanings are as follows:

 

1. Disable copy mode

2. When a notification of a data set change is received, the list is automatically rolled to the bottom. However, the condition must be that the last piece of data has already appeared 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 the touch gesture.

5. If we receive a touch event, we will wait to see if it is a sliding gesture.

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

7. Other constants and such constants are omitted here.

 

Next, let's take a look at a batch of variables related to view rendering:

 

    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 preceding meanings are as follows:

 

1. Used to draw the selected Image

2. The selected position in the list

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

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

5. Position of the selected padding

6. View of the logo to scroll up And the logo to scroll down

7. When the view is rolling, the flag is set to true, indicating that the subclass of the cache will be able to draw on its subclass.

8. Get the position of the downward movement

9. The annotations of other variables are omitted.

 

Next, let's look at the definitions of several interfaces.

1. OnScrollListener

This interface defines the callback when the list or nine cells are rolling.

The following constants exist in this interface:

 

public static int SCROLL_STATE_IDLE = 0;public static int SCROLL_STATE_TOUCH_SCROLL = 1;public static int SCROLL_STATE_FLING = 2;
The current list is at rest, the fingers are in the touch state, and the fingers move slowly and tend to be at rest.

 

 

The interface also defines two functions:

1. onScrollStateChanged

2. onScroll

 

The first view is the callback when the view is rolling.

The second view is the callback when the view rolling has ended.

 

2. SelectionBoundsAdjuster

This interface allows the top-level view of the current list item to implement this interface to modify its display boundary area.

 

The following code selects several 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. The manipulated object is FastScroller. You can set whether quick sliding is supported and then re-draw the view.

 

 

The following methods are used to calculate the rolling area and display effect. Let's take a few of them to look:

 

 @Override    protected int computeVerticalScrollExtent() {        final int count = getChildCount();        if (count > 0) {            if (mSmoothScrollbarEnabled) {                int extent = count * 100;                View view = getChildAt(0);                final int top = view.getTop();                int height = view.getHeight();                if (height > 0) {                    extent += (top * 100) / height;                }                view = getChildAt(count - 1);                final int bottom = view.getBottom();                height = view.getHeight();                if (height > 0) {                    extent -= ((bottom - getHeight()) * 100) / height;                }                return extent;            } else {                return 1;            }        }        return 0;    }

This is the rolling area in the vertical direction.

 

Let's take a look at this algorithm.

1,

extent += (top * 100) / height;
Here, we can understand that the system assumes that the height of the option for a single list is 100. Based on the principle of multiple return and less population, whether the height is greater than 100 or less than 100/height, the obtained value can be understood as the scaling factor scaleFactor. The top * scaleFactor calculates the margin that needs to be added.

 

 

 

 

 

 

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.