从2.3开始,android开始支持listview的overscroll, 应该很方便可以做出类似iPhone的弹性滚动,及越过list顶端或者底端,然后弹性滚回。大致如下:
package com.iaiai.activity;
import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.widget.ListView;
/**
*
* <p>
* Title: BounceListView.java
* </p>
* <p>
* E-Mail: [email protected]
* </p>
* <p>
* QQ: 176291935
* </p>
* <p>
* Http: iaiai.iteye.com
* </p>
* <p>
* Create time: 2011-12-30 下午3:07:49
* </p>
*
* @author 丸子
* @version 0.0.1
*/
public class BounceListView extends ListView {
private static final int MAX_Y_OVERSCROLL_DISTANCE = 500;
private Context mContext;
private int mMaxYOverscrollDistance;
public BounceListView(Context context) {
super(context);
mContext = context;
initBounceListView();
}
public BounceListView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
initBounceListView();
}
public BounceListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mContext = context;
initBounceListView();
}
private void initBounceListView() {
// get the density of the screen and do some maths with it on the max
// overscroll distance
// variable so that you get similar behaviors no matter what the screen
// size
final DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
final float density = metrics.density;
mMaxYOverscrollDistance = (int) (density * MAX_Y_OVERSCROLL_DISTANCE);
}
@Override
protected boolean overScrollBy(int deltaX, int deltaY, int scrollX, int scrollY, int scrollRangeX,
int scrollRangeY, int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) {
// This is where the magic happens, we have replaced the incoming
// maxOverScrollY with our own custom variable mMaxYOverscrollDistance;
return super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX, scrollRangeY, maxOverScrollX,
mMaxYOverscrollDistance, isTouchEvent);
}
}