Androidスライドリバウンド効果

20238 ワード

原理:
addHeaderViewでやったこと:
1.ヘッダの幅を測定し、measureViewメソッドを呼び出した.LayoutParamsの設定、幅:MATCH_PARENT、高さ:10 3.トップMarginの値を負のヘッダーの高さに設定し、ヘッダーを画面の一番上に隠すonInterceptTouchEvent:
スライド距離がゼロの場合は、onInterceptTouchEventで処理します.画面にスライド効果はありません.
onTouchEvent:スライド距離がゼロより大きい場合はonTouchEventで処理します.スライド中にヘッドのtopMarginを計算し続け、画面にスライド効果を与えます.
コード:
/**  * android     by haidaye  */ public class HaidayeView extends LinearLayout {
    private int mLastMotionY;
    /**  *   view  */  private View mHeaderView;
    private LayoutInflater mInflater;
    private ScrollView mScrollView;
    /**  *   view    */  private int mHeaderViewHeight;

    public HaidayeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public HaidayeView(Context context) {
        super(context);
        init();
    }

    private void init() {
        // Load all of the animations we need in code rather than through XML
        mInflater = LayoutInflater.from(getContext());
        // header view     ,         linearlayout    
        addHeaderView();
    }

    private void addHeaderView() {
        // header view
        mHeaderView = mInflater.inflate(R.layout.header, this, false);
        measureView(mHeaderView);
        mHeaderViewHeight = mHeaderView.getMeasuredHeight();
        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
                mHeaderViewHeight);
        //   topMargin     header View  ,         
        params.topMargin = -(mHeaderViewHeight);
        addView(mHeaderView, params);

    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        initContentAdapterView();
    }

    private void initContentAdapterView() {
        mScrollView = (ScrollView) getChildAt(1);
    }

    private void measureView(View child) {
        ViewGroup.LayoutParams p = child.getLayoutParams();
        if (p == null) {
            p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
        }

        int childWidthSpec = ViewGroup.getChildMeasureSpec(0, 0 + 0, p.width);
        int lpHeight = p.height;
        int childHeightSpec;
        if (lpHeight > 0) {
            childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight,
                    MeasureSpec.EXACTLY);
        } else {
            childHeightSpec = MeasureSpec.makeMeasureSpec(0,
                    MeasureSpec.UNSPECIFIED);
        }
        child.measure(childWidthSpec, childHeightSpec);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent e) {
        int y = (int) e.getRawY();
        switch (e.getAction()) {
            case MotionEvent.ACTION_DOWN:
                //     down  ,  y  
                mLastMotionY = y;
                break;
            case MotionEvent.ACTION_MOVE:
                // deltaY > 0      ,< 0     
                int deltaY = y - mLastMotionY;
                if (isRefreshViewScroll(deltaY)) {
                    return true;
                }
                mLastMotionY = y;
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                break;
        }
        return false;
    }

    /*
     *    onInterceptTouchEvent()       ( onInterceptTouchEvent()    return
     * false)  PullToRefreshView   View   ;           (  HaidayeView     )
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int y = (int) event.getRawY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                // onInterceptTouchEvent    
                // mLastMotionY = y;
                break;
            case MotionEvent.ACTION_MOVE:
                int deltaY = y - mLastMotionY;
                headerPrepareToRefresh(deltaY);
                mLastMotionY = y;
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                int topMargin = getHeaderTopMargin();
                setHeaderTopMargin(-mHeaderViewHeight);
                break;
            case MotionEvent.ACTION_POINTER_DOWN://      
                this.mLastMotionY = y;
                break;
        }
        return super.onTouchEvent(event);
    }

    /**  *        View, PullToRefreshView    *  * @param deltaY , deltaY > 0      ,< 0       * @return  */  private boolean isRefreshViewScroll(int deltaY) {
        //   ScrollView
        if (mScrollView != null) {
            //  scroll view      
            View child = mScrollView.getChildAt(0);
            if (deltaY > 0 && mScrollView.getScrollY() == 0) {
                return true;
            } else if (deltaY < 0
                    && child.getMeasuredHeight() <= getHeight()
                    + mScrollView.getScrollY()) {
                return true;
            }
        }
        return false;
    }

    /**  * header     ,      ,       *  * @param deltaY ,         */  private void headerPrepareToRefresh(int deltaY) {
        int newTopMargin = changingHeaderViewTopMargin(deltaY);
    }

    private int changingHeaderViewTopMargin(int deltaY) {
        LayoutParams params = (LayoutParams) mHeaderView.getLayoutParams();
        float newTopMargin = params.topMargin + deltaY * 0.3f;
        params.topMargin = (int) newTopMargin;
        mHeaderView.setLayoutParams(params);
        invalidate();
        return params.topMargin;
    }

    /**  *   header view  topMargin    */  private void setHeaderTopMargin(int topMargin) {
        LayoutParams params = (LayoutParams) mHeaderView.getLayoutParams();
        params.topMargin = topMargin;
        mHeaderView.setLayoutParams(params);
        invalidate();
    }

    private int getHeaderTopMargin() {
        LayoutParams params = (LayoutParams) mHeaderView.getLayoutParams();
        return params.topMargin;
    }
}

海おじさんに敬意を表します