Android分页控件xlistview
strings.xml
-------------------------------------------------------------------------------------------------------------------------------
<?xmlversion="1.0"encoding="utf-8"?>
<resources>
<stringname="hello">HelloWorld,XListViewActivity!</string>
<stringname="app_name">XListView</string>
<stringname="xlistview_header_hint_normal">下拉刷新</string>
<stringname="xlistview_header_hint_ready">松开刷新数据</string>
<stringname="xlistview_header_hint_loading">正在加载...</string>
<stringname="xlistview_header_last_time">上次更新时间:</string>
<stringname="xlistview_footer_hint_normal">查看更多</string>
<stringname="xlistview_footer_hint_ready">松开载入更多</string>
</resources>
activity_main.xml
-------------------------------------------------------------------------------------------------------------------------------
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"android:background="#f0f0f0">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"/>
<com.xlistview.view.XListView
android:id="@+id/xListView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"android:cacheColorHint="#00000000">
</com.xlistview.view.XListView>
</LinearLayout>
xlistview_header.xml
-------------------------------------------------------------------------------------------------------------------------------
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="bottom">
<RelativeLayout
android:id="@+id/xlistview_header_content"
android:layout_width="fill_parent"
android:layout_height="60dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:orientation="vertical"android:id="@+id/xlistview_header_text">
<TextView
android:id="@+id/xlistview_header_hint_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/xlistview_header_hint_normal"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="3dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/xlistview_header_last_time"
android:textSize="12sp"/>
<TextView
android:id="@+id/xlistview_header_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"/>
</LinearLayout>
</LinearLayout>
<ImageView
android:id="@+id/xlistview_header_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/xlistview_header_text"
android:layout_centerVertical="true"
android:layout_marginLeft="-35dp"
android:src="@drawable/xlistview_arrow"/>
<ProgressBar
android:id="@+id/xlistview_header_progressbar"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignLeft="@id/xlistview_header_text"
android:layout_centerVertical="true"
android:layout_marginLeft="-40dp"
android:visibility="invisible"/>
</RelativeLayout>
</LinearLayout>
|
|
xlistview_footer.xml
-------------------------------------------------------------------------------------------------------------------------------
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="@+id/xlistview_footer_content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<ProgressBar
android:id="@+id/xlistview_footer_progressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="invisible"/>
<TextView
android:id="@+id/xlistview_footer_hint_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/xlistview_footer_hint_normal"/>
</RelativeLayout>
</LinearLayout>
|
|
list_item.xml
-------------------------------------------------------------------------------------------------------------------------------
<?xmlversion="1.0"encoding="utf-8"?>
<TextViewxmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_item_textview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp"
android:textColor="#000"
android:textSize="16sp">
</TextView>
XListView.java
-------------------------------------------------------------------------------------------------------------------------------
packagecom.xlistview.view;
importcom.xlistview.R;
importandroid.annotation.SuppressLint;
importandroid.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;
/**
*
*项目名称:XListView
*类名称:XListView
*类描述:
*AnListViewsupport(a)Pulldowntorefresh,(b)Pulluptoloadmore.
*ImplementIXListViewListener,andseestopRefresh()/stopLoadMore().
*创建人:fy
*创建时间:2014-3-16下午5:16:28
*Copyright(c)方勇-版权所有
*/
publicclassXListViewextendsListViewimplementsOnScrollListener{
privatefloatmLastY=-1;//saveeventy
privateScrollermScroller;//usedforscrollback
privateOnScrollListenermScrollListener;//user'sscrolllistener
//theinterfacetotriggerrefreshandloadmore.
privateIXListViewListenermListViewListener;
//--headerview
privateXListViewHeadermHeaderView;
//headerviewcontent,useittocalculatetheHeader'sheight.Andhideit
//whendisablepullrefresh.
privateRelativeLayoutmHeaderViewContent;
privateTextViewmHeaderTimeView;
privateintmHeaderViewHeight;//headerview'sheight
privatebooleanmEnablePullRefresh=true;
privatebooleanmPullRefreshing=false;//isrefreashing.
//--footerview
privateXListViewFootermFooterView;
privatebooleanmEnablePullLoad;
privatebooleanmPullLoading;
privatebooleanmIsFooterReady=false;
//totallistitems,usedtodetectisatthebottomoflistview.
privateintmTotalItemCount;
//formScroller,scrollbackfromheaderorfooter.
privateintmScrollBack;
privatefinalstaticintSCROLLBACK_HEADER=0;
privatefinalstaticintSCROLLBACK_FOOTER=1;
privatefinalstaticintSCROLL_DURATION=400;//scrollbackduration
privatefinalstaticintPULL_LOAD_MORE_DELTA=50;//whenpullup>=50px
//atbottom,trigger
//loadmore.
privatefinalstaticfloatOFFSET_RADIO=1.8f;//supportiOSlikepull
//feature.
/**
*@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);
}
@SuppressLint("NewApi")
privatevoidinitWithContext(Contextcontext){
mScroller=newScroller(context,newDecelerateInterpolator());
//XListViewneedthescrollevent,anditwilldispatchtheeventto
//user'slistener(asaproxy).
super.setOnScrollListener(this);
//initheaderview
mHeaderView=newXListViewHeader(context);
mHeaderViewContent=(RelativeLayout)mHeaderView.findViewById(R.id.xlistview_header_content);
mHeaderTimeView=(TextView)mHeaderView.findViewById(R.id.xlistview_header_time);
addHeaderView(mHeaderView);
//initfooterview
mFooterView=newXListViewFooter(context);
//initheaderheight
mHeaderView.getViewTreeObserver().addOnGlobalLayoutListener(newOnGlobalLayoutListener(){
@Override
publicvoidonGlobalLayout(){
mHeaderViewHeight=mHeaderViewContent.getHeight();
//getViewTreeObserver().removeOnGlobalLayoutListener(this);
getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
});
}
@Override
publicvoidsetAdapter(ListAdapteradapter){
//makesureXListViewFooteristhelastfooterview,andonlyaddonce.
if(mIsFooterReady==false){
mIsFooterReady=true;
addFooterView(mFooterView);
}
super.setAdapter(adapter);
}
/**
*enableordisablepulldownrefreshfeature.
*
*@paramenable
*/
publicvoidsetPullRefreshEnable(booleanenable){
mEnablePullRefresh=enable;
if(!mEnablePullRefresh){//disable,hidethecontent
mHeaderViewContent.setVisibility(View.INVISIBLE);
}else{
mHeaderViewContent.setVisibility(View.VISIBLE);
}
}
/**
*enableordisablepulluploadmorefeature.
*
*@paramenable
*/
publicvoidsetPullLoadEnable(booleanenable){
mEnablePullLoad=enable;
if(!mEnablePullLoad){
mFooterView.hide();
mFooterView.setOnClickListener(null);
}else{
mPullLoading=false;
mFooterView.show();
mFooterView.setState(XListViewFooter.STATE_NORMAL);
//both"pullup"and"click"willinvokeloadmore.
mFooterView.setOnClickListener(newOnClickListener(){
@Override
publicvoidonClick(Viewv){
startLoadMore();
}
});
}
}
/**
*stoprefresh,resetheaderview.
*/
publicvoidstopRefresh(){
if(mPullRefreshing==true){
mPullRefreshing=false;
resetHeaderHeight();
}
}
/**
*stoploadmore,resetfooterview.
*/
publicvoidstopLoadMore(){
if(mPullLoading==true){
mPullLoading=false;
mFooterView.setState(XListViewFooter.STATE_NORMAL);
}
}
/**
*setlastrefreshtime
*
*@paramtime
*/
publicvoidsetRefreshTime(Stringtime){
mHeaderTimeView.setText(time);
}
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);//scrolltotopeachtime
}
/**
*resetheaderview'sheight.
*/
privatevoidresetHeaderHeight(){
intheight=mHeaderView.getVisiableHeight();
if(height==0)//notvisible.
return;
//refreshingandheaderisn'tshownfully.donothing.
if(mPullRefreshing&&height<=mHeaderViewHeight){
return;
}
intfinalHeight=0;//default:scrollbacktodismissheader.
//isrefreshing,justscrollbacktoshowalltheheader.
if(mPullRefreshing&&height>mHeaderViewHeight){
finalHeight=mHeaderViewHeight;
}
mScrollBack=SCROLLBACK_HEADER;
mScroller.startScroll(0,height,0,finalHeight-height,SCROLL_DURATION);
//triggercomputeScroll
invalidate();
}
privatevoidupdateFooterHeight(floatdelta){
intheight=mFooterView.getBottomMargin()+(int)delta;
if(mEnablePullLoad&&!mPullLoading){
if(height>PULL_LOAD_MORE_DELTA){//heightenoughtoinvokeload
//more.
mFooterView.setState(XListViewFooter.STATE_READY);
}else{
mFooterView.setState(XListViewFooter.STATE_NORMAL);
}
}
mFooterView.setBottomMargin(height);
//setSelection(mTotalItemCount-1);//scrolltobottom
}
privatevoidresetFooterHeight(){
intbottomMargin=mFooterView.getBottomMargin();
if(bottomMargin>0){
mScrollBack=SCROLLBACK_FOOTER;
mScroller.startScroll(0,bottomMargin,0,-bottomMargin,SCROLL_DURATION);
invalidate();
}
}
privatevoidstartLoadMore(){
mPullLoading=true;
mFooterView.setState(XListViewFooter.STATE_LOADING);
if(mListViewListener!=null){
mListViewListener.onLoadMore();
}
}
@Override
publicbooleanonTouchEvent(MotionEventev){
if(mLastY==-1){
mLastY=ev.getRawY();
}
switch(ev.getAction()){
caseMotionEvent.ACTION_DOWN:
mLastY=ev.getRawY();
break;
caseMotionEvent.ACTION_MOVE:
finalfloatdeltaY=ev.getRawY()-mLastY;
mLastY=ev.getRawY();
if(getFirstVisiblePosition()==0&&(mHeaderView.getVisiableHeight()>0||deltaY>0)){
//thefirstitemisshowing,headerhasshownorpulldown.
updateHeaderHeight(deltaY/OFFSET_RADIO);
invokeOnScrolling();
}elseif(getLastVisiblePosition()==mTotalItemCount-1&&(mFooterView.getBottomMargin()>0||deltaY<0)){
//lastitem,alreadypulleduporwanttopullup.
updateFooterHeight(-deltaY/OFFSET_RADIO);
}
break;
default:
mLastY=-1;//reset
if(getFirstVisiblePosition()==0){
//invokerefresh
if(mEnablePullRefresh&&mHeaderView.getVisiableHeight()>mHeaderViewHeight){
mPullRefreshing=true;
mHeaderView.setState(XListViewHeader.STATE_REFRESHING);
if(mListViewListener!=null){
mListViewListener.onRefresh();
}
}
resetHeaderHeight();
}elseif(getLastVisiblePosition()==mTotalItemCount-1){
//invokeloadmore.
if(mEnablePullLoad&&mFooterView.getBottomMargin()>PULL_LOAD_MORE_DELTA){
startLoadMore();
}
resetFooterHeight();
}
break;
}
returnsuper.onTouchEvent(ev);
}
@Override
publicvoidcomputeScroll(){
if(mScroller.computeScrollOffset()){
if(mScrollBack==SCROLLBACK_HEADER){
mHeaderView.setVisiableHeight(mScroller.getCurrY());
}else{
mFooterView.setBottomMargin(mScroller.getCurrY());
}
postInvalidate();
invokeOnScrolling();
}
super.computeScroll();
}
@Override
publicvoidsetOnScrollListener(OnScrollListenerl){
mScrollListener=l;
}
@Override
publicvoidonScrollStateChanged(AbsListViewview,intscrollState){
if(mScrollListener!=null){
mScrollListener.onScrollStateChanged(view,scrollState);
}
}
@Override
publicvoidonScroll(AbsListViewview,intfirstVisibleItem,intvisibleItemCount,inttotalItemCount){
//sendtouser'slistener
mTotalItemCount=totalItemCount;
if(mScrollListener!=null){
mScrollListener.onScroll(view,firstVisibleItem,visibleItemCount,totalItemCount);
}
}
publicvoidsetXListViewListener(IXListViewListenerl){
mListViewListener=l;
}
/**
*youcanlistenListView.OnScrollListenerorthisone.itwillinvoke
*onXScrollingwhenheader/footerscrollback.
*/
publicinterfaceOnXScrollListenerextendsOnScrollListener{
publicvoidonXScrolling(Viewview);
}
/**
*implementsthisinterfacetogetrefresh/loadmoreevent.
*/
publicinterfaceIXListViewListener{
publicvoidonRefresh();
publicvoidonLoadMore();
}
}
XListViewHeader.java
-------------------------------------------------------------------------------------------------------------------------------
packagecom.xlistview.view;
importcom.xlistview.R;
importandroid.content.Context;
importandroid.util.AttributeSet;
importandroid.view.Gravity;
importandroid.view.LayoutInflater;
importandroid.view.View;
importandroid.view.animation.Animation;
importandroid.view.animation.RotateAnimation;
importandroid.widget.ImageView;
importandroid.widget.LinearLayout;
importandroid.widget.ProgressBar;
importandroid.widget.TextView;
/**
*
*项目名称:XListView
*类名称:XListViewHeader
*类描述:自定义tbar布局
*创建人:fy
*创建时间:2014-3-16下午4:21:36
*Copyright(c)方勇-版权所有
*/
publicclassXListViewHeaderextendsLinearLayout{
/*tbar容器*/
privateLinearLayoutmContainer;
/*tbar容器->图标*/
privateImageViewmArrowImageView;
/*tbar容器->进度条*/
privateProgressBarmProgressBar;
/*tbar容器->文本*/
privateTextViewmHintTextView;
privateintmState=STATE_NORMAL;
privateAnimationmRotateUpAnim;
privateAnimationmRotateDownAnim;
privatefinalintROTATE_ANIM_DURATION=180;
/*1、无动画*/
publicfinalstaticintSTATE_NORMAL=0;
/*3、显示箭头图标*/
publicfinalstaticintSTATE_READY=1;
/*2、显示进度条*/
publicfinalstaticintSTATE_REFRESHING=2;
publicXListViewHeader(Contextcontext){
super(context);
initView(context);
}
publicXListViewHeader(Contextcontext,AttributeSetattrs){
super(context,attrs);
initView(context);
}
privatevoidinitView(Contextcontext){
//初始情况,设置下拉刷新view高度为0
LinearLayout.LayoutParamslp=newLinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,0);
mContainer=(LinearLayout)LayoutInflater.from(context).inflate(R.layout.xlistview_header,null);
/*Addsachildviewwiththespecifiedlayoutparameters.*/
addView(mContainer,lp);
/*Describeshowthechildviewsarepositioned.DefaultstoGRAVITY_TOP*/
setGravity(Gravity.BOTTOM);
mArrowImageView=(ImageView)findViewById(R.id.xlistview_header_arrow);
mHintTextView=(TextView)findViewById(R.id.xlistview_header_hint_textview);
mProgressBar=(ProgressBar)findViewById(R.id.xlistview_header_progressbar);
//旋转动画
mRotateUpAnim=newRotateAnimation(0.0f,-180.0f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
mRotateUpAnim.setDuration(ROTATE_ANIM_DURATION);
mRotateUpAnim.setFillAfter(true);
mRotateDownAnim=newRotateAnimation(-180.0f,0.0f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
mRotateDownAnim.setDuration(ROTATE_ANIM_DURATION);
mRotateDownAnim.setFillAfter(true);
}
publicvoidsetState(intstate){
if(state==mState)
return;
if(state==STATE_REFRESHING){//显示进度
mArrowImageView.clearAnimation();
mArrowImageView.setVisibility(View.INVISIBLE);
mProgressBar.setVisibility(View.VISIBLE);
}else{//显示箭头图片
mArrowImageView.setVisibility(View.VISIBLE);
mProgressBar.setVisibility(View.INVISIBLE);
}
//三个状态的动画效果切换
switch(state){
caseSTATE_NORMAL:
if(mState==STATE_READY){
mArrowImageView.startAnimation(mRotateDownAnim);
}
if(mState==STATE_REFRESHING){
mArrowImageView.clearAnimation();
}
mHintTextView.setText(R.string.xlistview_header_hint_normal);
break;
caseSTATE_READY:
if(mState!=STATE_READY){
mArrowImageView.clearAnimation();
mArrowImageView.startAnimation(mRotateUpAnim);
mHintTextView.setText(R.string.xlistview_header_hint_ready);
}
break;
caseSTATE_REFRESHING:
mHintTextView.setText(R.string.xlistview_header_hint_loading);
break;
default:
}
mState=state;
}
publicvoidsetVisiableHeight(intheight){
if(height<0)
height=0;
LinearLayout.LayoutParamslp=(LinearLayout.LayoutParams)mContainer.getLayoutParams();
lp.height=height;
mContainer.setLayoutParams(lp);
}
publicintgetVisiableHeight(){
returnmContainer.getHeight();
}
}
XListViewFooter.java
-------------------------------------------------------------------------------------------------------------------------------
packagecom.xlistview.view;
importcom.xlistview.R;
importandroid.content.Context;
importandroid.util.AttributeSet;
importandroid.view.LayoutInflater;
importandroid.view.View;
importandroid.widget.LinearLayout;
importandroid.widget.TextView;
/**
*
*项目名称:XListView
*类名称:XListViewFooter
*类描述:自定义bbar布局
*创建人:fy
*创建时间:2014-3-16下午5:02:38
*Copyright(c)方勇-版权所有
*/
publicclassXListViewFooterextendsLinearLayout{
/*1、无动画*/
publicfinalstaticintSTATE_NORMAL=0;
/*3、显示箭头图标*/
publicfinalstaticintSTATE_READY=1;
/*2、显示进度条*/
publicfinalstaticintSTATE_LOADING=2;
privateContextmContext;
/*bbar容器*/
privateViewmContentView;
/*bbar容器->进度条*/
privateViewmProgressBar;
/*bbar容器->文本*/
privateTextViewmHintView;
publicXListViewFooter(Contextcontext){
super(context);
initView(context);
}
publicXListViewFooter(Contextcontext,AttributeSetattrs){
super(context,attrs);
initView(context);
}
publicvoidsetState(intstate){
mHintView.setVisibility(View.INVISIBLE);
mProgressBar.setVisibility(View.INVISIBLE);
mHintView.setVisibility(View.INVISIBLE);
//手指向上滑动,显示松开载入更多
if(state==STATE_READY){
mHintView.setVisibility(View.VISIBLE);
mHintView.setText(R.string.xlistview_footer_hint_ready);
}elseif(state==STATE_LOADING){//手指点击查看更多,显示进度条
mProgressBar.setVisibility(View.VISIBLE);
}else{//查看更多
mHintView.setVisibility(View.VISIBLE);
mHintView.setText(R.string.xlistview_footer_hint_normal);
}
}
/*设置底部布局高度*/
publicvoidsetBottomMargin(intheight){
if(height<0)
return;
LinearLayout.LayoutParamslp=(LinearLayout.LayoutParams)mContentView.getLayoutParams();
lp.bottomMargin=height;
mContentView.setLayoutParams(lp);
}
/*获取底部布局高度*/
publicintgetBottomMargin(){
LinearLayout.LayoutParamslp=(LinearLayout.LayoutParams)mContentView.getLayoutParams();
returnlp.bottomMargin;
}
/**
*normalstatus
*/
publicvoidnormal(){
mHintView.setVisibility(View.VISIBLE);
mProgressBar.setVisibility(View.GONE);
}
/**
*loadingstatus
*/
publicvoidloading(){
mHintView.setVisibility(View.GONE);
mProgressBar.setVisibility(View.VISIBLE);
}
/**
*hidefooterwhendisablepullloadmore
*/
publicvoidhide(){
LinearLayout.LayoutParamslp=(LinearLayout.LayoutParams)mContentView.getLayoutParams();
lp.height=0;
mContentView.setLayoutParams(lp);
}
/**
*showfooter
*/
publicvoidshow(){
LinearLayout.LayoutParamslp=(LinearLayout.LayoutParams)mContentView.getLayoutParams();
lp.height=LayoutParams.WRAP_CONTENT;
mContentView.setLayoutParams(lp);
}
privatevoidinitView(Contextcontext){
mContext=context;
LinearLayoutmoreView=(LinearLayout)LayoutInflater.from(mContext).inflate(R.layout.xlistview_footer,null);
addView(moreView);
moreView.setLayoutParams(newLinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
mContentView=moreView.findViewById(R.id.xlistview_footer_content);
mProgressBar=moreView.findViewById(R.id.xlistview_footer_progressbar);
mHintView=(TextView)moreView.findViewById(R.id.xlistview_footer_hint_textview);
}
}
MainActivity.java
-------------------------------------------------------------------------------------------------------------------------------
packagecom.xlistview;
importjava.util.ArrayList;
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.os.Handler;
importandroid.widget.ArrayAdapter;
importcom.xlistview.view.XListView;
importcom.xlistview.view.XListView.IXListViewListener;
/**
*
*项目名称:com.xlistview
*类名称:MainActivity
*类描述:分页版listview
*创建人:fy
*创建时间:2014-3-18下午2:17:47
*Copyright(c)方勇-版权所有
*/
publicclassMainActivityextendsActivityimplementsIXListViewListener{
/*分页列表控件*/
privateXListViewmListView;
/*分页列表控件-->适配器*/
privateArrayAdapter<String>mAdapter;
/*分页列表控件-->适配器数据*/
privateArrayList<String>items=newArrayList<String>();
/*在子线程中异步更新UI数据*/
privateHandlermHandler;
/*总记录数*/
privateintstart=0;
/*递增变量*/
privatestaticintrefreshCnt=0;
/**Calledwhentheactivityisfirstcreated.*/
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
geneItems();
findViews();
setListeners();
initView();
mHandler=newHandler();
}
privatevoidfindViews(){
mListView=(XListView)findViewById(R.id.xListView);
}
privatevoidsetListeners(){
mListView.setXListViewListener(this);
}
privatevoidinitView(){
mListView.setPullLoadEnable(true);
mAdapter=newArrayAdapter<String>(this,R.layout.list_item,items);
mListView.setAdapter(mAdapter);
}
/*默认20条数据为一页*/
privatevoidgeneItems(){
for(inti=0;i!=20;++i){
items.add("refreshcnt"+(++start));
}
}
/*停止tbar、bbar,更新时间*/
privatevoidonLoad(){
mListView.stopRefresh();
mListView.stopLoadMore();
mListView.setRefreshTime("刚刚");
}
/*tbar刷新数据*/
@Override
publicvoidonRefresh(){
mHandler.postDelayed(newRunnable(){
@Override
publicvoidrun(){
start=++refreshCnt;
items.clear();
geneItems();
//mAdapter.notifyDataSetChanged();
mAdapter=newArrayAdapter<String>(MainActivity.this,R.layout.list_item,items);
mListView.setAdapter(mAdapter);
onLoad();
}
},2000);
}
/*bbar更新更多*/
@Override
publicvoidonLoadMore(){
mHandler.postDelayed(newRunnable(){
@Override
publicvoidrun(){
geneItems();
//局部适配器数据
mAdapter.notifyDataSetChanged();
onLoad();
}
},2000);
}
}
效果
-------------------------------------------------------------------------------------------------------------------------------
<!--EndFragment-->
更多相关文章
- Android(安卓)WIFI DEMO(扫描附近WIFI强度)
- Android(安卓)学习记录-调试输出
- Android(安卓)studio 页面布局无法显示问题
- Android登录界面开发及响应;页面跳转;传参
- WindowManger
- Android(安卓)APIDemo gif图片显示
- Android(安卓)WebView相关属性
- android显示和隐藏键盘
- Android(安卓)全屏显示实例