android listview 下拉回弹效果,ListView 拉出回弹效果 overscroll,阻尼效果

一  Android 9 之后才新增的API. 原理是

1. 在View中增加了overSrollBy方法,用于记录x, y 轴上滚动的之

2. 在AbsListView的onTouchEvent中判断是否到达边界(顶部 或 底部) ,然后调用view.overScrollBy ,传入 mScrollY等参数

3. overScrollBy 最终赋值给View的mScrollX, mScrollY 两个变量

4. 在AbsListView中调用完overScrollBy之后,调用invalidate重绘

二  下面是一个直接使用API的例子

1. 自定义ListView

public class BounceListView extends ListView{

private static final int MAX_Y_OVERSCROLL_DISTANCE = 200;

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);

}

}

2. Activity

public class MainActivity extends Activity {

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

LinearLayout linearLayout = new LinearLayout(this);

linearLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

setContentView(linearLayout);

BounceListView bounceListView = new BounceListView(this);

String[] data = new String[30];

for (int i = 0; i < data.length; i++) {

data[i] = "天天记录 " + i;

}

ArrayAdapter arrayAdapter =

new ArrayAdapter(this,android.R.layout.simple_list_item_1, data);

bounceListView.setAdapter(arrayAdapter);

linearLayout.addView(bounceListView);

}

}

资料:

2013-04-08   更新,这种效果也叫“阻尼效果”

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值