android之ExpandableListView 的滑动到底部的精确监听事件
1.给expandableListView 监听滑动到最底部的事件。最传统的方法不适合。
// 监听listview滚到最底部 expandableListView.setOnScrollListener(new OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view, int scrollState) { switch (scrollState) { // 当不滚动时 case OnScrollListener.SCROLL_STATE_IDLE: // 判断滚动到底部 if (view.getLastVisiblePosition() == (view.getCount() - 1)) { //TODO } break; } } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { } });
2.第二种方法,网上搜到的。虽然这个方法没解决我的问题,却给了我很大灵感。用了全局坐标来定位。
参考URL:http://www.oschina.net/question/234345_44392
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) { } }
3.我参考精确坐标,我用了相对坐标。
- 先算出了除却标题与状态栏的屏幕高度,即ExpandableListView所占屏幕的高度;
titleBarHeight = Util.dip2px(48); Rect frame = new Rect(); getActivity().getWindow().getDecorView().getWindowVisibleDisplayFrame(frame); statusBarHeight = frame.top; otherHeight = DeviceInfo.HEIGHT - titleBarHeight - statusBarHeight;
- 然后ExpandableListView滑动到最底部时,ExpandableListView离最屏幕底部是一个定值,我用的pad分辨率为1024*600.这个宣传即是490;
phoneExSearchList.setOnScrollListener(new OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view, int scrollState) { Logger.d(tag, "onScrollStateChanged... ..." + scrollState); if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) { Logger.d(tag, "getLastVisiblePosition... ..." + view.getLastVisiblePosition() + "-" + (view.getCount() - 1)); if (view.getLastVisiblePosition() == (view.getCount() - 1)) { Logger.d(tag, "DeviceInfo.HEIGHT - " + DeviceInfo.HEIGHT); Logger.d(tag, "DeviceInfo.WIDTH - " + DeviceInfo.WIDTH); if (Math.abs(otherHeight - v.getBottom()) < 5) { if (Util.isGoOn("onscroll", 800)) { mhandler.sendEmptyMessage(SearchTudouResultFragment.GET_SEARCH_PAGE_TURN); } } } } } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { } });
我要青春像陈孝正一样,不能有一毫米的误差!
我要青春像合伙人一样,为了自尊而战!