Android学习之listview的下拉刷新、上拉载入
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Android学习之listview的下拉刷新、上拉载入,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含22651字,纯文字阅读大概需要33分钟。
内容图文
本例是在上例的基础上完成的。本例实现的listview上拉载入、下拉刷新功能,是在开源网站上别人写好的listview,主要是对listview的控件进行重写,添加了footer和header。
1.listview_footer
listview_footer是listview的底部。
1 <? xml version="1.0" encoding="utf-8" ?> 2 < LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android" 3 android:layout_width ="fill_parent" 4 android:layout_height ="wrap_content" > 5 6 < RelativeLayout 7 android:id ="@+id/xlistview_footer_content" 8 android:layout_width ="fill_parent" 9 android:layout_height ="wrap_content" 10 android:padding ="10dp" > 11 12 < ProgressBar 13 android:id ="@+id/xlistview_footer_progressbar" 14 android:layout_width ="wrap_content" 15 android:layout_height ="wrap_content" 16 android:layout_centerInParent ="true" 17 android:visibility ="invisible" /> 18 19 < TextView 20 android:id ="@+id/xlistview_footer_hint_textview" 21 android:layout_width ="wrap_content" 22 android:layout_height ="wrap_content" 23 android:layout_centerInParent ="true" 24 android:text ="@string/xlistview_footer_hint_normal" /> 25 </ RelativeLayout > 26 27 </ LinearLayout >
listview_footer由ProgressBar和textview组成。用户点击textview或者上拉的时候,会触发监听事件,实现更多数据的载入。
1 public final static int STATE_NORMAL = 0; 2publicfinalstaticint STATE_READY = 1; 3publicfinalstaticint STATE_LOADING = 2; 4 5private Context mContext; 6 7private View mContentView; 8private View mProgressBar; 9private TextView mHintView; 1011public XListViewFooter(Context context) { 12super(context); 13 initView(context); 14 } 1516public XListViewFooter(Context context, AttributeSet attrs) { 17super(context, attrs); 18 initView(context); 19 } 202122publicvoid setState(int state) { 23 mHintView.setVisibility(View.INVISIBLE); 24 mProgressBar.setVisibility(View.INVISIBLE); 25 mHintView.setVisibility(View.INVISIBLE); 26if (state == STATE_READY) { 27 mHintView.setVisibility(View.VISIBLE); 28 mHintView.setText(R.string.xlistview_footer_hint_ready); 29 } elseif (state == STATE_LOADING) { 30 mProgressBar.setVisibility(View.VISIBLE); 31 } else { 32 mHintView.setVisibility(View.VISIBLE); 33 mHintView.setText(R.string.xlistview_footer_hint_normal); 34 } 35 } 3637publicvoid setBottomMargin(int height) { 38if (height < 0) return ; 39 LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)mContentView.getLayoutParams(); 40 lp.bottomMargin = height; 41 mContentView.setLayoutParams(lp); 42 } 4344publicint getBottomMargin() { 45 LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)mContentView.getLayoutParams(); 46return lp.bottomMargin; 47 } 4849/**50 * normal status 51*/52publicvoid normal() { 53 mHintView.setVisibility(View.VISIBLE); 54 mProgressBar.setVisibility(View.GONE); 55 } 565758/**59 * loading status 60*/61publicvoid loading() { 62 mHintView.setVisibility(View.GONE); 63 mProgressBar.setVisibility(View.VISIBLE); 64 } 6566/**67 * hide footer when disable pull load more 68*/69publicvoid hide() { 70 LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)mContentView.getLayoutParams(); 71 lp.height = 0; 72 mContentView.setLayoutParams(lp); 73 } 7475/**76 * show footer 77*/78publicvoid show() { 79 LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)mContentView.getLayoutParams(); 80 lp.height = LayoutParams.WRAP_CONTENT; 81 mContentView.setLayoutParams(lp); 82 } 8384privatevoid initView(Context context) { 85 mContext = context; 86 LinearLayout moreView = (LinearLayout)LayoutInflater.from(mContext).inflate(R.layout.xlistview_footer, null); 87 addView(moreView); 88 moreView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 8990 mContentView = moreView.findViewById(R.id.xlistview_footer_content); 91 mProgressBar = moreView.findViewById(R.id.xlistview_footer_progressbar); 92 mHintView = (TextView)moreView.findViewById(R.id.xlistview_footer_hint_textview); 93 }
后台代码定义了listview_footer的状态变化,包括加载、停止加载等,实现了对事件的监听。
2.listview_header
listview_header是listview显示的时候头部文件。用户下拉的时候,实现数据的重新载入。
1 <? xml version="1.0" encoding="utf-8" ?> 2 < LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android" 3 android:layout_width ="fill_parent" 4 android:layout_height ="wrap_content" 5 android:gravity ="bottom" > 6 7 < RelativeLayout 8 android:id ="@+id/xlistview_header_content" 9 android:layout_width ="fill_parent" 10 android:layout_height ="60dp" > 11 12 < LinearLayout 13 android:layout_width ="wrap_content" 14 android:layout_height ="wrap_content" 15 android:layout_centerInParent ="true" 16 android:gravity ="center" 17 android:orientation ="vertical" android:id ="@+id/xlistview_header_text" > 18 19 < TextView 20 android:id ="@+id/xlistview_header_hint_textview" 21 android:layout_width ="wrap_content" 22 android:layout_height ="wrap_content" 23 android:text ="@string/xlistview_header_hint_normal" /> 24 25 < LinearLayout 26 android:layout_width ="wrap_content" 27 android:layout_height ="wrap_content" 28 android:layout_marginTop ="3dp" > 29 30 < TextView 31 android:layout_width ="wrap_content" 32 android:layout_height ="wrap_content" 33 android:text ="@string/xlistview_header_last_time" 34 android:textSize ="12sp" /> 35 36 < TextView 37 android:id ="@+id/xlistview_header_time" 38 android:layout_width ="wrap_content" 39 android:layout_height ="wrap_content" 40 android:textSize ="12sp" /> 41 </ LinearLayout > 42 </ LinearLayout > 43 44 < ImageView 45 android:id ="@+id/xlistview_header_arrow" 46 android:layout_width ="wrap_content" 47 android:layout_height ="wrap_content" 48 android:layout_alignLeft ="@id/xlistview_header_text" 49 android:layout_centerVertical ="true" 50 android:layout_marginLeft ="-35dp" 51 android:src ="@drawable/xlistview_arrow" /> 52 53 < ProgressBar 54 android:id ="@+id/xlistview_header_progressbar" 55 android:layout_width ="30dp" 56 android:layout_height ="30dp" 57 android:layout_alignLeft ="@id/xlistview_header_text" 58 android:layout_centerVertical ="true" 59 android:layout_marginLeft ="-40dp" 60 android:visibility ="invisible" /> 61 </ RelativeLayout > 62 63 </ LinearLayout >
xml部分由textview和imageview组成。
1 private LinearLayout mContainer; 2 private ImageView mArrowImageView; 3 private ProgressBar mProgressBar; 4 private TextView mHintTextView; 5 private int mState = STATE_NORMAL; 6 7private Animation mRotateUpAnim; 8private Animation mRotateDownAnim; 9 10privatefinalint ROTATE_ANIM_DURATION = 180; 11 12publicfinalstaticint STATE_NORMAL = 0; 13publicfinalstaticint STATE_READY = 1; 14publicfinalstaticint STATE_REFRESHING = 2; 15 16public XListViewHeader(Context context) { 17super(context); 18 initView(context); 19 } 20 21/** 22 * @param context 23 * @param attrs 24*/ 25public XListViewHeader(Context context, AttributeSet attrs) { 26super(context, attrs); 27 initView(context); 28 } 29 30privatevoid initView(Context context) { 31// 鍒濆鎯呭喌锛岃缃笅鎷夊埛鏂皏iew楂樺害涓? 32 LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( 33 LayoutParams.FILL_PARENT, 0); 34 mContainer = (LinearLayout) LayoutInflater.from(context).inflate( 35 R.layout.xlistview_header, null); 36 addView(mContainer, lp); 37 setGravity(Gravity.BOTTOM); 38 39 mArrowImageView = (ImageView)findViewById(R.id.xlistview_header_arrow); 40 mHintTextView = (TextView)findViewById(R.id.xlistview_header_hint_textview); 41 mProgressBar = (ProgressBar)findViewById(R.id.xlistview_header_progressbar); 42 43 mRotateUpAnim = new RotateAnimation(0.0f, -180.0f, 44 Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 45 0.5f); 46 mRotateUpAnim.setDuration(ROTATE_ANIM_DURATION); 47 mRotateUpAnim.setFillAfter(true); 48 mRotateDownAnim = new RotateAnimation(-180.0f, 0.0f, 49 Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 50 0.5f); 51 mRotateDownAnim.setDuration(ROTATE_ANIM_DURATION); 52 mRotateDownAnim.setFillAfter(true); 53 } 54 55publicvoid setState(int state) { 56if (state == mState) return ; 57 58if (state == STATE_REFRESHING) { // 鏄剧ず杩涘害 59 mArrowImageView.clearAnimation(); 60 mArrowImageView.setVisibility(View.INVISIBLE); 61 mProgressBar.setVisibility(View.VISIBLE); 62 } else { // 鏄剧ず绠ご鍥剧墖 63 mArrowImageView.setVisibility(View.VISIBLE); 64 mProgressBar.setVisibility(View.INVISIBLE); 65 } 66 67switch(state){ 68case STATE_NORMAL: 69if (mState == STATE_READY) { 70 mArrowImageView.startAnimation(mRotateDownAnim); 71 } 72if (mState == STATE_REFRESHING) { 73 mArrowImageView.clearAnimation(); 74 } 75 mHintTextView.setText(R.string.xlistview_header_hint_normal); 76break; 77case STATE_READY: 78if (mState != STATE_READY) { 79 mArrowImageView.clearAnimation(); 80 mArrowImageView.startAnimation(mRotateUpAnim); 81 mHintTextView.setText(R.string.xlistview_header_hint_ready); 82 } 83break; 84case STATE_REFRESHING: 85 mHintTextView.setText(R.string.xlistview_header_hint_loading); 86break; 87default: 88 } 89 90 mState = state; 91 } 92 93publicvoid setVisiableHeight(int height) { 94if (height < 0) 95 height = 0; 96 LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mContainer 97 .getLayoutParams(); 98 lp.height = height; 99 mContainer.setLayoutParams(lp); 100 } 101102publicint getVisiableHeight() { 103return mContainer.getHeight(); 104 }
后台通过状态的改变,控制header的高度。
3.xlistview
xlistview是对listview控件的重写
1 public class XListView extends ListView implements OnScrollListener { 2 3privatefloat mLastY = -1; // save event y 4private Scroller mScroller; // used for scroll back 5private OnScrollListener mScrollListener; // user‘s scroll listener 6 7// the interface to trigger refresh and load more. 8private IXListViewListener mListViewListener; 9 10// -- header view 11private XListViewHeader mHeaderView; 12// header view content, use it to calculate the Header‘s height. And hide it 13// when disable pull refresh. 14private RelativeLayout mHeaderViewContent; 15private TextView mHeaderTimeView; 16privateint mHeaderViewHeight; // header view‘s height 17privateboolean mEnablePullRefresh = true; 18privateboolean mPullRefreshing = false; // is refreashing. 19 20// -- footer view 21private XListViewFooter mFooterView; 22privateboolean mEnablePullLoad; 23privateboolean mPullLoading; 24privateboolean mIsFooterReady = false; 25 26// total list items, used to detect is at the bottom of listview. 27privateint mTotalItemCount; 28 29// for mScroller, scroll back from header or footer. 30privateint mScrollBack; 31privatefinalstaticint SCROLLBACK_HEADER = 0; 32privatefinalstaticint SCROLLBACK_FOOTER = 1; 33 34privatefinalstaticint SCROLL_DURATION = 400; // scroll back duration 35privatefinalstaticint PULL_LOAD_MORE_DELTA = 50; // when pull up >= 50px 36// at bottom, trigger 37// load more. 38privatefinalstaticfloat OFFSET_RADIO = 1.8f; // support iOS like pull 39// feature. 40 41/** 42 * @param context 43*/ 44public XListView(Context context) { 45super(context); 46 initWithContext(context); 47 } 48 49public XListView(Context context, AttributeSet attrs) { 50super(context, attrs); 51 initWithContext(context); 52 } 53 54public XListView(Context context, AttributeSet attrs, int defStyle) { 55super(context, attrs, defStyle); 56 initWithContext(context); 57 } 58 59privatevoid initWithContext(Context context) { 60 mScroller = new Scroller(context, new DecelerateInterpolator()); 61// XListView need the scroll event, and it will dispatch the event to 62// user‘s listener (as a proxy). 63super.setOnScrollListener(this); 64 65// init header view 66 mHeaderView = new XListViewHeader(context); 67 mHeaderViewContent = (RelativeLayout) mHeaderView 68 .findViewById(R.id.xlistview_header_content); 69 mHeaderTimeView = (TextView) mHeaderView 70 .findViewById(R.id.xlistview_header_time); 71 addHeaderView(mHeaderView); 72 73// init footer view 74 mFooterView = new XListViewFooter(context); 75 76// init header height 77 mHeaderView.getViewTreeObserver().addOnGlobalLayoutListener( 78new OnGlobalLayoutListener() { 79 @SuppressWarnings("deprecation") 80 @Override 81publicvoid onGlobalLayout() { 82 mHeaderViewHeight = mHeaderViewContent.getHeight(); 83 getViewTreeObserver() 84 .removeGlobalOnLayoutListener(this); 85 } 86 }); 87 } 88 89 @Override 90publicvoid setAdapter(ListAdapter adapter) { 91// make sure XListViewFooter is the last footer view, and only add once. 92if (mIsFooterReady == false) { 93 mIsFooterReady = true; 94 addFooterView(mFooterView); 95 } 96super.setAdapter(adapter); 97 } 98 99/**100 * enable or disable pull down refresh feature. 101 * 102 * @param enable 103*/104publicvoid setPullRefreshEnable(boolean enable) { 105 mEnablePullRefresh = enable; 106if (!mEnablePullRefresh) { // disable, hide the content107 mHeaderViewContent.setVisibility(View.INVISIBLE); 108 } else { 109 mHeaderViewContent.setVisibility(View.VISIBLE); 110 } 111 } 112113/**114 * enable or disable pull up load more feature. 115 * 116 * @param enable 117*/118publicvoid setPullLoadEnable(boolean enable) { 119 mEnablePullLoad = enable; 120if (!mEnablePullLoad) { 121 mFooterView.hide(); 122 mFooterView.setOnClickListener(null); 123 } else { 124 mPullLoading = false; 125 mFooterView.show(); 126 mFooterView.setState(XListViewFooter.STATE_NORMAL); 127// both "pull up" and "click" will invoke load more.128 mFooterView.setOnClickListener(new OnClickListener() { 129 @Override 130publicvoid onClick(View v) { 131 startLoadMore(); 132 } 133 }); 134 } 135 } 136137/**138 * stop refresh, reset header view. 139*/140publicvoid stopRefresh() { 141if (mPullRefreshing == true) { 142 mPullRefreshing = false; 143 resetHeaderHeight(); 144 } 145 } 146147/**148 * stop load more, reset footer view. 149*/150publicvoid stopLoadMore() { 151if (mPullLoading == true) { 152 mPullLoading = false; 153 mFooterView.setState(XListViewFooter.STATE_NORMAL); 154 } 155 } 156157/**158 * set last refresh time 159 * 160 * @param time 161*/162publicvoid setRefreshTime(String time) { 163 mHeaderTimeView.setText(time); 164 } 165166privatevoid invokeOnScrolling() { 167if (mScrollListener instanceof OnXScrollListener) { 168 OnXScrollListener l = (OnXScrollListener) mScrollListener; 169 l.onXScrolling(this); 170 } 171 } 172173privatevoid updateHeaderHeight(float delta) { 174 mHeaderView.setVisiableHeight((int) delta 175 + mHeaderView.getVisiableHeight()); 176if (mEnablePullRefresh && !mPullRefreshing) { // 鏈浜庡埛鏂扮姸鎬侊紝鏇存柊绠ご177if (mHeaderView.getVisiableHeight() > mHeaderViewHeight) { 178 mHeaderView.setState(XListViewHeader.STATE_READY); 179 } else { 180 mHeaderView.setState(XListViewHeader.STATE_NORMAL); 181 } 182 } 183 setSelection(0); // scroll to top each time184 } 185186/**187 * reset header view‘s height. 188*/189privatevoid resetHeaderHeight() { 190int height = mHeaderView.getVisiableHeight(); 191if (height == 0) // not visible.192return; 193// refreshing and header isn‘t shown fully. do nothing.194if (mPullRefreshing && height <= mHeaderViewHeight) { 195return; 196 } 197int finalHeight = 0; // default: scroll back to dismiss header. 198// is refreshing, just scroll back to show all the header.199if (mPullRefreshing && height > mHeaderViewHeight) { 200 finalHeight = mHeaderViewHeight; 201 } 202 mScrollBack = SCROLLBACK_HEADER; 203 mScroller.startScroll(0, height, 0, finalHeight - height, 204 SCROLL_DURATION); 205// trigger computeScroll206 invalidate(); 207 } 208209privatevoid updateFooterHeight(float delta) { 210int height = mFooterView.getBottomMargin() + (int) delta; 211if (mEnablePullLoad && !mPullLoading) { 212if (height > PULL_LOAD_MORE_DELTA) { // height enough to invoke load 213// more.214 mFooterView.setState(XListViewFooter.STATE_READY); 215 } else { 216 mFooterView.setState(XListViewFooter.STATE_NORMAL); 217 } 218 } 219 mFooterView.setBottomMargin(height); 220221// setSelection(mTotalItemCount - 1); // scroll to bottom222 } 223224privatevoid resetFooterHeight() { 225int bottomMargin = mFooterView.getBottomMargin(); 226if (bottomMargin > 0) { 227 mScrollBack = SCROLLBACK_FOOTER; 228 mScroller.startScroll(0, bottomMargin, 0, -bottomMargin, 229 SCROLL_DURATION); 230 invalidate(); 231 } 232 } 233234privatevoid startLoadMore() { 235 mPullLoading = true; 236 mFooterView.setState(XListViewFooter.STATE_LOADING); 237if (mListViewListener != null) { 238 mListViewListener.onLoadMore(); 239 } 240 } 241242 @Override 243publicboolean onTouchEvent(MotionEvent ev) { 244if (mLastY == -1) { 245 mLastY = ev.getRawY(); 246 } 247248switch (ev.getAction()) { 249case MotionEvent.ACTION_DOWN: 250 mLastY = ev.getRawY(); 251break; 252case MotionEvent.ACTION_MOVE: 253finalfloat deltaY = ev.getRawY() - mLastY; 254 mLastY = ev.getRawY(); 255 System.out.println(getFirstVisiblePosition() + "---->" 256 + getLastVisiblePosition()); 257if (getFirstVisiblePosition() == 0 258 && (mHeaderView.getVisiableHeight() > 0 || deltaY > 0)) { 259// the first item is showing, header has shown or pull down.260 updateHeaderHeight(deltaY / OFFSET_RADIO); 261 invokeOnScrolling(); 262 } elseif (getLastVisiblePosition() == mTotalItemCount - 1 263 && (mFooterView.getBottomMargin() > 0 || deltaY < 0)) { 264// last item, already pulled up or want to pull up.265 updateFooterHeight(-deltaY / OFFSET_RADIO); 266 } 267break; 268default: 269 mLastY = -1; // reset270if (getFirstVisiblePosition() == 0) { 271// invoke refresh272if (mEnablePullRefresh 273 && mHeaderView.getVisiableHeight() > mHeaderViewHeight) { 274 mPullRefreshing = true; 275 mHeaderView.setState(XListViewHeader.STATE_REFRESHING); 276if (mListViewListener != null) { 277 mListViewListener.onRefresh(); 278 } 279 } 280 resetHeaderHeight(); 281 } 282if (getLastVisiblePosition() == mTotalItemCount - 1) { 283// invoke load more.284if (mEnablePullLoad 285 && mFooterView.getBottomMargin() > PULL_LOAD_MORE_DELTA) { 286 startLoadMore(); 287 } 288 resetFooterHeight(); 289 } 290break; 291 } 292returnsuper.onTouchEvent(ev); 293 } 294295 @Override 296publicvoid computeScroll() { 297if (mScroller.computeScrollOffset()) { 298if (mScrollBack == SCROLLBACK_HEADER) { 299 mHeaderView.setVisiableHeight(mScroller.getCurrY()); 300 } else { 301 mFooterView.setBottomMargin(mScroller.getCurrY()); 302 } 303 postInvalidate(); 304 invokeOnScrolling(); 305 } 306super.computeScroll(); 307 } 308309 @Override 310publicvoid setOnScrollListener(OnScrollListener l) { 311 mScrollListener = l; 312 } 313314 @Override 315publicvoid onScrollStateChanged(AbsListView view, int scrollState) { 316if (mScrollListener != null) { 317 mScrollListener.onScrollStateChanged(view, scrollState); 318 } 319 } 320321 @Override 322publicvoid onScroll(AbsListView view, int firstVisibleItem, 323int visibleItemCount, int totalItemCount) { 324// send to user‘s listener325 mTotalItemCount = totalItemCount; 326if (mScrollListener != null) { 327 mScrollListener.onScroll(view, firstVisibleItem, visibleItemCount, 328 totalItemCount); 329 } 330 } 331332publicvoid setXListViewListener(IXListViewListener l) { 333 mListViewListener = l; 334 } 335336/**337 * you can listen ListView.OnScrollListener or this one. it will invoke 338 * onXScrolling when header/footer scroll back. 339*/340publicinterface OnXScrollListener extends OnScrollListener { 341publicvoid onXScrolling(View view); 342 } 343344/**345 * implements this interface to get refresh/load more event. 346*/347publicinterface IXListViewListener { 348publicvoid onRefresh(); 349350publicvoid onLoadMore(); 351 } 352 }
在xlistview中定义了一个事件接口,来监听加载和刷新。
4.mainactivity
1 protected void onCreate(Bundle savedInstanceState) { 2 super .onCreate(savedInstanceState); 3 setContentView(R.layout.activity_main); 4 handler=new Handler(); 5 sqlhelper=new SQLiteHelper(MainActivity.this, "test", null, 1); 6 list=new ArrayList<PersonInfo>(); 7 getData(); 8 listview=(XListView)findViewById(R.id.listView1); 9 registerForContextMenu(listview); 10 listview.setPullLoadEnable(true); 11// View moreview=getLayoutInflater().inflate(R.layout.moredata, null); 12//listview.addFooterView(moreview);13 adapter=new MyAdapter(this); 14 listview.setAdapter(adapter); 15 listview.setXListViewListener(this); 16 listview.setOnItemLongClickListener(new OnItemLongClickListener() { 17 @Override 18publicboolean onItemLongClick(AdapterView<?> arg0, View arg1, 19int arg2, long arg3) { 20// TODO Auto-generated method stub21 PersonInfo person=list.get(arg2); 22 delname=person.getName(); 23returnfalse; 24 } 25 }); 2627/* btnpro=(Button)moreview.findViewById(R.id.bt_load); 28 pg=(ProgressBar)moreview.findViewById(R.id.pg); 29 btnpro.setOnClickListener(new View.OnClickListener() { 3031 @Override 32 public void onClick(View v) { 33 // TODO Auto-generated method stub 34 pg.setVisibility(View.VISIBLE);// 将进度条可见 35 btnpro.setVisibility(View.GONE);// 按钮不可见 36 Handler hanldre=new Handler(); 37 hanldre.postDelayed(new Runnable(){ 3839 @Override 40 public void run() { 41 // TODO Auto-generated method stub 4243 adapter.count+=5; 4445 btnpro.setVisibility(View.VISIBLE); 46 pg.setVisibility(View.GONE); 47 if(adapter.count>list.size()) 4849 { 50 adapter. count=list.size(); 51 btnpro.setVisibility(View.INVISIBLE); 52 } 53 adapter.notifyDataSetChanged(); 54 }}, 2000); 55 } 56 });*/5758 } 59privatevoid getData() 60 { 61 Cursor cur=sqlhelper.getReadableDatabase().query("person", null, null, null, null, null, null); 62if(cur.moveToFirst()) 63 { 64while(cur.moveToNext()) 65 { 66 PersonInfo person=new PersonInfo(); 67 person.name=cur.getString(cur.getColumnIndex("name")); 68 person.age=cur.getInt(cur.getColumnIndex("age")); 69 list.add(person); 70 } 71 } 72 } 73privatevoid onLoad() { 74 listview.stopRefresh(); 75 listview.stopLoadMore(); 76 SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss"); 77 String strTime = timeFormat.format(new java.util.Date()); 78 listview.setRefreshTime(strTime); 79 }
1 @Override 2 public void onRefresh() { 3 // TODO Auto-generated method stub 4 handler.postDelayed(new Runnable(){ 5 6 @Override 7publicvoid run() { 8// TODO Auto-generated method stub 9if(list!=null &&list.size()>0) 10 { 11 list.clear(); 12 getData(); 13 adapter.notifyDataSetChanged(); 14 onLoad(); 15 } 16 }}, 2000); 17 } 18 @Override 19publicvoid onLoadMore() { 20// TODO Auto-generated method stub2122 handler.postDelayed(new Runnable(){ 2324 @Override 25publicvoid run() { 26// TODO Auto-generated method stub27 adapter.count+=5; 2829if(adapter.count>list.size()) 3031 { 32 adapter. count=list.size(); 33 } 34 adapter.notifyDataSetChanged(); 35 onLoad(); 36 }}, 2000); 37 }
刷新和加载都是新建了一个handler。在handler中执行。
5.demo
原文:http://www.cnblogs.com/ggz19/p/3897478.html
内容总结
以上是互联网集市为您收集整理的Android学习之listview的下拉刷新、上拉载入全部内容,希望文章能够帮你解决Android学习之listview的下拉刷新、上拉载入所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。