2 usniyo usniyo 于 2012.11.17 00:12 提问

android 的ListView中,如何判断其内容已滚动到最顶部或者最底部?

android 的ListView中,如何判断其内容已滚动到最顶部或者最底部?

2个回答

Sueyexin
Sueyexin   2012.11.19 13:11
已采纳

根据这个方法检测:

getListView().setOnScrollListener(new OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if(firstVisibleItem==0){
                    Log.e("log", "滑到顶部");
                }
                if(visibleItemCount+firstVisibleItem==totalItemCount){
                    Log.e("log", "滑到底部");
                }
            }
        });
Sueyexin
Sueyexin 不客气
接近 5 年之前 回复
usniyo
usniyo 非常感谢!
接近 5 年之前 回复
ce_la_vie
ce_la_vie   2012.11.19 09:49

不知道你是不是要这种效果?

代码:

<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 {
    /** 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>

原文和效果图连接在这里:http://djt.qq.com/thread-28459-1-1.html

usniyo
usniyo 谢谢!
接近 5 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片