The android list stops scrolling and loads images. This is a common method,

Source: Internet
Author: User

The android list stops scrolling and loads images. This is a common method,

In the itemView of the Adapter, It is troublesome to judge whether the list is rolling. Coupling may be serious.

So I have considered whether it is possible to check the rolling status of the list in itemView and listen to the stopped status to load the picture, so that itemView and Adapter can be decoupled from ListView?

 

I came up with an implementation method and directly went to the Code:

/**
* Determines whether the parent control (ListView, ViewPager, etc.) of a view is in the scrolling state.
* @ Param view Interface to be judged
* @ Return is still rolling
*/
Public static boolean isParentScrolling (View view ){
ViewParent parent = view. getParent ();
While (parent! = Null ){
If (parent instanceof AbsListView ){
// Private int mLastScrollState = OnScrollListener. SCROLL_STATE_IDLE;
Int state = getAbsListViewScrollState (AbsListView) parent );
Return state! = AbsListView. OnScrollListener. SCROLL_STATE_IDLE;
}
If (parent instanceof RecyclerView ){
// Private int mScrollState = SCROLL_STATE_IDLE;
Int state = getRecyclerViewScrollState (RecyclerView) parent );
Return state! = RecyclerView. SCROLL_STATE_IDLE;
}
// ...... Other types
Parent = parent. getParent ();
}
Return false;
}

/**
* Get the scrolling status of AbsListView through reflection
*/
Public static int getAbsListViewScrollState (AbsListView listView ){
// Private int mLastScrollState = OnScrollListener. SCROLL_STATE_IDLE;
Try {
Field f = AbsListView. class. getDeclaredField ("mLastScrollState ");
F. setAccessible (true );
Return f. getInt (listView );
} Catch (Exception e ){
E. printStackTrace ();
}
Return AbsListView. OnScrollListener. SCROLL_STATE_IDLE;
}

/**
* Get the scrolling status of AbsListView through reflection
*/
Public static int getRecyclerViewScrollState (RecyclerView listView ){
// Private int mScrollState = SCROLL_STATE_IDLE;
Try {
Field f = AbsListView. class. getDeclaredField ("mScrollState ");
F. setAccessible (true );
Return f. getInt (listView );
} Catch (Exception e ){
E. printStackTrace ();
}
Return RecyclerView. SCROLL_STATE_IDLE;
}

Usage:

Before loading an image:

Boolean isScrolling = ViewUtil. isParentScrolling (this );
If (isScrolling ){
// Test. Normally, if the scroll stops, the onDraw method will not be called to load the image for Refresh.
// Therefore, we have to add latency detection to determine if it has stopped.
WaitScrollStop ();
Return;
}


The related methods may vary in different situations:
/**
* Called when loading an image and detecting scrolling
*/
Private Runnable checkScrollIdle = new Runnable (){
@ Override
Public void run (){
Boolean isScrolling = ViewUtil. isParentScrolling (DrawView. this );
If (! IsScrolling ){
// Stop time and reload the image
// Refresh and redraw the image to check whether the image is loaded.
PostInvalidate ();
} Else {
// No stop, continue detection
WaitScrollStop ();
}
}
};

/**
* Call When scrolling is detected and handler is used to check the stop status
*/
Private void waitScrollStop (){
If (isAttachedToWindow ()){
// Ensure that it is not removed
RemoveCallbacks (checkScrollIdle );
PostDelayed (checkscrolequalle, 1000 );
}
}

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.