PART_ONLY 自定义类继承 ListView
,覆写 overScrollBy()
public class CListView extends ListView {
private static final int MAX_OVERSCROLLY_DISTANCE = 200;
private Context mCtx;
private int mMaxYOverscrollDistance;
public CListView(Context context) {
super(context);
mCtx = context;
initBounceListView();
}
public CListView(Context context, AttributeSet attrs) {
super(context, attrs);
mCtx = context;
initBounceListView();
}
public CListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mCtx = 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 = mCtx.getResources().getDisplayMetrics();
final float density = metrics.density;
mMaxYOverscrollDistance = (int) (density * MAX_OVERSCROLLY_DISTANCE);
/**
* @author WangYehan
*
* @desc 上面的mMaxYOverscrollDistance有点问题,直接改成MAX_OVERSCROLLY_DISTANCE效果才出来。。
*/
mMaxYOverscrollDistance = MAX_OVERSCROLLY_DISTANCE;
}
@Override
protected boolean overScrollBy(int deltaX, int deltaY, int scrollX, int scrollY, int scrollRangeX, int scrollRangeY, int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) {
/**
* @author WangYehan
*
* @desc 摩擦摩擦
*/
if (!isTouchEvent) { // 禁止惯性滑动
if ((scrollY < 0 && deltaX < 0) || (scrollY > getHeight() && deltaX > 0)) {
deltaY = 0;
}
}
// This is where the magic happens, we have replaced the incoming maxOverScrollY with our own custom variable mMaxYOverscrollDistance;
return super.overScrollBy(deltaX, (deltaY + 1) / 2, scrollX, scrollY, scrollRangeX, scrollRangeY, maxOverScrollX, mMaxYOverscrollDistance, isTouchEvent);
}
}
以上。如有错误和疑问,欢迎指正提出。