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 );
}
}