Pull-down refresh and pull-up load more, pull-down refresh load more

Source: Internet
Author: User

Pull-down refresh and pull-up load more, pull-down refresh load more

The Code is as follows:

/*** Refresh and load more in the ListView drop-down list ** <p> ** <strong> change description: </strong> * <p> * If the OnRefreshListener interface and OnLoadMoreListener interface are set by default * <p> * three remaining flags: <br> * mIsAutoLoadMore (whether to automatically load more) <br> * mIsMoveToFirstItemAfterRefresh (whether to display the first Item after the pull-down refresh) <br> * mIsDoRefreshOnWindowFocused (when the control of the ListView is displayed on the screen, whether to directly display refreshing ...) ** @ author lee */public class MongoPullToRefreshOrLoadMoreListView extends ListView implementsOnScrollListe Ner {/** display formatted date template */private final static String DATE_FORMAT_STR = "yyyy MM dd HH: mm "; /** RATIO of actual padding distance to the offset on the Interface */private final static int RATIO = 3; // ============================== the following four constants are // the pull-down refresh status id ======================================// ** release refresh */private final static int RELEASE_TO_REFRESH = 0; /** pull-down refresh */private final static int PULL_TO_REFRESH = 1;/** REFRESHING */private final static int REFRESHING = 2 ;/ ** The refresh is complete or nothing is done, and the original status is restored. */Private final static int DONE = 3; // ============================ the following three constants are // load more status ID ====================================================/** loading in progress */ private final static int ENDINT_LOADING = 1; /** manually refresh */private final static int ENDINT_MANUAL_LOAD_DONE = 2;/** automatically refresh */private final static int ENDINT_AUTO_LOAD_DONE = 3; /*** <strong> pull-down refresh the real-time status flag of HeadView </strong> *** <p> * 0: RELEASE_TO_REFRESH; * <p> * 1: PULL_To_R EFRESH; * <p> * 2: REFRESHING; * <p> * 3: DONE; **/private int mHeadState; /*** <strong> load the real-time status flag of more FootView (EndView) </strong> *** <p> * 0: Complete/Wait For refresh; * <p> * 1: loading */private int mEndState; /// ===================================================== Flag // ======================================================//*** can load more */private boolean mCanLoadMore = false; /** you can pull down and refresh */private boolean mCanRefresh = false;/** can you automatically load more data? (Note: first determine whether there are more loads. If not, this flag is meaningless.) */private boolean mIsAutoLoadMore = false; /** whether to display the first Item */private boolean mIsMoveToFirstItemAfterRefresh = false after the pull-down refresh;/** when the control of the ListView is displayed on the screen, whether to directly display refreshing... */private boolean mIsDoRefreshOnUIChanged = false; public boolean isCanLoadMore () {return mCanLoadMore;} public void setCanLoadMore (boolean pCanLoadMore) {mCanLoadMore = pCanLoadMore; if (mCanLoadMore & getFoo TerViewsCount () = 0) {addFooterView ();} else {if (null! = MEndRootView) {removeFooterView (mEndRootView) ;}} public boolean isCanRefresh () {return empty;} public void setCanRefresh (boolean pCanRefresh) {mCanRefresh = empty;} public boolean isAutoLoadMore () {return mIsAutoLoadMore;} public void setAutoLoadMore (boolean pIsAutoLoadMore) {mIsAutoLoadMore = pIsAutoLoadMore;} public boolean isMoveToFirstItemAfterRefresh () {return mIsMoveToFirstItemAfter Refresh;} public void initialize (boolean empty) {empty = empty;} public boolean empty () {return mIsDoRefreshOnUIChanged;} public void setDoRefreshOnUIChanged (boolean empty) {mIsDoRefreshOnUIChanged = empty ;} // ============================================== ============================================================ Private layoutInflater mInflater; private LinearLayout mHeadRootView; private TextView mTipsTextView; private TextView overview; private ImageView mArrowImageView; private ProgressBar mProgressBar; private View mEndRootView; private ProgressBar mEndLoadProgressBar; private TextView overview; /** headView animation */private RotateAnimation mArrowAnim ;/ ** HeadView reverse animation */private RotateAnimation mArrowReverseAnim;/** it is used to ensure that the value of startY is recorded only once in a complete touch event */private boolean mIsRecored; private int mHeadViewWidth; private int mHeadViewHeight; private int mStartY; private boolean mIsBack; private int mFirstItemIndex; private int mLastItemIndex; private int mCount; @ SuppressWarnings ("unused") private boolean mEnoughCount; // how many screens are filled? Private OnRefreshListener mRefreshListener; private OnLoadMoreListener mLoadMoreListener; private String mLabel; public String getLabel () {return mLabel;} public void setLabel (String pLabel) {mLabel = pLabel ;} public aggregate (Context pContext) {super (pContext); init (pContext);} public MongoPullToRefreshOrLoadMoreListView (Context pContext, AttributeSet pAttrs) {super (pContext, pA Ttrs); init (pContext);} public MongoPullToRefreshOrLoadMoreListView (Context pContext, AttributeSet pAttrs, int pDefStyle) {super (pContext, pAttrs, pDefStyle); init (pContext );} /*** initialization operation ** @ param pContext */private void init (Context pContext) {// final ViewConfiguration _ ViewConfiguration = // ViewConfiguration. get (pContext); // mTouchSlop = _ ViewConfiguration. getScaledTouchSlop (); setCacheColorHint (pC Ontext. getResources (). getColor (R. color. transparent); setOnLongClickListener (null); mInflater = LayoutInflater. from (pContext); addHeadView (); setOnScrollListener (this); initPullImageAnimation (0);}/*** Add the HeadView refreshed by the drop-down list */private void addHeadView () {mHeadRootView = (LinearLayout) mInflater. inflate (R. layout. pull_to_refresh_head2, null); mArrowImageView = (ImageView) mHeadRootView. findViewById (R. id. head_arr OwImageView); mArrowImageView. setMinimumWidth (70); mArrowImageView. setMinimumHeight (50); mProgressBar = (ProgressBar) mHeadRootView. findViewById (R. id. head_progressBar); mTipsTextView = (TextView) mHeadRootView. findViewById (R. id. head_tipsTextView); mLastUpdatedTextView = (TextView) mHeadRootView. findViewById (R. id. head_lastUpdatedTextView); measureView (mHeadRootView); mHeadViewHeight = mHeadRootView. getMea SuredHeight (); mHeadViewWidth = mHeadRootView. getMeasuredWidth (); mHeadRootView. setPadding (0,-1 * mHeadViewHeight, 0, 0); mHeadRootView. invalidate (); addHeaderView (mHeadRootView, null, false); mHeadState = DONE; changeHeadViewByState ();}/*** add and load more FootView */private void addFooterView () {mEndRootView = mInflater. inflate (R. layout. pull_to_refresh_load_more, null); mEndRootView. setVisibility (View. VISIBLE ); MEndLoadProgressBar = (ProgressBar) mEndRootView. findViewById (R. id. pull_to_refresh_progress); mEndLoadTipsTextView = (TextView) mEndRootView. findViewById (R. id. load_more); mEndRootView. setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View v) {if (mCanLoadMore) {if (mCanRefresh) {if (mEndState! = ENDINT_LOADING & mHeadState! = REFRESHING) {mEndState = ENDINT_LOADING; onLoadMore () ;}} else if (mEndState! = ENDINT_LOADING) {mEndState = ENDINT_LOADING; onLoadMore () ;}}}); addFooterView (mEndRootView); if (mIsAutoLoadMore) {mEndState = ENDINT_AUTO_LOAD_DONE ;} else {mEndState = ENDINT_MANUAL_LOAD_DONE ;}/ *** instantiate the animation effect of the refreshed arrow in the drop-down menu */private void initPullImageAnimation (final int pAnimDuration) {int _ Duration; if (pAnimDuration> 0) {_ Duration = pAnimDuration;} else {_ Duration = 250;} Interpolator _ Interpolator = ne W LinearInterpolator (); mArrowAnim = new RotateAnimation (0,-180, RotateAnimation. RELATIVE_TO_SELF, 0.5f, RotateAnimation. RELATIVE_TO_SELF, 0.5f); mArrowAnim. setInterpolator (_ Interpolator); mArrowAnim. setDuration (_ Duration); mArrowAnim. setFillAfter (true); mArrowReverseAnim = new RotateAnimation (-180, 0, RotateAnimation. RELATIVE_TO_SELF, 0.5f, RotateAnimation. RELATIVE_TO_SELF, 0.5f); mArrowReverseAnim. setInt Erpolator (_ Interpolator); mArrowReverseAnim. setDuration (_ Duration); mArrowReverseAnim. setFillAfter (true);}/*** measure HeadView width and height (this method applies only to LinearLayout) ** @ param pChild */private void measureView (View pChild) {ViewGroup. layoutParams p = pChild. getLayoutParams (); if (p = null) {p = new ViewGroup. layoutParams (ViewGroup. layoutParams. MATCH_PARENT, ViewGroup. layoutParams. WRAP_CONTENT);} int childWidthSpec = Vie WGroup. getChildMeasureSpec (0, 0 + 0, p. width); int lpHeight = p. height; int childHeightSpec; if (lpHeight> 0) {childHeightSpec = MeasureSpec. makeMeasureSpec (lpHeight, MeasureSpec. EXACTLY);} else {childHeightSpec = MeasureSpec. makeMeasureSpec (0, MeasureSpec. UNSPECIFIED);} pChild. measure (childWidthSpec, childHeightSpec);}/*** to determine whether to slide to the bottom of the ListView */@ Overridepublic void onScroll (AbsListView pView, int pFir StVisibleItem, int week, int pTotalItemCount) {mFirstItemIndex = week; mLastItemIndex = week + week-2; mCount = pTotalItemCount-2; if (pTotalItemCount> pVisibleItemCount) {mEnoughCount = true ;} else {mEnoughCount = false ;}}/*** load judgment. * // @ Overridepublic void onScrollStateChanged (AbsListView pView, int pScrollState) {if (mCanLoadMore) {if (mLastItemIndex = mCount & pScrollState = success) {if (mEndState! = ENDINT_LOADING) {if (mIsAutoLoadMore) {if (mCanRefresh) {if (mHeadState! = REFRESHING) {mEndState = ENDINT_LOADING; onLoadMore (); changeEndViewByState () ;}} else {mEndState = ENDINT_LOADING; onLoadMore (); else () ;}} else {mEndState = callback; changeEndViewByState () ;}}} else if (mEndRootView! = Null & mEndRootView. getVisibility () = VISIBLE) {mEndRootView. setVisibility (View. GONE); this. removeFooterView (mEndRootView);}/*** change loading more statuses */private void changeEndViewByState () {if (mCanLoadMore) {switch (mEndState) {case ENDINT_LOADING: // refresh if (mEndLoadTipsTextView. getText (). equals (R. string. p2refresh_doing_end_refresh) {break;} mEndLoadTipsTextView. setText (R. string. p2refresh_doing_end_refresh ); MEndLoadTipsTextView. setVisibility (View. VISIBLE); mEndLoadProgressBar. setVisibility (View. VISIBLE); break; case ENDINT_MANUAL_LOAD_DONE: // refresh the mEndLoadTipsTextView manually. setText (R. string. p2refresh_end_click_load_more); mEndLoadTipsTextView. setVisibility (View. VISIBLE); mEndLoadProgressBar. setVisibility (View. GONE); mEndRootView. setVisibility (View. VISIBLE); break; case ENDINT_AUTO_LOAD_DONE: // The mEndLoadTip is automatically refreshed. STextView. setText (R. string. p2refresh_end_load_more); mEndLoadTipsTextView. setVisibility (View. VISIBLE); mEndLoadProgressBar. setVisibility (View. GONE); mEndRootView. setVisibility (View. VISIBLE); break; default: break; }}}/*** Note: This method is not applicable to ViewPager. The method is to directly call pull2RefreshManually (); * // @ Overridepublic void onWindowFocusChanged (boolean pHasWindowFocus) {super. onWindowFocusChanged (pHasWindowFocus); if (mIsDoRefr EshOnUIChanged) {if (pHasWindowFocus) {pull2RefreshManually () ;}}/ *** when the control of the ListView is displayed on the screen, it is directly refreshed... */public void pull2RefreshManually () {mHeadState = REFRESHING; changeHeadViewByState (); onRefresh (); mIsRecored = false; mIsBack = false ;} /***/public boolean onTouchEvent (MotionEvent event) {if (mCanRefresh) {if (mCanLoadMore & mEndState = ENDINT_LOADING) {return super. onTouchEvent (event);} swit Ch (event. getAction () {case MotionEvent. ACTION_DOWN: if (mFirstItemIndex = 0 &&! MIsRecored) {mIsRecored = true; mStartY = (int) event. getY ();} else if (mFirstItemIndex = 0 & mIsRecored) {mStartY = (int) event. getY ();} break; case MotionEvent. ACTION_UP: if (mHeadState! = REFRESHING) {if (mHeadState = DONE) {} if (mHeadState = PULL_TO_REFRESH) {mHeadState = DONE; changeHeadViewByState ();} if (mHeadState = RELEASE_TO_REFRESH) {mHeadState = REFRESHING; changeHeadViewByState (); onRefresh () ;}} mIsRecored = false; mIsBack = false; break; case MotionEvent. ACTION_MOVE: int _ TempY = (int) event. getY (); if (! MIsRecored & mFirstItemIndex = 0) {mIsRecored = true; mStartY = _ TempY;} if (mHeadState! = REFRESHING & mIsRecored) {if (mHeadState = RELEASE_TO_REFRESH) {setSelection (0); if (_ TempY-mStartY)/RATIO <mHeadViewHeight) & (_ TempY-mStartY)> 0) {mHeadState = PULL_TO_REFRESH; changeHeadViewByState ();} else if (_ TempY-mStartY <= 0) {mHeadState = DONE; changeHeadViewByState () ;}} if (mHeadState = PULL_TO_REFRESH) {setSelection (0); if (_ TempY-mStartY)/RATIO> = mHeadViewHeight) {mHe AdState = RELEASE_TO_REFRESH; mIsBack = true; changeHeadViewByState ();} else if (_ TempY-mStartY <= 0) {mHeadState = DONE; changeHeadViewByState ();}} if (mHeadState = DONE) {if (_ TempY-mStartY> 0) {mHeadState = PULL_TO_REFRESH; changeHeadViewByState () ;}} if (mHeadState = PULL_TO_REFRESH) {mHeadRootView. setPadding (0,-1 * mHeadViewHeight + (_ TempY-mStartY)/RATIO, 0, 0);} if (mHeadState = RELEASE _ TO_REFRESH) {mHeadRootView. setPadding (0, (_ TempY-mStartY)/RATIO-mHeadViewHeight, 0, 0) ;}} break ;}} return super. onTouchEvent (event);}/*** this method is called when the HeadView status changes to update the interface */private void changeHeadViewByState () {switch (mHeadState) {case RELEASE_TO_REFRESH: mArrowImageView. setVisibility (View. VISIBLE); mProgressBar. setVisibility (View. GONE); mTipsTextView. setVisibility (View. VISIBLE); // mLastUpdatedTextV Iew. setVisibility (View. VISIBLE); mArrowImageView. clearAnimation (); mArrowImageView. startAnimation (mArrowAnim); mTipsTextView. setText (R. string. p2refresh_release_refresh); break; case PULL_TO_REFRESH: mProgressBar. setVisibility (View. GONE); mTipsTextView. setVisibility (View. VISIBLE); // mLastUpdatedTextView. setVisibility (View. VISIBLE); mArrowImageView. clearAnimation (); mArrowImageView. setVisibility (View. VISIBLE ); If (mIsBack) {mIsBack = false; mArrowImageView. clearAnimation (); mArrowImageView. startAnimation (mArrowReverseAnim); mTipsTextView. setText (R. string. p2refresh_pull_to_refresh);} else {mTipsTextView. setText (R. string. p2refresh_pull_to_refresh);} break; case REFRESHING: changeHeaderViewRefreshState (); break; case DONE: mHeadRootView. setPadding (0,-1 * mHeadViewHeight, 0, 0); mProgressBar. setVisibility (View. GON E); mArrowImageView. clearAnimation (); // mArrowImageView. setImageResource (R. drawable. pulltorefresh_arrow); mArrowImageView. setImageResource (R. drawable. arrow_gray); mTipsTextView. setText (R. string. p2refresh_pull_to_refresh); mLastUpdatedTextView. setVisibility (View. GONE); break ;}}/*** changed the display of HeadView in the refreshed state */private void changeHeaderViewRefreshState () {mHeadRootView. setPadding (0, 0, 0, 0); mProgressBar. setVi Sibility (View. VISIBLE); mArrowImageView. clearAnimation (); mArrowImageView. setVisibility (View. GONE); mTipsTextView. setText (R. string. p2refresh_doing_head_refresh); mLastUpdatedTextView. setVisibility (View. VISIBLE);}/*** pull-down refresh listener interface */public interface OnRefreshListener {public void onRefresh ();} /*** load more listening interfaces */public interface OnLoadMoreListener {public void onLoadMore ();} public void setOnRefreshListener (O NRefreshListener pRefreshListener) {if (pRefreshListener! = Null) {mRefreshListener = pRefreshListener; mCanRefresh = true;} public void setOnLoadListener (OnLoadMoreListener pLoadMoreListener) {if (pLoadMoreListener! = Null) {mLoadMoreListener = pLoadMoreListener; mCanLoadMore = true; if (mCanLoadMore & getFooterViewsCount () = 0) {addFooterView ();}}} /*** refreshing from the drop-down list */private void onRefresh () {if (mRefreshListener! = Null) {mRefreshListener. onRefresh () ;}}/*** pull-down refresh completed */public void onRefreshComplete () {mHeadState = DONE; mLastUpdatedTextView. setText (getResources (). getString (R. string. p2refresh_refresh_lasttime) + new SimpleDateFormat (DATE_FORMAT_STR, Locale. CHINA ). format (new Date (); changeHeadViewByState (); if (mIsMoveToFirstItemAfterRefresh) {mFirstItemIndex = 0; setSelection (0) ;}/ *** loading more, footView: loading... */Private void onLoadMore () {if (mLoadMoreListener! = Null) {mEndLoadTipsTextView. setText (R. string. p2refresh_doing_end_refresh); mEndLoadTipsTextView. setVisibility (View. VISIBLE); mEndLoadProgressBar. setVisibility (View. VISIBLE); mLoadMoreListener. onLoadMore () ;}/ *** load more complete */public void onLoadMoreComplete () {if (mIsAutoLoadMore) {mEndState = ENDINT_AUTO_LOAD_DONE;} else {mEndState = finished ;} changeEndViewByState ();}/*** updates the refresh time! */Public void setAdapter (BaseAdapter adapter) {mLastUpdatedTextView. setText (getResources (). getString (R. string. p2refresh_refresh_lasttime) + new SimpleDateFormat (DATE_FORMAT_STR, Locale. CHINA ). format (new Date (); super. setAdapter (adapter);} public void setEndRootViewVisibility (boolean B) {if (mEndRootView = null) {return;} if (getFooterViewsCount () = 0 & B) {addFooterView () ;}else {removeFooterView (mEndRootView) ;}} public View getFooterView () {return mEndRootView ;}}



You can pull and refresh more source code from a drop-down list on the android listview.

1035688327@qq.com
Please check it!
I will give you the source code of the project. You can think about it by yourself. The data in it also has a local storage.

Kneel Android development ListView pull-down refresh, pull up and load source code



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.