获取ListView中的Item的位置与Item对象,列表页下拉手势
在开发列表功能的时候我们往往需要在列表最底部下拉的时候进行获取下一页数据,这是我们可以使用手势来实现.
GestureDetector与GestureDetector.OnGestureListener来实现手势的操作,我个人的理解手势其实就是记录用户对屏幕的起始点与结束点之间的动作,例如. ACTION_DOWN代表手指按下 ACTION_UP手指松开, ACTION_MOVE手指移动 , 每一个动作都会产生一个MotionEvent事件.
用户对屏幕的操作过程都会产生MotionEvent事件,如果我们自己想处理这个事件就需要在对应的View设置一个监听器 ,通过setOnTouchListener来设置,具体代码如下
articleList.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
detector.onTouchEvent(event);
return false;
}
});
如果使用自己处理MotionEvent来识别手势会很麻烦,所以我们还是使用GestureDetector来实现手势识别,GestureDetector由需要一个回调来让用户觉得处理哪种手势,将 GestureDetector.OnGestureListener一个实例作为一个参数传递给GestureDetector来完成相关工作.
如下代码:
detector = new GestureDetector(this , new GestureDetector.OnGestureListener(){
public Rect mRectSrc = new Rect();
public Point mPoint = new Point();
int pos[] = new int[2];
/**
* Notified when a tap occurs with the down {@link MotionEvent}
* that triggered it. This will be triggered immediately for
* every down event. All other events should be preceded by this.
*
* @param e The down motion event.
*/
@Override
public boolean onDown(MotionEvent e) {
return false;
}
/**
* The user has performed a down {@link MotionEvent} and not performed
* a move or up yet. This event is commonly used to provide visual
* feedback to the user to let them know that their action has been
* recognized i.e. highlight an element.
*
* @param e The down motion event
*/
@Override
public void onShowPress(MotionEvent e) {
}
/**
* Notified when a tap occurs with the up {@link MotionEvent}
* that triggered it.
*
* @param e The up motion event that completed the first tap
* @return true if the event is consumed, else false
*/
@Override
public boolean onSingleTapUp(MotionEvent e) {
return false;
}
/**
* Notified when a scroll occurs with the initial on down {@link MotionEvent} and the
* current move {@link MotionEvent}. The distance in x and y is also supplied for
* convenience.
*
* @param e1 The first down motion event that started the scrolling.
* @param e2 The move motion event that triggered the current onScroll.
* @param distanceX The distance along the X axis that has been scrolled since the last
* call to onScroll. This is NOT the distance between {@code e1}
* and {@code e2}.
* @param distanceY The distance along the Y axis that has been scrolled since the last
* call to onScroll. This is NOT the distance between {@code e1}
* and {@code e2}.
* @return true if the event is consumed, else false
*/
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
//Log.w("demo", "GlobalVisibleRect--->" + mRectSrc);
//articleList.getVerticalScrollbarPosition();
//android.support.v4.widget.SwipeRefreshLayout;
//articleList.getGlobalVisibleRect()
return false;
}
/**
* Notified when a long press occurs with the initial on down {@link MotionEvent}
* that trigged it.
*
* @param e The initial on down motion event that started the longpress.
*/
@Override
public void onLongPress(MotionEvent e) {
}
/**
* Notified of a fling event when it occurs with the initial on down {@link MotionEvent}
* and the matching up {@link MotionEvent}. The calculated velocity is supplied along
* the x and y axis in pixels per second.
*
* @param e1 The first down motion event that started the fling.
* @param e2 The move motion event that triggered the current onFling.
* @param velocityX The velocity of this fling measured in pixels per second
* along the x axis.
* @param velocityY The velocity of this fling measured in pixels per second
* along the y axis.
* @return true if the event is consumed, else false
*/
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
return false;
}
}
以上方法中onFling与onScroll来处理滚动事件,onFling在滑动结束系统自动调用,onScroll 在用户滑动的过程中请求,一次滑动会收到N此请求. onDown用于处理用户手指按下请求.
Q:在处理下拉过程中我们需要知道列表页面是否已经到底部?
A:我们需要调用getLastVisiblePosition()来得知列表业最后一条记录的index, getCount()返回列表中所有记录总数 , 只要比较这两个值是否相等就知道是否是到达列表最底部!
Q:如何检查向下滚动是否超出已定的范围?
A:可以比较列表页的getBottom与列表Item的getBottom来比较
Q:如何取得列表Item的View对象
A:通过ListView对象的getChildAt(index)来获取
在调用getChildAt时候只返回当前屏幕的某一个Item ,可以通过getChildrenCount来检查当前屏幕的总Item个数,然后取得最后一个就OK了, getChildAt方法需要的位置不是Adapter中的位置,是当前屏幕显示区域中相对的总数的偏移!
web开发工程师一名,喜欢研究技术,学习新技术.爱好:读书,电影,民谣,乡村音乐,相声,羽毛球,爬山,徒步,动物!

浙公网安备 33010602011771号