企业互联网服务系统V1.01importandroid.content.Context;importandroid.util.AttributeSet;importandroid.view.MotionEvent;importandroid.view.View;importandroid.view.ViewTreeObserver.OnGlobalLayoutListener;importandroid.view.animation.DecelerateInterpolator;importandroid.widget.AbsListView;importandroid.widget.AbsListView.OnScrollListener;importandroid.widget.ListAdapter;importandroid.widget.ListView;importandroid.widget.RelativeLayout;importandroid.widget.Scroller;importandroid.widget.TextView;importcom.insthub.ecmobile.R;importandroid.os.Message;importandroid.os.Handler;publicclassXListViewextendsListViewimplementsOnScrollListener{privatefloatmLastY=-1;//saveeventyprivateScrollermScroller;//usedforscrollbackprivateOnScrollListenermScrollListener;//user'sscrolllistener//theinterfacetotriggerrefreshandloadmore.privateIXListViewListenermListViewListener;//--headerviewprivateXListViewHeadermHeaderView;//headerviewcontent,useittocalculatetheHeader'sheight.Andhideit//whendisablepullrefresh.privateRelativeLayoutmHeaderViewContent;privateTextViewmHeaderTimeView;privateintmHeaderViewHeight;//headerview'sheightprivatebooleanmEnablePullRefresh=true;privatebooleanmPullRefreshing=false;//isrefreashing.//--footerviewprivateXListViewFootermFooterView;privatebooleanmEnablePullLoad;privatebooleanmPullLoading;privatebooleanmIsFooterReady=false;//totallistitems,usedtodetectisatthebottomoflistview.privateintmTotalItemCount;企业互联网服务系统V1.02//formScroller,scrollbackfromheaderorfooter.privateintmScrollBack;privatefinalstaticintSCROLLBACK_HEADER=0;privatefinalstaticintSCROLLBACK_FOOTER=1;privatefinalstaticintSCROLL_DURATION=400;//scrollbackdurationprivatefinalstaticintPULL_LOAD_MORE_DELTA=50;//whenpullup=50px//atbottom,trigger//loadmore.privatefinalstaticfloatOFFSET_RADIO=1.8f;//supportiOSlikepull//feature.privateintid;/***@paramcontext*/publicXListView(Contextcontext){super(context);initWithContext(context);}publicXListView(Contextcontext,AttributeSetattrs){super(context,attrs);initWithContext(context);}publicXListView(Contextcontext,AttributeSetattrs,intdefStyle){super(context,attrs,defStyle);initWithContext(context);}privatevoidinitWithContext(Contextcontext){mScroller=newScroller(context,newDecelerateInterpolator());//XListViewneedthescrollevent,anditwilldispatchtheeventto//user'slistener(asaproxy).super.setOnScrollListener(this);//initheaderviewmHeaderView=newXListViewHeader(context);mHeaderViewContent=(RelativeLayout)mHeaderView.findViewById(R.id.xlistview_header_content);mHeaderTimeView=(TextView)mHeaderView.findViewById(R.id.xlistview_header_time);addHeaderView(mHeaderView);企业互联网服务系统V1.03//initfooterviewmFooterView=newXListViewFooter(context);//initheaderheightmHeaderView.getViewTreeObserver().addOnGlobalLayoutListener(newOnGlobalLayoutListener(){@OverridepublicvoidonGlobalLayout(){mHeaderViewHeight=mHeaderViewContent.getHeight();getViewTreeObserver().removeGlobalOnLayoutListener(this);}});}@OverridepublicvoidsetAdapter(ListAdapteradapter){//makesureXListViewFooteristhelastfooterview,andonlyaddonce.if(mIsFooterReady==false){mIsFooterReady=true;addFooterView(mFooterView);}super.setAdapter(adapter);}/***enableordisablepulldownrefreshfeature.**@paramenable*/publicvoidsetPullRefreshEnable(booleanenable){mEnablePullRefresh=enable;if(!mEnablePullRefresh){//disable,hidethecontentmHeaderViewContent.setVisibility(View.INVISIBLE);}else{mHeaderViewContent.setVisibility(View.VISIBLE);}}/***enableordisablepulluploadmorefeature.**@paramenable企业互联网服务系统V1.04*/publicvoidsetPullLoadEnable(booleanenable){mEnablePullLoad=enable;if(!mEnablePullLoad){mFooterView.hide();mFooterView.setOnClickListener(null);}else{mPullLoading=false;mFooterView.show();mFooterView.setState(XListViewFooter.STATE_NORMAL);//bothpullupandclickwillinvokeloadmore.mFooterView.setOnClickListener(newOnClickListener(){@OverridepublicvoidonClick(Viewv){startLoadMore();}});}}/***stoprefresh,resetheaderview.*/publicvoidstopRefresh(){if(mPullRefreshing==true){mPullRefreshing=false;resetHeaderHeight();}}/***stoploadmore,resetfooterview.*/publicvoidstopLoadMore(){if(mPullLoading==true){mPullLoading=false;mFooterView.setState(XListViewFooter.STATE_NORMAL);}mFooterView.setEnabled(true);}/***setlastrefreshtime企业互联网服务系统V1.05**@paramtime*/publicvoidsetRefreshTime(){//mHeaderTimeView.setText(newDate().toLocaleString());mHeaderTimeView.setText(date());}//获取系统时间publicStringdate(){SimpleDateFormatsdf=newSimpleDateFormat(yyyy-MM-ddHH:mm:ss);Datedate=newDate();Stringt=sdf.format(date);returnt;}privatevoidinvokeOnScrolling(){if(mScrollListenerinstanceofOnXScrollListener){OnXScrollListenerl=(OnXScrollListener)mScrollListener;l.onXScrolling(this);}}privatevoidupdateHeaderHeight(floatdelta){mHeaderView.setVisiableHeight((int)delta+mHeaderView.getVisiableHeight());if(mEnablePullRefresh&&!mPullRefreshing){//未处于刷新状态,更新箭头if(mHeaderView.getVisiableHeight()mHeaderViewHeight){mHeaderView.setState(XListViewHeader.STATE_READY);}else{mHeaderView.setState(XListViewHeader.STATE_NORMAL);}}setSelection(0);//scrolltotopeach