Swipe listeners in Android

前端 未结 4 1308
無奈伤痛
無奈伤痛 2021-02-08 09:49

Is there something like onLeftSwipeListener and onRightSwipeListener in Android? I want to switch views swiping finger back and forward. I use a FrameLayout and an ImageView ins

4条回答
  •  长发绾君心
    2021-02-08 10:37

    yourView.setOnTouchListner(onThumbTouch );
    
        OnTouchListener onThumbTouch = new OnTouchListener() {
                float previouspoint = 0 ;
                float startPoint=0;
                @Override
                public boolean onTouch(View v, MotionEvent event) {   
                    switch(v.getId()) {
                        case R.id.tvDetailsalaujairiyat: // Give your R.id.sample ...
                            switch(event.getAction()){
                                case MotionEvent.ACTION_DOWN:
                                    startPoint=event.getX();
                                    System.out.println("Action down,..."+event.getX());
                                break;
                                case MotionEvent.ACTION_MOVE:
    
                                break;
                                case MotionEvent.ACTION_CANCEL:
                                    previouspoint=event.getX();
                                    if(previouspoint > startPoint){
                                       //Right side swipe        
                                    }else{
                                       // Left side swipe
                                    }        
                                break;
                           }
                        break;
                    }
                    return true;
                }
            };
    

提交回复
热议问题