有弹性的ListView

有弹性的ListView

package com.weichuang.china.video.view;


import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.ListView;

public class MyListView extends ListView {
    private Context context;
    private boolean outBound = false;
    private int distance;
    private int firstOut;  

    public MyListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;

    }

    public MyListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.context = context;

    }

    public MyListView(Context context) {
        super(context);
        this.context = context;

    }

    GestureDetector gestureDetector = new GestureDetector(
            new OnGestureListener() {

                public boolean onSingleTapUp(MotionEvent e) {
                    // TODO Auto-generated method stub
                    return false;
                }


                public void onShowPress(MotionEvent e) {
                    // TODO Auto-generated method stub
                }



                public boolean onScroll(MotionEvent e1, MotionEvent e2,
                        float distanceX, float distanceY) {

                    int firstPos = getFirstVisiblePosition();
                    int lastPos = getLastVisiblePosition();
                    int itemCount = getCount();
                    // outbound Top
                    if (outBound && firstPos != 0 && lastPos != (itemCount - 1)) {
                        scrollTo(0, 0);
                        return false;
                    }
                    View firstView = getChildAt(firstPos);
                    if (!outBound)
                        firstOut = (int) e2.getRawY();
                    if (firstView != null
                            && (outBound || (firstPos == 0
                                    && firstView.getTop() == 0 && distanceY < 0))) {
                        // Record the length of each slide
                        distance = firstOut - (int) e2.getRawY();
                        scrollTo(0, distance / 2);
                        return true;
                    }

                    if (lastPos != (itemCount - 1))
                        return false;
                    View lastView = getChildAt(lastPos - firstPos);
                    int GridHeight = getHeight(); 
                    if (lastView != null && (outBound || 
                        ((lastView.getBottom() + 8) >= GridHeight && distanceY > 0))) { 
                        distance = firstOut - (int) e2.getRawY();
                        scrollTo(0, distance);
                        return true;
                    } 


                    return false;
                }


                public void onLongPress(MotionEvent e) {
                    // TODO Auto-generated method stub
                }

                public boolean onFling(MotionEvent e1, MotionEvent e2,
                        float velocityX, float velocityY) {
                    // TODO Auto-generated method stub
                    return false;
                }

                public boolean onDown(MotionEvent e) {
                    // TODO Auto-generated method stub
                    return false;
                }
            });

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {

        int act = event.getAction();

        if ((act == MotionEvent.ACTION_UP || act == MotionEvent.ACTION_CANCEL)
                && outBound) {
            outBound = false;

        }
        if (!gestureDetector.onTouchEvent(event)) {
            outBound = false;

        } else {
            outBound = true;
        }
        Rect rect = new Rect();
        getLocalVisibleRect(rect);
        TranslateAnimation am = new TranslateAnimation(0, 0, -rect.top, 0);
        am.setDuration(300);
        startAnimation(am);
        scrollTo(0, 0);
        return super.dispatchTouchEvent(event);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值