精确监听AbsListView滚动至底部
程序员文章站
2023-12-22 09:18:46
...
本文来自http://blog.csdn.net/hellogv/ ,引用必须注明出处!
用户使用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值都一样,即到底然后执行回调函数......源码如下:
- <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,源码如下:
- <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 {
- /** Called when the activity is first created. */
- @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); // 添加图像资源的ID
- map.put("ItemText" , "NO." + String.valueOf(i)); // 按序号做ItemText
- lstImageItem.add(map);
- }
- // 生成适配器的ImageItem <====> 动态数组的元素,两者一一对应
- SimpleAdapter saImageItems = new SimpleAdapter( this , // 没什么解释
- lstImageItem,// 数据来源
- R.layout.night_item,// night_item的XML实现
- // 动态数组与ImageItem对应的子项
- new String[] { "ItemImage" , "ItemText" },
- // ImageItem的XML文件里面的一个ImageView,两个TextView ID
- 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();
- }
- };
- // 当AdapterView被单击(触摸屏或者键盘),则返回的Item单击事件
- class ItemClickListener implements OnItemClickListener {
- public void onItemClick(AdapterView<?> arg0, // The AdapterView where the
- // click happened
- View arg1,// The view within the AdapterView that was clicked
- int arg2, // The position of the view in the adapter
- long arg3 // The row id of the item that was clicked
- ) {
- // 在本例中arg2=arg3
- HashMap<String, Object> item = (HashMap<String, Object>) arg0
- .getItemAtPosition(arg2);
- // 显示所选Item的ItemText
- setTitle((String) item.get("ItemText" ));
- }
- }
- }
- </span>