生活随笔
收集整理的這篇文章主要介紹了
精确监听AbsListView滚动至底部
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
? 用戶使用android客戶端時,當ListView滾動至底部,可以由一個按鈕來提示用戶是否讀下一頁,那么如果使用GridView呢?現在很多WEB 2.0上的體驗就是當底部時自動讀取下一頁數據,GridView(ListView也可)可以采用這種方法。網上已經有很多文章介紹了如何判斷ListView是否滾動至底部,原理是 AbsListView.getLastVisiblePosition() = (AbsListView.getCount() - 1) 即到底,如果往上拖一點,用戶看起來已經離開底部,但實際上 AbsListView.getLastVisiblePosition()?== (AbsListView.getCount() - 1) 依然成立,會導致誤判斷。本文在它們基礎上加以改進,做到更精確地監聽是否滾動至底部。先來看看本文程序運行的效果:
?
本文在?Android入門第八篇之GridView(九宮圖)基礎上加入滾到事件判斷,文件名為AutoLoadListener.java,原理是在AbsListView.getLastVisiblePosition() = =(AbsListView.getCount() - 1) 時,保存最后一個Item的絕對坐標,如果兩次獲取的絕對Y值都一樣,即到底然后執行回調函數......源碼如下:
[java]?view plaincopyprint?
<span?style="font-family:Comic?Sans?MS;">package?com.testScroll;?? ?? import?android.util.Log;?? import?android.view.View;?? import?android.widget.AbsListView;?? import?android.widget.Toast;?? import?android.widget.AbsListView.OnScrollListener;?? ? ? ?? public?class?AutoLoadListener?implements?OnScrollListener{?? ?? ????public?interface?AutoLoadCallBack?{?? ????????void?execute(String?url);?? ????}?? ?? ????private?int?getLastVisiblePosition?=?0,lastVisiblePositionY=0;?? ????private?AutoLoadCallBack??mCallback;?? ????public?AutoLoadListener(AutoLoadCallBack?callback)?? ????{?? ????????this.mCallback?=?callback;?? ????}?? ?????? ????public?void?onScrollStateChanged(AbsListView?view,?int?scrollState)?{?? ?? ????????if?(scrollState?==?OnScrollListener.SCROLL_STATE_IDLE)?{?? ?????????????? ????????????if?(view.getLastVisiblePosition()?==?(view.getCount()?-?1))?{?? ????????????????View?v=(View)?view.getChildAt(view.getChildCount()-1);?? ????????????????int[]?location?=?new??int[2]?;?? ????????????????v.getLocationOnScreen(location);?? ????????????????int?y=location?[1];?? ?? ????????????????Log.e("x"+location[0],"y"+location[1]);?? ????????????????if?(view.getLastVisiblePosition()!=getLastVisiblePosition?? ????????????????????????&&?lastVisiblePositionY!=y)?? ????????????????{?? ????????????????????Toast.makeText(view.getContext(),?"再次拖至底部,即可翻頁",500).show();?? ????????????????????getLastVisiblePosition=view.getLastVisiblePosition();?? ????????????????????lastVisiblePositionY=y;?? ????????????????????return;?? ????????????????}?? ????????????????else?if?(view.getLastVisiblePosition()==getLastVisiblePosition?? ????????????????????????&&?lastVisiblePositionY==y)?? ????????????????{?? ????????????????????mCallback.execute(">>>>>拖至底部");?? ????????????????}?? ????????????}?? ?????????????? ?????????????? ????????????getLastVisiblePosition=0;????? ????????????lastVisiblePositionY=0;?? ????????}?? ????}?? ?? ????public?void?onScroll(AbsListView?arg0,?int?arg1,?int?arg2,?int?arg3)?{?? ?????????? ????}?? }?? </span>??
主程序為testScroll.java,源碼如下:
[java]?view plaincopyprint?
<span?style="font-family:Comic?Sans?MS;">package?com.testScroll;?? ?? import?java.util.ArrayList;?? import?java.util.HashMap;?? ?? import?com.testScroll.AutoLoadListener.AutoLoadCallBack;?? ?? ?? import?android.app.Activity;?? import?android.os.Bundle;?? import?android.view.View;?? import?android.widget.AdapterView;?? import?android.widget.AdapterView.OnItemClickListener;?? import?android.widget.GridView;?? import?android.widget.SimpleAdapter;?? import?android.widget.Toast;?? ?? public?class?testScroll?extends?Activity?{?? ?????? ????@Override?? ????public?void?onCreate(Bundle?savedInstanceState)?{?? ????????super.onCreate(savedInstanceState);?? ????????setContentView(R.layout.main);?? ????????setTitle("精確監聽AbsListView滾動至底部----hellogv");?? ????????GridView?gridview?=?(GridView)?findViewById(R.id.gridview);?? ?? ?????????? ????????ArrayList<HashMap<String,?Object>>?lstImageItem?=?new?ArrayList<HashMap<String,?Object>>();?? ????????for?(int?i?=?0;?i?<?30;?i++)?{?? ????????????HashMap<String,?Object>?map?=?new?HashMap<String,?Object>();?? ????????????map.put("ItemImage",?R.drawable.icon);?? ????????????map.put("ItemText",?"NO."?+?String.valueOf(i));?? ????????????lstImageItem.add(map);?? ????????}?? ?????????? ????????SimpleAdapter?saImageItems?=?new?SimpleAdapter(this,??? ????????????????lstImageItem,?? ????????????????R.layout.night_item,?? ?? ?????????????????? ????????????????new?String[]?{?"ItemImage",?"ItemText"?},?? ?? ?????????????????? ????????????????new?int[]?{?R.id.ItemImage,?R.id.ItemText?});?? ??????????? ?????????? ????????AutoLoadListener?autoLoadListener?=new?AutoLoadListener(callBack);?? ????????gridview.setOnScrollListener(autoLoadListener);?? ?????????? ????????gridview.setAdapter(saImageItems);?? ?????????? ????????gridview.setOnItemClickListener(new?ItemClickListener());?? ????}?? ?? ????AutoLoadCallBack?callBack=new?AutoLoadCallBack(){?? ?? ????????public?void?execute(String?url)?{?? ????????????Toast.makeText(testScroll.this,?url,?500).show();?? ????????}?? ?????????? ????};?? ?????? ????class?ItemClickListener?implements?OnItemClickListener?{?? ????????public?void?onItemClick(AdapterView<?>?arg0,?? ?????????????????????????????????????????????????????? ????????????????View?arg1,?? ????????????????int?arg2,?? ????????????????long?arg3?? ????????)?{?? ?????????????? ????????????HashMap<String,?Object>?item?=?(HashMap<String,?Object>)?arg0?? ????????????????????.getItemAtPosition(arg2);?? ?????????????? ????????????setTitle((String)?item.get("ItemText"));?? ????????}?? ?? ????}?? }?? </span> ?
總結
以上是生活随笔為你收集整理的精确监听AbsListView滚动至底部的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。