Home | History | Annotate | Download | only in cardstream
      1 /*
      2 * Copyright 2013 The Android Open Source Project
      3 *
      4 * Licensed under the Apache License, Version 2.0 (the "License");
      5 * you may not use this file except in compliance with the License.
      6 * You may obtain a copy of the License at
      7 *
      8 *     http://www.apache.org/licenses/LICENSE-2.0
      9 *
     10 * Unless required by applicable law or agreed to in writing, software
     11 * distributed under the License is distributed on an "AS IS" BASIS,
     12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13 * See the License for the specific language governing permissions and
     14 * limitations under the License.
     15 */
     16 
     17 
     18 
     19 
     20 package com.example.android.batchstepsensor.cardstream;
     21 
     22 import android.content.Context;
     23 import android.util.AttributeSet;
     24 import android.view.MotionEvent;
     25 import android.view.ViewConfiguration;
     26 import android.widget.RelativeLayout;
     27 
     28 /**
     29  * Custom Button with a special 'pressed' effect for touch events.
     30  */
     31 public class CardLayout extends RelativeLayout {
     32 
     33     private boolean mSwiping = false;
     34     private float mDownX = 0.f;
     35     private float mDownY = 0.f;
     36     private float mTouchSlop = 0.f;
     37 
     38     public CardLayout(Context context) {
     39         super(context);
     40         init();
     41     }
     42 
     43     public CardLayout(Context context, AttributeSet attrs) {
     44         super(context, attrs);
     45         init();
     46     }
     47 
     48     public CardLayout(Context context, AttributeSet attrs, int defStyle) {
     49         super(context, attrs, defStyle);
     50         init();
     51     }
     52 
     53     private void init(){
     54         setFocusable(true);
     55         setDescendantFocusability(FOCUS_AFTER_DESCENDANTS);
     56         setWillNotDraw(false);
     57         setClickable(true);
     58 
     59         mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop() * 2.f;
     60     }
     61 
     62     @Override
     63     public boolean onTouchEvent(MotionEvent event) {
     64         switch(event.getAction()){
     65             case MotionEvent.ACTION_CANCEL:
     66             case MotionEvent.ACTION_UP:
     67                 mSwiping = false;
     68                 break;
     69         }
     70         return super.onTouchEvent(event);
     71     }
     72 
     73     @Override
     74     public boolean onInterceptTouchEvent(MotionEvent event) {
     75 
     76         switch(event.getAction()){
     77             case MotionEvent.ACTION_MOVE:
     78                 if( !mSwiping ){
     79                     mSwiping = Math.abs(mDownX - event.getX()) > mTouchSlop;
     80                 }
     81                 break;
     82             case MotionEvent.ACTION_DOWN:
     83                 mDownX = event.getX();
     84                 mDownY = event.getY();
     85                 mSwiping = false;
     86                 break;
     87             case MotionEvent.ACTION_CANCEL:
     88             case MotionEvent.ACTION_UP:
     89                 mSwiping = false;
     90                 break;
     91         }
     92         return mSwiping;
     93     }
     94 }