Android built-in refresh SwipeRefreshLayout pull load more automatic refresh, swiperefreshlayout

Source: Internet
Author: User

Android built-in refresh SwipeRefreshLayout pull load more automatic refresh, swiperefreshlayout

Previously, this article mainly focused on SwipeRefreshLayout, which is provided by the system. However, it only supports pull-down refresh and does not load more functions. But I don't know why the official SwipeRefreshLayout only provides the pull-down refreyout function, in many cases, we need to pull up and refresh the function, but sometimes we also need to pull up and load more, and there will be a market if needed, so the pull up and load came into being.

Add pull-on and load more functions to automatically refresh SwipeRefreshLayout, similar to the effect of automatically refresh loading when the Android and Youku versions open the page. If you have any questions, please correct them.

The Code is as follows:

Package com. example. swiperefreshlayoutdemo;


Import java. lang. reflect. InvocationTargetException;
Import java. lang. reflect. Method;


Import android. annotation. SuppressLint;
Import android. content. Context;
Import android. support. v4.widget. SwipeRefreshLayout;
Import android. util. AttributeSet;
Import android. util. Log;
Import android. view. LayoutInflater;
Import android. view. MotionEvent;
Import android. view. View;
Import android. view. ViewConfiguration;
Import android. widget. AbsListView;
Import android. widget. AbsListView. OnScrollListener;
Import android. widget. ListView;


/**
* Inherits from SwipeRefreshLayout to pull and load more functions when sliding to the bottom.
*/
Public class RefreshLayout extends SwipeRefreshLayout implements
OnScrollListener {


/**
* Pull up when sliding to the bottom
*/


Private int mTouchSlop;
/**
* Listview instance
*/
Private ListView mListView;


/**
* Pull the listener to the bottom of the pull and Load Operation
*/
Private OnLoadListener mOnLoadListener;


/**
* Footer loading of ListView
*/
Private View mListViewFooter;


/**
* Y coordinate at press time
*/
Private int mYDown;
/**
* Y coordinate during lifting, used together with mYDown to slide to the bottom to determine whether to pull up or pull down
*/
Private int mLastY;
/**
* Loading or not (pull and load more)
*/
Private boolean isLoading = false;


/**
* @ Param context
*/
Public RefreshLayout (Context context ){
This (context, null );
}


@ SuppressLint ("InflateParams ")
Public RefreshLayout (Context context, AttributeSet attrs ){
Super (context, attrs );


MTouchSlop = ViewConfiguration. get (context). getScaledTouchSlop ();


MListViewFooter = LayoutInflater. from (context). inflate (
R. layout. listview_footer, null, false );
}


@ Override
Protected void onLayout (boolean changed, int left, int top, int right,
Int bottom ){
Super. onLayout (changed, left, top, right, bottom );
// Initialize the ListView object
If (mListView = null ){
GetListView ();
}
}


/**
* Get the ListView object
*/
Private void getListView (){
Int childs = getChildCount ();
If (childs> 0 ){
View childView = getChildAt (0 );
If (childView instanceof ListView ){
MListView = (ListView) childView;
// Set the scroll listener to ListView so that the listener can be automatically loaded when scrolling.
MListView. setOnScrollListener (this );
Log. d (VIEW_LOG_TAG, "### find listview ");
}
}
}


/*
* (Non-Javadoc)
*
* @ See android. view. ViewGroup # dispatchTouchEvent (android. view. MotionEvent)
*/
@ Override
Public boolean dispatchTouchEvent (MotionEvent event ){
Final int action = event. getAction ();


Switch (action ){
Case MotionEvent. ACTION_DOWN:
// Press
MYDown = (int) event. getRawY ();
Break;


Case MotionEvent. ACTION_MOVE:
// Move
MLastY = (int) event. getRawY ();
Break;


Case MotionEvent. ACTION_UP:
// Lift
If (canLoad ()){
LoadData ();
}
Break;
Default:
Break;
}


Return super. dispatchTouchEvent (event );
}


/**
* Whether more data can be loaded. The condition is at the bottom. The listview is not being loaded and it is a pull-up operation.
*
* @ Return
*/
Private boolean canLoad (){
Return isBottom ()&&! IsLoading & isPullUp ();
}


/**
* Determines whether it is at the bottom.
*/
Private boolean isBottom (){


If (mListView! = Null & mListView. getAdapter ()! = Null ){
Return mListView. getLastVisiblePosition () = (mListView
. GetAdapter (). getCount ()-1 );
}
Return false;
}


/**
* Whether it is a pull-up operation
*
* @ Return
*/
Private boolean isPullUp (){
Return (mYDown-mLastY)> = mTouchSlop;
}


/**
* If it is at the bottom and the pull operation is performed, the onLoad method is executed.
*/
Private void loadData (){
If (mOnLoadListener! = Null ){
// Set the status
SetLoading (true );
//
MOnLoadListener. onLoad ();
}
}


/**
* @ Param loading
*/
Public void setLoading (boolean loading ){
IsLoading = loading;
If (isLoading ){
MListView. addFooterView (mListViewFooter );
} Else {
MListView. removeFooterView (mListViewFooter );
MYDown = 0;
MLastY = 0;
}
}


/**
* @ Param loadListener
*/
Public void setOnLoadListener (OnLoadListener loadListener ){
MOnLoadListener = loadListener;
}


@ Override
Public void onScrollStateChanged (AbsListView view, int scrollState ){


}


@ Override
Public void onScroll (AbsListView view, int firstVisibleItem,
Int visibleItemCount, int totalItemCount ){
// More can be loaded at the bottom of the scroll.
If (canLoad ()){
LoadData ();
}
}

/**
* Set refresh
*/
Public static void setRefreshing (SwipeRefreshLayout refreshLayout,
Boolean refreshing, boolean every y ){
Class <? Extends SwipeRefreshLayout> refreshLayoutClass = refreshLayout
. GetClass ();
If (refreshLayoutClass! = Null ){


Try {
Method setRefreshing = refreshLayoutClass. getDeclaredMethod (
"SetRefreshing", boolean. class, boolean. class );
SetRefreshing. setAccessible (true );
SetRefreshing. invoke (refreshLayout, refreshing, interval y );
} Catch (NoSuchMethodException e ){
E. printStackTrace ();
} Catch (IllegalAccessException e ){
E. printStackTrace ();
} Catch (InvocationTargetException e ){
E. printStackTrace ();
}
}
}


/**
* Load more listeners
*
* @ Author mrsimple
*/
Public static interface OnLoadListener {
Public void onLoad ();
}
}


Method call code:

SwipeLayout. setOnRefreshListener (this );
SwipeLayout. setOnLoadListener (this );


Retry

/**
* Pull up and refresh
*/
@ Override
Public void onRefresh (){
SwipeLayout. postDelayed (new Runnable (){


@ Override
Public void run (){
// Update data
// Call this method after the update ends and refresh
SwipeLayout. setRefreshing (false );
}
},2000 );


}


/**
* Load more
*/
@ Override
Public void onLoad (){
SwipeLayout. postDelayed (new Runnable (){


@ Override
Public void run (){
// Update data
// Call this method after the update ends and refresh
SwipeLayout. setLoading (false );
}
},2000 );
}


If necessary, download the project source code.


Welcome to reprint, reprint Please note: http://blog.csdn.net/seven2729/article/details/48311451

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.